The rightmost and bottommost blocks won’t actually have 8x4 pixels in them, because you’ll run out of space. For instance, the block that started at row 28 and column 32 in the above image is 35 rows down and 8 columns over.īe careful not to run out of bounds when taking pixels in a block. This means that if we use a block that’s 8 tall and 4 wide, we can fit 59 blocks from top to bottom and 117 blocks from left to right. So, for instance, the above image is 466x466 pixels. When you do this, you should take all non-overlapping blocks in the image that you can. If we take a block that’s one one of the bricks, it’s somewhere in between, so the average is about middle gray, which ends up getting a lowercase c, which uses a medium amount of ink. If we take a dark region in the tree, the average is quite low, and it gets an uppercase B, which is one of the characters that uses the most ink. If we take a block in a bright region, the average grayscale is very high, so it ends up getting a single quote, which has very little ink. To see an example, let’s consider taking a block that’s 8 pixels tall and 4 pixels wide in different parts of an image of an Ursinus flag. In particular, we can average all of the grayscale values in a rectangular block that’s twice as tall as it is wide, and print out a single character for this block. To address both of these problems, we can output a single character per block of pixels. However, not only would this print out way too many characters, but the characters are only printed out half as wide as they are tall, so this would end up printing out an image that’s stretched out by a factor of 2 vertically (the “aspect ratio” would be wrong).
Background: Rectangular AveragingĪt this point, we could simply print out a row of text for each row in the grayscale image, and print out the character at the index of GRAYSCALE_VALUES with the closest grayscale value. This indicates that a B is a darker character, with a grayscale value of only about 0.08. For instance, let’s run the following code, using the methods provided in the assignment: We usually treat the outer array index as a row (y coordinate) and the inner array index as a column (x coordinate). In this assignment, we will be dealing with grayscale images, in which each pixel goes from black (0) to white (1).
Background: Grayscale Images and TextĪs we discussed in our 2D arrays module, a 2D array is a natural data structure for image data, since an image is a two dimensional object with both an x and y coordinate for each pixel. Please download the skeleton code for this assignment. In this assignment, we will restrict ourselves to characters in the ASCII subset of UTF-8, including lowercase letters, uppercase letters, and some special characters hence the name “ASCII Art.” An example is given below with the UC logo. Students will write a program to automatically generate “ASCII Art”, which is text that approximates a given image. In this lab, you will work with with 2D arrays in Java with a concrete, visual application. If (and only if) you are using GitHub to submit, you can clone this assignment from GitHub Classroom at.
The ASCII ART image is not wonderfully clear but at least one can catch the drift. But nevertheless, it was an interesting exercise. It can get quite scientific and will involve far more math calcs than the simple approach that I have followed here. The image can be reduced if one for eg take blocks of let's say 5 x 5 pixels and work out what character would best represent the 5 x 5 pixels. I have done this on a 7" tablet so that the display can fit the ASCII image. Thus, every pixel in the bitmap is represented by an ASCII character in the ASCII ART image. This is a very oversimplified way of doing it - have just used the average of each pixel's RGB values to decide upon the ASCII character to be used for each pixel. Note that the project uses inline Java code and you will need a B4A version that supports inline Java code to run the project (with the JavaObject library enabled). Have used a text size of 2 for the label and have set the typeface to MONOSPACE. The ASCII ART image is actually a label with the label's text set to the ASCII string that was generated from the bitmap (image on the left). The project uses 70 ASCII characters to generate an ASCII ART image from the bitmap.