How to Read Ppm File in Python

Program eight: Image Processing

For this program, you will be modifying image files. The image format nosotros are going to utilize is called PPM, which stands for Portable Pixmap format. Don't worry if you're non familiar with PPM images, they are non used much these days. Their main advantage for this project is that they are stored equally plain text files, they are easy to like shooting fish in a barrel to read from and write to files, and they can easily exist stored in Python lists. Formats like GIF, JPEG, or PNG are not so easy to piece of work with, unfortunately. We draw the way PPM images work afterward in this document.

The chief disadvantage to PPM images is that they take upwardly a lot of storage space on your computer equally compared to formats like JPEG or PNG. About modernistic image formats use some kind of compression to make their size reasonable while preserving the prototype appearance. PPM images are not compressed at all.

Mac computers come up with the built-in ability to open and read PPM images (you can double-click on them and they'll open in Preview). Windows computers, unfortunately, practise not accept this ability congenital-in, so you will want to download a program to view PPM files. IrfanView is a small plan you can use to do this (click here to download IrfanView for Windows). IrfanView will also allow yous to convert your ain images to PPM so you can practice with your ain pictures (keep in mind that you may need to make them very small or the resulting PPM will be quite large!).

PPM Image Format

The various colors in an image are represented in a PPM file as RGB triples, where RGB stands for red-green-blue. This is a standard manner of representing colors.

RGB Triples

An RGB triple is three numbers, usually betwixt 0 and 255, that when taken together, represent a color. The iii numbers specify different intensities of red, greenish, and bluish, that are then mixed together to create 1 color. For instance, the color reddish is represented by (255, 0, 0). The 255 as the first number ways "all the red," and the two zeroes mean "no green and no blue." That color red looks like this: . The RGB color (0, 255, 0) would be all green, like this: , and (0, 0, 255) would be all blue: . Black is (0, 0, 0) and white is (255, 255, 255).

By varying the numbers, you can brand almost any color. For case, yellow is (255, 255, 0), purple is (128, 0, 255), and a medium gray is (128, 128, 128). Try playing around with RGB values at this link and see what colors you tin can brand.

Inside a PPM file

The PPM image format encodes an image by simply listing, in order, the color of every pixel in the image, one by one, row by row, height to bottom. Remember that a pixel is an individual dot of low-cal on your estimator's screen.

The PPM image format is encoded in human-readable plain text. A PPM image has two main parts: the header followed past the body.

PPM Header

The PPM header is always the first 3 lines in the file. Here is an example:

PPM images accept a few different encoding conventions; the outset line of the file specifies the specific type of encoding that the file uses. For this projection, we volition always use the "P3" specification, so the starting time line of our files will ever be P3.

The second line of the header always contains exactly two numbers. These are the dimensions of the epitome in pixels: the first number is the width (number of columns), and the second is the height (number of rows). In the example above, nosotros have a 4 pixel by iv pixel paradigm. Remember that a pixel is an private point of light on your screen, then a 4-by-4 epitome volition appear incredibly small --- probably too small to actually examine closely.

The 3rd line of the header always contains exactly ane number. This number indicates the maximum value for each red, light-green, and blue (RGB) element in the picture. Nosotros will always use a max value of 255.

PPM Body

Below the header is the trunk of the PPM file. Each row of the file specifies a row of the image every bit RGB triples. For example, the torso content of our 4x4 image might exist:

          255 0   0       0   255 0      0   0   255    255 255 255 0   0   0       255 0   0      0   255 0      0   0   255 255 0   255     0   0   0      255 0   0      0   255 0 0   255 255     255 0   255    0   0   0      255 0   0        

With these values, the pixel in the first cavalcade and first row has a RGB value of (255, 0, 0), which is cerise. The pixel in the last cavalcade and the first row has a RGB value of (255, 255, 255), which is white. The spacing of the numbers on each line doesn't affair, as long as there is at least one space separating each number.

A blown-up image containing these values would look like this:

You tin can download this PPM image here and open it in a text editor (IDLE, Notepad, or TextEdit) to take a look at it.

A larger example PPM image

Download this PPM image of the Rhodes College Byran Campus Life Middle: bclc.ppm. Once more, you can open the paradigm with software such as IrfanView to view it as an epitome. You can as well open it as a text file (directly in IDLE, for instance, or yous tin can open information technology in Notepad on Windows or TextEdit on Mac) to view it equally patently text (useful for debugging your code!).

How your program will work

Your programme will prompt the user for the proper noun of a PPM image file to read from. And so your program will the user for what modifications they would similar to make to the file. The possible modifications are:

  1. Capsize red
  2. Invert bluish
  3. Invert green
  4. Invert all
  5. Remove cerise
  6. Remove blue
  7. Remove light-green
  8. Flip horizontally
  9. Greyness scale

Your programme should output (write) a PPM file with the modifications fabricated to the input PPM file. The output file name should include your name equally well as the name of the modification that you did to the file.

How to get started

  1. Begin by downloading the starter code here. Read it over and familiarize yourself with information technology. The program will come together past writing a series of functions. First, write the function make_output_filename. Exist sure to add your name to the file name. Test it in the Python beat out. Hither are some examples:

                  >>> make_output_filename("bclc.ppm", two) 'bclc_kirlin_phillip_invert_blue.ppm'  >>> make_output_filename("bclc.ppm", 9) 'bclc_kirlin_phillip_grey_scale.ppm'            
  2. Write the functions invert, flatten, average3, and contrary. Read the comments higher up each office in the starter code to understand how these functions should piece of work. Test these functions from the Python shell, by calling them manually with different input lists. Hither are some examples:

                  >>> invert([1, 2, 3, 200, 100, 150]) [254, 253, 252, 55, 155, 105]  >>> flatten([ane, ii, 3, 200, 100, 150]) [0, 0, 0, 0, 0, 0]  >>> opposite([2, 1, 5, 4, iii, 6]) [6, 3, 4, 5, ane, ii]  >>> average3([one, 200], [two, 100], [3, 150]) [two, 150]            
  3. Write the role ppm_to_lists. This function takes a line of text from the torso of a PPM image and converts it into three lists, i each for the red, green, and blue values. Follow the guide in the code, making sure to end your role by returning all iii lists at once (endeavor: return red, green, blue as the last line of code).

    Test from the Python shell:

                  >>> ppm_to_lists("ane ii iii 4 5 six 7 8 9") ([1, 4, vii], [2, five, 8], [iii, six, 9])  >>> ppm_to_lists("20 l lxx twoscore 80 xc 0 100 50") ([twenty, 40, 0], [50, lxxx, 100], [seventy, 90, l])            
  4. Write the office lists_to_ppm. This function takes is the opposite of the previous function: information technology takes three lists of RGB values and converts them into a single line of text that will go into the trunk of the PPM file. Follow the guide in the code, making certain to adhere a newline grapheme to your cord before yous return it (if your string is called answer, attempt doing answer += "\n" right before yous say render answer.

    Test from the Python vanquish:

                  >>> lists_to_ppm([one, 4, vii], [2, 5, viii], [3, 6, nine]) '1 2 3 4 5 6 7 8 nine \northward'  >>> lists_to_ppm([20, 40, 0], [50, 80, 100], [70, ninety, 50]) '20 fifty 70 40 80 90 0 100 50 \due north'            
  5. Fill up in the gaps in the code in process_file. Look for YOUR CODE Hither. You tin can uncomment the call to main() at the stop when y'all start working on this. Y'all shouldn't have to modify any of the code in main except where it says YOUR Code Hither, with the exception of maybe uncommenting the print(lineout) statement to meet the output every bit its written to the file. Annotate information technology back out earlier you submit, however!

  6. Add together in an input validation loop to the main function so that if the user selects a modification outside of the 1-nine range, they are prompted to re-enter the number.

How do I know when my programme works?

Y'all should brainstorm by testing on the fourbyfour.ppm epitome from to a higher place. You can download information technology again hither. Try running this prototype through all ix modifications and look at the output files that are generated. Yous tin can too print out the lines as they are written to the file to verify they make sense.

When you're gear up to examination with larger files, you can effort these BCLC images:

Original

bclc.ppm:

Invert Ruddy

bclc_invert_red.ppm:

Invert Blue

bclc_invert_blue.ppm:

Invert Green

bclc_invert_green.ppm

Invert All

bclc_invert_all.ppm

Remove Red

bclc_remove_red.ppm:

Remove Blue

bclc_remove_blue.ppm:

Remove Green

bclc_remove_green.ppm

Flip Horizontally

bclc_flip_horizontally.ppm

Grey Scale

bclc_grey_scale.ppm

More images to exam on

barrett.ppm

rhodes-campus.ppm

lynx.ppm

ny.ppm

What to turn in

Through Moodle, turn in your code equally a file called images_yourLastName_yourFirstName.py.

Claiming problems

  • Try adding an selection to flip the epitome vertically. This is hard.

baineknines.blogspot.com

Source: https://www.cs.rhodes.edu/~kirlinp/courses/cs1/f19/projects/proj8/

0 Response to "How to Read Ppm File in Python"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel