Java image to character output example demo

Java image to character output example demo

The previous blog posts introduced the use of jdk to do some interesting conversion of pictures. Next, we will introduce an interesting way to output a two-dimensional character array directly according to the pictures to realize the scene of painting with characters

<!-- more -->

You may have seen some interesting comments, such as giant Buddha and beauty. I believe you can easily implement similar scenes

The key implementation is also mentioned in the previous article. The following is the hyperlink

What we need to do next is to slightly change the output of the character picture, select the appropriate replacement character according to the current color and save it

Therefore, the key implementation is how to select characters according to color

// This character comes from the github search results. The last character is changed from the original dot to a space, that is, when it is white, no character is output
private static final String DEFAULT_CHAR_SET = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\\\"^`' ";

/**
 * Obtain the corresponding characters based on the gray value of color
 * @param g
 * @return
 */
public static char toChar(Color g) {
    double gray = 0.299 * g.getRed() + 0.578 * g.getGreen() + 0.114 * g.getBlue();
    return DEFAULT_CHAR_SET.charAt((int) (gray / 255 * DEFAULT_CHAR_SET.length()));
}

Next, we will modify the previous method slightly

Color getAverage(BufferedImage image, int x, int y, int w, int h) {
    int red = 0;
    int green = 0;
    int blue = 0;

    int size = 0;
    for (int i = y; (i < h + y) && (i < image.getHeight()); i++) {
        for (int j = x; (j < w + x) && (j < image.getWidth()); j++) {
            int color = image.getRGB(j, i);
            red += ((color & 0xff0000) >> 16);
            green += ((color & 0xff00) >> 8);
            blue += (color & 0x0000ff);
            ++size;
        }
    }

    red = Math.round(red / (float) size);
    green = Math.round(green / (float) size);
    blue = Math.round(blue / (float) size);
    return new Color(red, green, blue);
}

private void parseChars(BufferedImage img) {
    int w = img.getWidth(), h = img.getHeight();
    // This size can be used to control the accuracy. The smaller the size, the more like the original image
    int size = 4;
    List<List<String>> list = new ArrayList<>();
    for (int y = 0; y < h; y += size) {
        List<String> line = new ArrayList<>();
        for (int x = 0; x < w; x += size) {
            Color avgColor = getAverage(img, x, y, size, size);
            line.add(String.valueOf(toChar(avgColor)));
        }
        list.add(line);
    }

    System.out.println("---------------------- start ------------------------");
    for (List<String> line: list) {
        for (String s: line) {
            System.out.print(s + " ");
        }
        System.out.println();
    }
    System.out.println("---------------------- end ------------------------");
}

Pay attention to the above implementation. It is important to pay attention to the traversal mode of the original graph, layer by layer, that is, the external is the y-axis and the internal loop is the x-axis

Next, let's look at the test case

@Test
public void testChars() throws Exception{
    String file = "http://pic.dphydh.com/pic/newspic/2017-12-13/505831-1.png";
    BufferedImage img = ImageLoadUtil.getImageByPath(file);
    // Zoom the picture to 300x300 to take a screenshot of the output characters
    img = GraphicUtil.scaleImg(300,300, img);
    parseChars(img);
    System.out.println("---over------");
}

The actual output is as follows (the actual output is still very similar to the skin God)

