Encoding and Decoding PPM image
3 & 4 are parts of question 2
Code for a09q1:
I put the code for the entire project here(a09q2 and a09q1), made some minor changes to a09q1
a09q2.py
from a09q1 import PPM
#to perform deepcopies of objects
import copy
#function to encode image
def encode_img(file_name, file_name_secret, file_name_out):
original_img = PPM(file_name)
secret_img = PPM(file_name_secret)
#check if dimmensions match
if original_img.height != secret_img.height or original_img.width != secret_img.width:
raise Exception('Image dimmensions do not match, cannot encode')
#make a copy of the original_img
encoded_img = copy.deepcopy(original_img)
#zero out the copied image
encoded_img.zero_out()
for i,row in enumerate(secret_img.image):
for j,col in enumerate(row):
#check if pixel is white
if col == [255,255,255]:
#increment blue by 1 if white
encoded_img.image[i][j][2] += 1
#write to file
with open(file_name_out, 'w') as file:
file.write(encoded_img.magic_number+'\n')
file.write(str(encoded_img.width) + ' ' + str(encoded_img.height)+'\n')
file.write(str(encoded_img.maxval)+'\n')
for i,row in enumerate(encoded_img.image):
for j,col in enumerate(row):
for k, color in enumerate(col):
file.write(str(color))
if k != 2 or j != encoded_img.width-1:
file.write(' ')
file.write('\n')
#function to decode image
def decode_img(file_name, file_name_out):
encoded_img = PPM(file_name)
decoded_img = copy.deepcopy(encoded_img)
for i,row in enumerate(decoded_img.image):
for j,col in enumerate(row):
#check if pixel is white
if col[2] % 2 == 0:
decoded_img.image[i][j] = [0,0,0]
else:
decoded_img.image[i][j] = [255,255,255]
#write to file
with open(file_name_out, 'w') as file:
file.write(decoded_img.magic_number+'\n')
file.write(str(decoded_img.width) + ' ' + str(decoded_img.height)+'\n')
file.write(str(decoded_img.maxval)+'\n')
for i,row in enumerate(decoded_img.image):
for j,col in enumerate(row):
for k, color in enumerate(col):
file.write(str(color))
if k != 2 or j != decoded_img.width-1:
file.write(' ')
file.write('\n')
a09q1.py
class PPM:
def __init__(self, filename):
#the image is defined as a 3d list
#1st dim = row list
#2nd dim = rbg pixel list
#3rd dim = color value for that pixel
with open(filename, 'r') as file:
data = file.read().splitlines()
self.magic_number = data[0]
dimmensions = data[1].split(" ")
self.width = int(dimmensions[0])
self.height = int(dimmensions[1])
self.maxval = int(data[2])
#validata image height
if len(data)-3 != self.height:
raise Exception('Invalid Image {}, height mismatch'.format(filename))
self.image = []
for row in range(self.height):
rowForImg = []
rowdata = data[3 + row].split()
#validate image width
if len(rowdata)/3 != self.width:
raise Exception('Invalid Image {}, row mismatch'.format(filename))
for col in range(self.width):
pixel = []
for color in range(3):
#add color to pixe;
pixel.append( int(rowdata[col * 3 + color]) )
#validate max val
if pixel[color] > self.maxval:
raise Exception('Pixel value exceeds max value at row={} col={} filename={}'.format(row,col, filename))
#add pixel to row
rowForImg.append(pixel)
#add row to image
self.image.append(rowForImg)
def __eq__(self, other):
if not isinstance(other, PPM):
return False
if self.magic_number != other.magic_number:
return False
if self.width != other.width:
return False
if self.height != other.height:
return False
if self.maxval != other.maxval:
return False
if self.image != other.image:
return False
return True
def __repr__(self):
return "Dimmensions: ({0.width}, {0.height})\nImage: {0.image}".format(self)
def zero_out(self):
for row in self.image:
for col in row:
if col[2] % 2 != 0:
col[2] = col[2] - 1
Code Screenshots
a09q2.py
a09q1.py
Encoding and Decoding PPM image 3 & 4 are parts of question 2 Code for a09q1:...
AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...
the following python code edits BMP image file to negative but I need help with modifying this code so it turns it the same BMP image file into a black and white instead of a negative. heres python code I have so far [pasted below] ## # This program processes a digital image by creating a negative of a BMP image. # from io import SEEK_CUR from sys import exit def main() : filename = input("Please enter the file name:...
Your program should be capable of creating a ppm image of the flag of Benin • The new flag generated should use the proper width-to-height ratio as defined on Wikipedia for the flag. o For the colors, use pure red, and yellow, and a value of 170 for green. ▪ Pure color values are talked about in Lab 6. o The left green field should be 6/15ths of the width of the flag. Variables country_code should also include a new...
Please assist. I keep getting the error message in the middle screen when I run the program on the left. The image on the right is the section of images.py where it's indicating the issue with the .split is. How do I fix this? images.cy - /Users/carrietarpy Downloads/lab9-3/images.py 12.7.18) Python 2.7.18 Shell Python 2.7.15 (v2.7.18:542 102112, Apr 19 2820, 29:48:48) [GCC 4.7.1 Compatible Apple II w 6.3 Clong-FA3.0.57)] on der in Type "help", "copyright', 'credits' or "license()' for more inforyotion...
Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...
I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...
Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile The file 'BMPmain.c' contains a main() function which is already written for you and attached with this homework. When appropriate functions are provided, main() will create a .bmp image file. Your job is to write 'BMPfns.c' which contains the functions which main() uses to create .bmp image files. You must also write the header file 'BMPfns.h' to #include in BMPmain.c and which contains prototypes of the functions defined in BMPfns.c . Problem2: BMPcheckerboard.c, BMPfns.c, BMPfns.h,...
Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...
It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) { this->type = type; this->name = name; } //returns the type. string File::getType() { return type; } //returns the name of file. string File::getName() { return name; } File.h #ifndef __FILE_H__ #define...
Please design the function in version 3 of python 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents the portion of the original image that is specified by the other four parameters. The extracted portion of the image should consist of the pixels that fall in the intersection of the rows of pixels that begin with row rmin...