In this tutorial you will be working with 2D arrays to do some image manipulation.
Download the BlueJ project ASCIIArt.zip. Inside you will find a class that contains the method readImage(String url)
. This method takes the URL for any image on the web and converts it into a 2D array of integers. Each entry in the 2D array represents the brightness of one pixel. The values range from zero (black) to 5 (white). Entries are arranged in width by height order so an array like:
{{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 5, 0, 0, 0, 0, 0, 0, 0}
{0, 5, 5, 5, 0, 0, 0, 0, 0},
{0, 5, 3, 3, 5, 5, 0, 0, 0},
{0, 5, 3, 3, 3, 3, 5, 5, 0},
{0, 5, 3, 3, 5, 5, 0, 0, 0},
{0, 5, 5, 5, 0, 0, 0, 0, 0},
{0, 5, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0}}
represents a grey triangle (the ’3′ pixels) with a white border (the bright ’5′ pixels) pointing to the right on a black background (the ’0′ pixels).
Your tasks:
- Write a method to draw an image on the console using ASCII art. That is, each pixel of the image is drawn as a character according to the following table:
Brightness Character 0 'X'
1 '8'
2 'o'
3 ':'
4 '.'
5 ' '
So the triangle above would be rendered as:
XXXXXXXXX X XXXXXXX X XXXXX X :: XXX X :::: X X :: XXX X XXXXX X XXXXXXX XXXXXXXXX
- Write a method to invert an image, so that bright pixels are converted to dark and vice versa. It should input an image and return an inverted copy.
- Write a method to flip an image horizontally. It should input an image and return a flipped copy.
- Challenge: Write a method to rotate an image clockwise. It should input an image and return a rotated copy.
Try these sample images
- http://sfpl.org/images/graphics/chicklets/google-small.png
- http://a0.twimg.com/profile_images/2258868342/Pokemon_Red_Sprite_normal.jpg
No Comments so far ↓
Comments are closed.