---------------------- start ------------------------
                                                                                          l m                                                         
                                                                                        ' b $ I                                                       
                                                                                        f $ $ [                                                       
                                                                                      \ 8 $ $ f                                                       
  i ~ ,                                                                               x $ $ $ u                                                       
  _ $ $ a X } ^                                                                     ' W $ $ $ c                                                       
    c $ $ $ $ B L ] '                                                               } q $ $ $ z                                                       
    ` d $ $ $ $ $ 0 r ( "                                                           t < U $ $ c                                                       
      , * $ $ $ $ z < + j | `                                                     \ t < < O $ n                                                       
        l W $ $ $ U < < < ~ t [                                                   { + < < _ W f                                                       
          > & $ $ 0 < < < < < - j ~                                               \ < < < < n (                                                       
            ! # $ k < < < < < < < ( t `                                           j < < < < ] ?                                                       
              : k B 1 + < < < < < < + n !                                       > } < < < < \ i                                                       
                ^ C z ( [ ~ < < < < < < f ]                                     1 < < < < < u `                                                       
                    1 v ( ) ? < < < < < < | {                                   ( < < < < < u                                                         
                      I v / ( 1 + < < < < < 1 }   ' l > i "                     \ < < < < ~ x                                                         
                          1 v ( ( [ ~ < < < < z r z t | \ n z f ( + '           t < < < < ? /                                                         
                            " / v | ) ? < < < < < < < < < < < < < ] f ) ^     " \ < < < < | ?                                                         
                                " ) n v / ~ < < < < < < < < < < < < < ~ j [   l 1 < < < + z ^                                                         
                                      - | < < < < < < < < < < < < < < < < ] j ~ { < < < [ u                                                           
                                    ' f < < < < < < < < < < < < < < < < < < ~ z | < < ~ ( /                                                           
                                    ] + < < < < < < < < < < < < < < < < < < < < n - < ? n i                                                           
                                  ' | < < < < < < < < < < < < < < < < < < < < < < { ~ ) v                                                             
                                  ) + < ] 0 w f < < < < < < < < < < < < < < < < < < ? / 1                                                             
                                  x < ~ * @ " | [ < < < < < < < < < < < < < < < < < } c '                                                             
                                i ( < } $ $ x w \ < < < < < < < < < < < < < < < < < / -                                                               
                                / < < + % $ $ 8 _ < < < < < < < < < < < < < < < < < { >                                                               
                              _ q f < < ( q m } < < < < < < < < < < < < { \ ~ < < < } <                                                               
                            " O U Z < < < < < < < < < < n f < < < < < n r [ h + < < \ "                                                               
                            j U U 0 } < < < < _ < < < < ~ ~ < < < < < M u ( $ r < < t                                                                 
                            U U U Q ( < < < < { Y v Y 0 Z } < < < < < * $ $ $ x < < |                                                                 
                            J U U O [ < < < < < # * # # o a t < < < < j $ $ # _ < < )                                                                 
                            Y U U O < < < < < < W b q q k # # O r \ < < [ / + < < } <                                                                 
                            x U L r < < < < < < d U c c C w * o L < < < < < < < < f '                                                                 
                            } Q n < < < < < < < J x x x x z w W [ < < < < < < < < f         : + [ { ,                                                 
                            ^ f < < < < < < < < L x x x x x J Y < < < < < < _ Y O Y   ] \ j \ [ _ } -                                                 
                              / < < < < < < < < L x x x x x C _ < < < < < - Z U U Z j ? < < < < < \ v >                                               
                              l | < < < < < < < Y x x x x X | < < < < < < J U U U z < < < < < < < + ) (                                               
                        i ) t / L | + < < < < < c x x x c x < < < < < < ) L U U C t < < < < < < < < f !                                               
                    ? x ( < < < < ? f x j ~ < < 1 J x Y x < < < < < < < u U U U 0 < < < < < < < < + r                                                 
                  1 { < < < < < < < < < _ x \ < < t v 1 < < < < < < < < n U U Z [ < < < < < < < v x _                                                 
                < ) < < < < < < < < < < < < { u ] < < < < < < < < < < < - 0 m { < < < < < < < } n | / n r ( / \ 1 } i '                               
                / < < < < < < < < < < < < < < ~ U < < < < < < < < < < < | c _ < < < < < < < - n < < < < < < < < < < { f / ( ] ^                       
                | < < < < < < < < < < < < < < < ) n ] _ ~ < < < < < / n 1 < < < < < < < ~ [ L + < < < < < < < < < < < < < < _ t / 1 l                 
                t < < < < < < < < < < < < < < < v j ( ( ) - < < ~ } + < < < < < < < < _ 1 Q j < < < < < < < < < < < < < < < < < < < ) f ( :           
                ) + < < < < < < < < < < < < < < c [ ] ? ~ < < < < < < < < < < < < ~ } ( c t [ < < < < < < < < < < < < < < < < < < < < < ~ / t <       
                ^ x { } ] ] - _ ~ < < < ~ _ ~ r c < < < < < < < < < < < < < < < - ) ( u < \ < < 1 - < < < < < < < < < < < < < < < < < < < < < \ )     
                  < c ( ( ( ( ( ( ( ) ) / Y n n < < < < < < < < < < < < < < ~ { ( ( v i   f < _ ( ( 1 _ < < < < < < < < < < < < < < < < < < + j '     
                    ! v n ( ( ( ( r X c f ~ < < < < < < < < < < < < < < < ~ 1 ( ( c ;     r < ] ( ( ( ( } + < < < < < < < < < < < < < < < + f '       
                        ] X x u u | + < < < < < < < < < < < < < < < < < < { ( t n ^     \ f < { ( ( ( ( ( ( [ ~ < < < < < < < < < < < < + / '         
                        t ~ < < < < < < < < < < < < < < < < < < < < < < - ( v {         ? ] < ) ( ( ( ( ( ( ( ) ? < < < < < < < < < < + \             
                        u < < < < < < < < < < < < < < < < < < < < < < < { w :           \ < + ( ( ( | ( ( ( ( ( ( { ~ < < < < < < < ~ (               
                      i \ < < < < < < < < < < < < < < < < < < < < < < < ~ n             j < - ( ( / x t c n ( ( ( ( ) - < < < < < ~ (                 
                      t < < < < < < < < < < < < < < < < < < < < < < < < < n   + ] :     x < [ ( ( v '     ! | n X \ ( ( [ < < < ~ /                   
                      u < < < < < < < < < < < < < < < < < < < < < < < < < x   ( 1 | r t ( < { ( x +             " { j z r { ~ ~ f '                   
                    ~ 1 < < < < < < < < < < < < < < < < < < < < < < < < < t   ] t 1 + < < < ( ( x                     ' ~ / n u ^                     
                    t < < < < < < < < < < < < < < < < < < < < < < < < < < \   i n ( ( ? < + ( v :                                                     
                  ' r < < < < < < < < < < < < < < < < < < < < < < < < < < /   " z ( ( ( } ] | \                                                       
                  _ [ < < < < < < < < < < < < < < < < < < < < < < < < < < t     L t \ Y u z z `                                                       
                  / < < < < < < < < < < < < < < < < < < < < < < < < < < < / ;   Z Q Q \   , I                                                         
                ' f < < < < < < < < < < < < < < < < < < < < < < < < < < < + # 0 d d d }                                                               
                ] ? < < < < < < < < < < < < < < < < < < < < < < < < < < < < Z d d d b ?                                                               
                \ < < < < < < < < < < < < < < < < < < < < < < < < < < < < < u o b d k ~                                                               
                j < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ] | ? u d :                                                               
                j < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < j                                                                       
                r < ~ < < < < < < < < < < < < < < < < < < < < < < < < < < < < /                                                                       
                / + ( ( { ? + < < < < < < < < < _ ? ] ] ] ] ] - + < < < < < < f                                                                       
                i f ( ( ( ( ( 1 { } [ [ [ } 1 ( ( ( ( ( ( ( ( ( ( ) } _ < < < /                                                                       
                  ] X ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( } < < |                                                                       
              : ( f ) v u ( ( \ j u c c u x J d m C J | ( ( ( ( ( ( ( ( ( [ r \                                                                       
          ` \ J t ] ~ { ( ( n X r , '         ` < ( j c C z r ( ( ( ( ( | n z }                                                                       
          u X z r } / u c f \ 1                           l ] j z J Y Y n ) } } v _                                                                   
          + r ( ) ( - I                                         I n c \ + < < ] L Z u ^                                                               
                                                                    ' i ] } | ( - x O w                                                               
                                                                              ; 1 \ z J                                                               
---------------------- end ------------------------

Although the above is a character image output, and the result is also a price comparison image, it should be noted that if the background of the image is not white and the protagonist is not so prominent, the output result through the above method may not be very friendly. Of course, the solution is to identify the background, identify the subject and convert the subject elements (this process will be introduced later)

Next, we use open source projects https://github.com/liuyueyi/quick-media To quickly realize the output of character graph

Verify the effect with a transformation diagram of the snow queen

String file = "http://5b0988e595225.cdn.sohucs.com/images/20200410/76499041d3b144b58d6ed83f307df8a3.jpeg";
BufferedImage res = ImgPixelWrapper.build().setSourceImg(file).setBlockSize(4).setPixelType(PixelStyleEnum.CHAR_BLACK).build().asBufferedImg();

A gray contact information

It is better to believe in a book than not to have a book. The above contents are purely the words of one family. Due to limited personal ability, it is inevitable that there are omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct and appreciate it

Tags: Java

Posted on Mon, 06 Dec 2021 16:45:55 -0500 by php-n00b