Write a program to modify a PPM in python image in two ways

Write a program to modify a PPM in python image in two ways:

Negate the colors

Apply a high contrast

To do this, the flow of your program should be as follows:

Prompt the user to enter the input PPM file name, the output PPM file name, and the command \"negate\" or \"high contrast\" to determine what modification to apply the input PPM image.

Apply the modification. Your solution must contain the functions below to perform the image modification. These functions will help you get started! You are encouraged to define more than the ones listed as you wish.

process_header(infile, outfile): Writes the first three lines of the input image to the output image (no need to modify these lines)

process_body(infile, outfile, modification): Walks through each line in the body of the input image, modifies the RGB values according to modification, and writes out the modified output image. For the modification, process_body() calls either negate() or high_contrast()for each RGB value in the line:

negate(): Accepts a single integer RGB value. To negate the value, subtract 255 and take the absolute value of the result. Return the result.

high_contrast(): Accepts a single integer RGB value. To apply high contrast, if the value is greater than 127, set it to 255. Return the result.

Write the modified image out to the user specified file.

Negate line example: 1 2 3 200 100 150 negated is 254 253 252 55 155 105

High contrast line example: 1 2 3 200 100 150 in high contrast is 0 0 0 255 255 255

Note: Other than strip(), do not use any other string methods in your code.

Note: I recommend you add code to prompt the user last. You can debug faster if you hard code the file names and the command, rather than typing them in each time as you implement your code to perform the image modifications.

Example Runs

Here are two example runs of the program:

Solution

class PPM(object):
def __init__(self, infile, outfile):
self.infile=infile
self.outfile=outfile

#Read in data of image
data= open(self.infile,\"r\")
datain=data.read()
splits=datain.split(None, 4)

#Header info and pixel info
self.type=splits[0]
self.columns=int(splits[1])
self.rows=int(splits[2])
self.colour=int(splits[3])

#(Return a new array of bytes)
self.pixels=bytearray(splits[4])

def gry_scle(self):
for row in range(self.rows):
for column in range(self.columns):
start = row * self.columns * 3 + column * 3
end = start + 3
r, g, b = self.pixels[start:end]
brightness = int(round( (r + g + b) / 3.0 ))
self.pixels[start:end] = brightness, brightness, brightness

def wrtToaFile(self):
dataout= open(self.outfile, \"wb\")
#Use format to convert back to strings to concatenate them and Those {} in the write function get\'s replaced by the arguments of the format function.
dataout.write(\'{}\ {} {}\ {}\ {}\'.format(self.type,
self.columns, self.rows,
self.colour,
self.pixels))

smple = PPM(\"cake.ppm\", \"Replica.ppm\")
smple.gry_scle()
smple.wrtToaFile()

Write a program to modify a PPM in python image in two ways: Negate the colors Apply a high contrast To do this, the flow of your program should be as follows:
Write a program to modify a PPM in python image in two ways: Negate the colors Apply a high contrast To do this, the flow of your program should be as follows:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site