图片转符号

Paste_Image.png
                                        &********
                                   *%%%%%%%%%%%%%%%%%*
                                *%%%%%****%%%%%%%****&%%*
                              *%%%%%%      *%%%%      *%%%&*
                             %%%%%%&        *%%        *%%%%&
                           *%%%%%%%          %*         %%%%%%*
                         %%%%%%%%%*          %          &%%%%%%&
                        *%%%%%%%%%&          *          *%%%%%%%%
                       *%%%%%%%%%%                       %%%%%%%%%&
                      *%%%%%%%%%%%       **&    **       %%%%%%%%%%
                     *%%%%%%%%%%*%       ***    **        %%%%%%%%%
                    *%%%%%%%%&&          * *     *           *%%%%%%*
                   *%%%%%%%&      &       *  &  *&      *      *%%%&&*
                   %%**%%%*       %          ***                 * %%%
                  *%%%%% *         *        *****&     *       *  *%%%*
                  %%%%%&    &*      *      *  ****    *     *      %%%%
                 *%%%%&         *     *  * *  ****        *         *%%*
                 %%%%%                     *******     *        *   **%%
                *%%%%*****                 %******                   *&%*
                %%%%%       *               *****                      %%
               &%%%%                          *                *********%
               *%%%*                          *                        *%*
               &%%%*                          *                         %*
               %%%%*                          *                       * %*
               %%%&                           *          *            * %*
               %%%*                           *              *          %*
               %%%*            *%           &&**&                **  *  **
               %%%*  *     **      *************************        * * **
               %%%*    **      ************************************     **
               %%%* *    ***************************************        %*
               %%%*      &**************************************        **
               *%%*       **************************************        **
               *%%*       *************************************         **
                %%&        ************************************         *
                %%%        ************************************        **
                *%%         **********************************        %**
                 %%*        &*********%%************%********&        &*   *     *
                 *%*         ******%******************%******        %**  *
                  %%          **************%***************         &** *         *
                  *%*          **%*************************%        &**
                   %%           ***************************        %&**             *
                   &%*           %************************         &**  *           *
                    *%            ***********************         &&*  %%           %
                     &%            ********************          &&***%%%
                      %*             ***************%%          &&**%%%%%          *
                      &%               *&**********            &&&*%%%%%%%*       *
                       %%                  *****              &&&*%%%%%%%%%%*   *
                        *%                                    ***%%%%%%%%%%%%*
                         ***                         ************%%%%%%%%%%%*
                         ********************%*******************%%%%%%%%%%%
                         ******************* &&&&&************** %%%%%%%%%%*
                        %%*****************  *   *&********%&%%%*%%%%%%%%**
                      &%%%%%%%%%%*         *& *  *&*       &%%%%%%%%%%%%**
                     *%%%%%%%%%%*         *  & * &&*       %%%%%&%%%%%***
                    *%%%%%%%%%%%          *&&&%**&&&         %%%%%%%**%
                   *%%%%%%%%%%%*           &&&***&&*        *%%%%&****
                  *%%%%%%&%%%%%            *&&&*&&&          %%%%***&
                  %%%%%%%%%%%%               &&*&&&          &%%%**
                 &%%%%%%%*%%%%*               ***            *%%%&
                *%%%%%%%%*%%%%*                              *%%%*
              ****%%%%%%%*%%%%              %%******    **   *%%%*
            *      *%%%%%*%%%%                               *%%%*
           *        *%%%**%%%%                               *%%%*
                     &%&**%%%%                           %   %%%%*
          *          *&***%%%%                           *   %%%%
          *          *** &%%%%&   *                          %%%%
          *          *    %%%%*                         *   *%%%%
                     &    %%%%%    &                        %%%%%
                          %%%%%*                       &   *%%%%%
                          %%%%%%                      *    %%%%%&
             *    *       *%%%%%%%    *                    %%%%%%
                          %%%%%%%%*      *       %*      &%%%%%%*
                           %%%%%%%%&*                  *%%%%%%%%*
                           %%%%%%%%%% *              *%%%%%%%%%%*
                           &%%%%%%%%%%%%%**      ** %%%%%%%%%%%%
                           &%%%%%%%%%%%%%%%%%*****%%%%%%%%%%%%%%
                           &%%%%%%%%%%%%%%%*  ****%%%%%%%%%%%%%%
                          **%%%%%%%%%%%%%%**   **%%%%%%%%%%%%%%% *
                        %   * %%%%%%%%%%%%**   * %%%%%%%%%&***     *
                       &        &********&
                                            %*                      *
                      *                      *                      *
                                             *
                      *                     *                      *
                                               &                  *
                        *                &*     &*             **
                          %***%%%%%***%                %***

要实现上面的功能还是比较简单的,先把三通道图像转为灰度图像,然后根据灰度值进行替换。

def img2txt(path):
    im = Image.open(path).convert('L').resize((100,100))
    data = np.array(im)
    lines = []
    for i in range(100):
        line = []
        for j in range(100):
            c = data[i,j]
            if c>200:
                x = ' '
            elif c>150:
                x = '&'
            elif c>100:
                x = '%'
            else :
                x = '*'
            line.append(x)
        lines.append(''.join(line))
    return lines

除了把这些图像转为符号之外,我们还可以把这些符号转为图片。

#params:lines=text lines,size=font size,font = font type
def lines2img(lines,size=10,font ='simfang.ttf'):
    im = Image.new('RGB',(1000,1000),(255,255,255))
    font = ImageFont.truetype(font,10)
    draw = ImageDraw.Draw(im)
    for i,l in enumerate(lines):
        for j,c in enumerate(l):
            draw.text((j*10,i*10),c,font=font,fill=(0,0,0))
    return im

推荐阅读更多精彩内容