This article describes an iOS APP that converts images into ASCII characters and is written in Swift.
For example, we use the famous Rena photo as the original image.
After the app has been converted into a character drawing, we print it out, the effect is as follows:
Zoom in on her face to see that the images are made of strings!
Project Address: Https://github.com/ijoshsmith/swift-ascii-art
The principle of character painting
Each pixel in the image corresponds to an ASCII value, which is shown here as a character .
Each pixel in the image is converted to an intermediate value, as follows:
Let's take a step-by-step analysis.
- First, we convert a pixel color to a grayscale color.
The intensity of the grayscale color (for example, brightness) is a value between 0 and 1, 0 for Black, and 1 for white.
- Next, convert the intensity value of each color to ASCII characters. This is the most interesting part of me,
The Asciipalette class implements this part of the algorithm, and we wait for a bit of analysis.
- Finally, the ASCII word of each line is conformed and combined, then the multi-line string is merged and formed: Character metalized
Algorithm implementation
The asciiartist class implements the conversion of an image to a character picture, referencing 31 to 33 lines.
asciiartist objects rely on Pixel and asciipalette classes, we analyze them.
Convert pixel to Grayscale color
The Pixel class represents a pixel color in the image, with a color of 4 bytes,
Each byte represents a channel (red, green, blue, and Alpha).
The asciiartist class determines the intensity value of its color through the Pixel class, converting the pixel strength value to a percentage,
Let's see how it's achieved:
The above code of 47 to 49 lines, is the standard pixel grayscale conversion method, if you care about this part of the principle, you can refer to grayscale
Convert grayscale colors to ASCII characters
The symbolfromintensity method in the Asciiartist class can convert a grayscale color to an ASCII character,
We find the corresponding character in the character array of the Asciipalette object based on the converted intensity value.
Character arrays are arranged from dark to bright based on the color intensity values.
Here is the implementation of our approach:
Symbol_from_intensity
So the question is: which ASCII characters should we put in the array, in what order?
Poorly designed character arrays will output an unclear ASCII character metalized:
A good array of characters produces a very clear effect:
My goal is to design a good character array so that the computer can draw a clear word metalized.
The Asciipalette class renders characters as images, drawing black text on a white background.
We sort the character array by calculating the ratio of the white pixels in the ASCII character image, and the more white pixels, the greater the strength value of the character; the empty characters (") represents the highest strength value because it contains only white pixels, so it is sorted at the end of the character array.
Asciipalette class initialization requires a Uifont object as a parameter, because different fonts are selected and the characters are rendered differently, which affects how many white pixels surround the character.
Source Address: Https://github.com/ijoshsmith/swift-ascii-art
This article translated from: Creating ASCII art in functional Swift
The source of this article: http://aswifter.com/2015/06/26/creating-ascii-art-in-functional-swift/
Reprint please indicate the source of this article at the beginning.
Wen/* Noodle (Jane book author)
Original link: http://www.jianshu.com/p/19d609c79ee5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Swift realizes the function of picture to character drawing