-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.py
30 lines (25 loc) · 1 KB
/
image.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
class Image:
def __init__(self, width, height):
self.width = width
self.height = height
self.pixels = [[None for _ in range(width)] for _ in range(height)]
def set_pixel(self, x, y, col):
self.pixels[y][x] = col
def write_ppm(self, img_fileobj):
Image.write_ppm_header(img_fileobj, height=self.height, width=self.width)
self.write_ppm_raw(img_fileobj)
@staticmethod
def write_ppm_header(img_fileobj, height=None, width=None):
"""Writes only the header of a PPM file"""
img_fileobj.write("P3 {} {}\n255\n".format(width, height))
def write_ppm_raw(self, img_fileobj):
def to_byte(c):
return round(max(min(c * 255, 255), 0))
for row in self.pixels:
for color in row:
img_fileobj.write(
"{} {} {} ".format(
to_byte(color.x), to_byte(color.y), to_byte(color.z)
)
)
img_fileobj.write("\n")