Go is fun, 5.20 make a diagram with go / let the words dance

Today, I'd like to introduce an interesting codeThe picture is generated into a pattern of text. Have you seen the GIF of dancing sister composed...

Today, I'd like to introduce an interesting code
The picture is generated into a pattern of text. Have you seen the GIF of dancing sister composed of those words in station B? With this code you can do it yourself.

GIF, I'm too lazy to do it. As a back-end programmer, I'm not very patient with frame by frame screenshots. You can see the effect of pasting pattern text in VScode first

Code directly. The description is in the code

/*Define a function first Parameters: imgPath: Picture path size: The size after generating the text (this is not the real size, 1 represents 1 pixel, 1 pixel will be replaced by 1 character, so it is the number of characters, and the height is automatically converted, so the size here refers to how many pixels the "width" is compressed into) txts: List of characters to process pixels into rowend: Newline characters (because windows and linux are different) output: Generate text file save path */ func img2txt(imgPath string, size uint, txts []string, rowend string, output string) { //Get picture file file, err := os.Open(imgPath) if err != nil { fmt.Println(err.Error()) return } defer file.Close() //Get picture object with picture file img, err := png.Decode(file) if err != nil { fmt.Println(err.Error()) return } //Use to set the width to size, and then convert to an equal proportion of the height var width = size var height = (size * uint(img.Bounds().Dy())) / (uint(img.Bounds().Dx())) height = height * 6 / 10 //Here 6 / 10 is the width to height ratio of approximate characters newimg := resize.Resize(width, height, img, resize.Lanczos3) //resize the picture according to the height and width, and get the pixel value of the new picture dx := newimg.Bounds().Dx() dy := newimg.Bounds().Dy() //Create a byte buffer, which will be used to save characters later textBuffer := bytes.Buffer{} //Traversing pixels in each row and column of a picture for y := 0; y < dy; y++ { for x := 0; x < dx; x++ { colorRgb := newimg.At(x, y) r, g, b, _ := colorRgb.RGBA() //Get the value of three primary colors and calculate an average avg := uint8((r + g + b) / 3 >> 8) //As many characters as there are to replace, divide 256 into several equal parts, then calculate which character the average value of this pixel will be closer to, and finally, add this character to the character buffer num := avg / uint8(256/len(txts)) textBuffer.WriteString(txts[num]) fmt.Print(txts[num]) //Print out } textBuffer.WriteString(rowend) //Line end, line break fmt.Print(rowend) } //Write the data of the character buffer to the text file. End. f, err := os.Create(output + ".txt") if err != nil { fmt.Println(err.Error()) return } defer f.Close() f.WriteString(textBuffer.String()) }

Then, in the main function

func main() { img2txt("Your picture.png", 200, []string{"@", "#"," * ","% "," + ",", ".", ""}, "\ n", ". / saved text. txt") }

Get it!!!

go run main.go

Try to open the file you just saved to see the effect

Like to surprise your friends, you can use their photos to make a text picture, dance little sister's text, code with a logo in the notes

Guess who she is?

My favorite classmates can add my public number and discover the fun of learning programming together.

10 November 2019, 11:26 | Views: 1045

Add new comment

For adding a comment, please log in
or create account

0 comments