-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_utils.py
82 lines (66 loc) · 3.13 KB
/
image_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import random
# A variable defining "black" as a computer color.
# This is used in load when making an empty list-of-lists for loading data into.
# You probably don't need this, but what do I know?
BLACK = (0, 0, 0)
def read_ppm(filename):
"""
Reads an image saved in ppm format (specifically, "plain" ppm format P3)
:param filename: the name of the ppm file to load
:return: a list-of-lists representing the image. The list-of-lists will be width x height, and each element will be
a 3-tuple representing the color of the image as a red / green / blue value.
"""
f = open(filename, "r")
file = f.read().split()
f.close()
if file[0] != "P3":
print("[ERROR] First line is not P3. File is probably not formatted correctly!")
width = int(file[1])
height = int(file[2])
if file[3] != "255":
print("[ERROR] ppm image uses a different color representation (not 255) than this code is designed for")
image_dat = file[4:]
image = []
for columnNum in range(width):
row = [BLACK] * height
image.append(row)
# a width x height list of lists (that is a width long list, each element of which is a height long lists) of colors
i = 0
for row_num in range(height):
for col_num in range(width):
image[col_num][row_num] = (int(image_dat[i]), int(image_dat[i + 1]), int(image_dat[i + 2]))
i += 3
return image
def get_width_height(image):
""" Returns a tuple (width, height) indicating the width and height of the image."""
width = len(image)
height = len(image[0])
return width, height
def random_color():
"""Returns a random color as a 3-tuple representing the red, green, and blue values.
Each color component will be between 0 and 255"""
return (random.randrange(256), random.randrange(256), random.randrange(256))
def save_ppm(filename, image):
"""
Writes an image in ppm format (specifically "plain" ppm format P3)
:param filename: The filename to save to
:param image: The image data, should be a width x height list-of-lists with each element
being a 3-tuple of red,green,blue values each of which should be between 0 and 255.
:return: Nothing.
"""
# Provided, open the file and write the first few lines, as these are required
# Note, the syntax for writing to a file is a little different than print.
out_file = open(filename, "w")
print("P3", file=out_file)
width, height = get_width_height(image)
print(width, height, file=out_file)
print(255, file=out_file)
# From here, you need to write the image data.
# To write one pixel you simply need to output the red, green, and blue components
# in order with whitespace separating them. I would also recommend putting a newline after each pixed.
# you need to output the image row-by-row, so all of the first row, then all of the second etc.
for y in range(height):
for x in range(width):
print(str(image[x][y][0]), str(image[x][y][1]), str(image[x][y][2]), file=out_file)
# Provided - gotta close the file, skipping this is bad ok?
out_file.close()