Golang Image Color Inversion, image grayscale, to character painting

Source: Internet
Author: User

See online a lot of through the character formation of the painting, feel very fun, so online search for the original is called character painting.
See Baidu Encyclopedia: https://baike.baidu.com/item/%E5%AD%97%E7%AC%A6%E7%94%BB/1347002

Found an article hand in hand teach you picture to ASCII code diagram this article is written in JavaScript, and tell the reason.

The approximate principle is that the picture session is processed first, then the RGB value of each pixel after the gray picture is converted to the corresponding character, then the character picture is then on the stroke.

The following is a picture conversion function implemented through the Go language

Imagetool Project Main.go Package main import ("bytes" "FMT" "Image" "Image/color" "Image/gif"
"Image/jpeg" "Image/png" "Io/ioutil" "OS" "strings")//the tool supports image color inversion, image ashing, image conversion to character painting. Author Iccboy 2017-9-2 func main () {args: = os. Args//Get all parameters entered by the user if args = = Nil | | Len (args)! = 4 | | ! (Args[1] = = "-r" | | args[1] = = "-G" | | args[1] = = "-T") {usage () return} FMT. Print ("... Convert ... ") option: = args[1] Source: = args[2] Target: = args[3] ff, _: = Ioutil. ReadFile (source) BBB: = bytes. Newbuffer (FF) m, _, _: = image. Decode (BBB) if option = = "-r" {Newrgba: = Fzimage (M) F, _: = OS. Create (target) defer f.close () encode (source, F, Newrgba)} else if option = = "G" {newgray: = Hdimage (M) F, _: = OS. 
    Create (target) defer f.close () encode (source, F, Newgray)} else {ascllimage (M, Target)} Fmt. PRINTLN ("Conversion complete...")
} Help tip information var usage = func () {fmt. PRINTLN ("Input error, please enter in the following format:") fmt. Println ("Use: Imagetool [OPTION] source_image [Output]") fmt. Println ("Options is Flow:") fmt. Println ("-R picture Color Flip") Fmt. Println ("-G image Grayscale") fmt. Println ("-T turns into text")}//Picture-encoded func encode (InputName string, file *os. File, Rgba *image. RGBA) {if strings. Hassuffix (inputname, "jpg") | | Strings. Hassuffix (InputName, "JPEG") {JPEG. Encode (file, Rgba, nil)} else if strings. Hassuffix (InputName, "png") {png. Encode (file, Rgba)} else if strings. Hassuffix (InputName, "gif") {gif. Encode (file, Rgba, nil)} else {fmt. Errorf ("Unsupported Picture Format")}}//Picture color reversal func fzimage (M image. Image) *image. RGBA {bounds: = M.bounds () dx: = bounds. Dx () Dy: = bounds. Dy () Newrgba: = image. Newrgba (bounds) for I: = 0; i < DX; i++ {for J: = 0; J < Dy; J + + {colorrgb: = m.at (i, J) R, G, B,A: = Colorrgb.rgba () r_uint8: = Uint8 (R >> 8) G_uint8: = Uint8 (g >> 8) B_u int8: = uint8 (b >> 8) A_uint8: = Uint8 (a >> 8) r_uint8 = 255-r_uint8 G_u int8 = 255-g_uint8 B_uint8 = 255-b_uint8 Newrgba.setrgba (i, J, color. Rgba{r_uint8, G_uint8, B_uint8, A_uint8})}} return Newrgba}//Picture grayed handle func hdimage (M image. Image) *image. RGBA {bounds: = M.bounds () dx: = bounds. Dx () Dy: = bounds. Dy () Newrgba: = image. Newrgba (bounds) for I: = 0; i < DX;
            i++ {for J: = 0; J < Dy; J + + {colorrgb: = m.at (I, J) _, G, _, A: = Colorrgb.rgba () G_uint8: = Uint8 (g >> 8) A_uint8: = Uint8 (a >> 8) Newrgba.setrgba (i, J, Colo R.rgba{g_uint8, G_uint8, G_uint8, A_uint8})}} return Newrgba}//Picture converted to character draw Func Ascllimage (M image. Image, target string) {bounds: = M.bounds () dx: = Bounds. Dx () Dy: = bounds.

    Dy () Arr: = []string{"M", "N", "H", "Q", "$", "O", "C", "?", "7", ">", "!", ":", "-", ";", "."} FileName: = Target Dstfile, err: = OS. Create (fileName) if err! = Nil {fmt. Println (Err. Error ()) return} defer Dstfile.close () for I: = 0; i < dy;
            i++ {for J: = 0; J < DX; J + + {colorrgb: = m.at (J, I) _, G, _, _: = Colorrgb.rgba () Avg: = Uint8 (g >> 8) num: = AVG/18 dstfile.writestring (Arr[num]) if j = = dx-1 {dstfile.writestring ("\ n")}}}

The converted Result






From the above effect can be seen that the basic function has been implemented, but there are two problems to be solved, one is the GIF image after the conversion of only the first image, the second is "character painting" is not a character painting, just a TXT file, the following article will optimize the program, the implementation of GIF character animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.