Question: PLEASE READ IT CAREFULLY AND THEN POST SOLUTION WHICH SOLVES EACH AND EVERY CONDITION. PYTHON Whew! That may sound like a lot of work, but
PLEASE READ IT CAREFULLY AND THEN POST SOLUTION WHICH SOLVES EACH AND EVERY CONDITION.
PYTHON
Whew! That may sound like a lot of work, but it's really pretty simple. To make it easy to code up we've broken the program into three phases. In the first you are reading the whole image and writing it to a file without making any changes to the image in the process. Believe it or not, at this point you've written most of the program! In phase II we add some effects, and in phase III we add a menu for the user.
Phase I:
The user will specify the name of the image file. The file will be a text file in PPM format as described in the discussion above. Remember an image file has three dimensions: (i) rows, (ii) columns, and (iii) channel, where channel is either the red, green, or blue channel. So to store this, Use a single 3-dimenional list .
The user will specify an output filename. The purpose of the program is to make an exact copy of the input file as the output file.
Your output file can actually be formatted as you like. Whitespace includes newline characters, so you can put them in where you wish. The format allows for it.
Example interaction with the user:
Portable Pixmap (PPM) Image Editor!
Enter name of image file: feep.ppm
Enter name of output file: out.ppm
out.ppm created.
And the output file created would be in a file called out.ppm and would be identical to feep.ppm. Your program is NOT responsible for displaying the image in the file, just to manipulate the pixels and create an output file in the proper PPM format.
Test this with small files and large files. You can check to see if they are identical by loading them both into Notepad and comparing number by number.
Example ppm files can be downloaded here: ZIP Archive (1.2 MB) containing the following images:
cake.ppm - A picture of a slice of cake on a plate
squares.ppm - Some boxes
blocks.ppm - Some solid blocks of color
tinypix.ppm - The example image from above
Phase II:
Write a function called negate_red. It will change just the RED color numbers into their "negative". That is, if the red number is low, it should become high and vice versa. The maximum color depth number is useful here. If the red were 0, it would become 255; if it were 255 it would become 0. If the red were 100, it would become 155. It should make changes to the list as described above. When you have this function written, insert it into Phase I so that every pixel of the picture has had its red color negated in the output file.
View this picture - does it look as you expected?
Write a function called "flip_horizontal" which will flip the picture horizontally. That is, the pixel that is on the far right end of the row ends up on the far left of the row and vice versa (remember to preserve RGB order!).
Write a function called grey_scale which will change the picture into a grey scale image. This is done by averaging the values of all three color numbers for a pixel, the red, green and blue, and then replacing them all by that average. So if the three colors were 25, 75 and 250, the average would be 116, and all three numbers would become 116.
Write a function called "flatten_red" which will set the red value to zero. Write similar functions for the other color numbers. I
n summary, you must implement the following functionality:
negate_red - Its job is to negate the red number of each pixel.
negate_green, as above but change the green
negate_blue, as above but change the blue
flip_horizontal that flips each row horizontally
grey_scale sets each pixel value to the average of the three
flatten_red sets the red value to zero
flatten_green sets the green value to zero
flatten_blue sets the blue value to zero
Phase III:
The last part of the problem is to add a menu so that the user can decide which of these effects will be applied to an image.
You should apply the desired effects to the image one at a time.
Testing:
Your input routine should avoid reading from a failed input stream. What if the file is not as large as the row and column numbers indicate it will be? Does your code handle that gracefully (without falling into infinite loops, for example)? All your manipulations should NOT cause a color number to be less than 0 nor larger than the maximum color depth specified in the file.
Bonus:
Implement up to 2 of these other functions.
"horizontal_blur" which will take the values of the red numbers of three adjacent pixels and replace them with their average - note that this is different from greyscale! it also does the same with the greens and the blues of 3 adjacent pixels. Pixels on the edges will have to be handled specially.
"extreme_contrast" which will change each color number to either the highest color number possible or to 0. This change is based on whether it is greater than the midpoint of the color range, or less. If it is greater than half of the color depth, replace it with the colordepth. If it is less, replace it with zero.
"random_noise" adds a random number to each color number or subtracts a random number. It would have a parameter which would represent the size of the random number range; i.e. a value of 10 would add or subtract numbers in the range of 0 to 9, a value of 50 would add or subtract numbers in the range 0 to 49. Another random number would decide whether it was an addition or a subtraction. Remember that the values in the buffer should not exceed the colordepth nor get less than 0. You can use the extreme values instead.
Adapted from http://nifty.stanford.edu/2012/guerin-imageeditor/image_editor.html
Sample run:
Portable Pixmap (PPM) Image Editor
Enter name of image file: cake.ppm
Enter name of output file: outcake.ppm
Here are your choices:
[0] exit
[1] convert to greyscale
[2] flip horizontally
[3] negative of red
[4] negative of green
[5] negative of blue
[6] just the reds
[7] just the greens
[8] just the blues
Enter choice: 1
outcake.ppm created with effect, convert to greyscale.
Do you want to do more operations (y or n): y
Do you want to change input file (y or n): n
Do you want to change output file (y or n): y
Enter name of output file: outcake2.ppm
Here are your choices:
[0] exit
[1] convert to greyscale
[2] flip horizontally
[3] negative of red
[4] negative of green
[5] negative of blue
[6] just the reds
[7] just the greens
[8] just the blues
Enter choice: 2
Outcake2.ppm created with effect, flip horizontally.
Do you want to do more operations (y or n): n
Bye bye.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
