Skip to content

Commit

Permalink
Merge branch 'drop_numpy'
Browse files Browse the repository at this point in the history
  • Loading branch information
Magi3r committed Nov 17, 2023
2 parents 84f5324 + 8437173 commit b4e9fe8
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/huecircle.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def circle_generator():
for y in range(14):
rho, phi = cart2pol(x / 28 - 0.5, y / 14 - 0.5)
image[y, x, :] = from_hsv((phi + i / 180) % 1.0, 1-rho, i / 90 if i < 91 else 1 - (i - 90) / 90)
yield image
yield image.tolist()


circle = circle_generator()
Expand Down
6 changes: 3 additions & 3 deletions examples/noisefill.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

def image_gen():
image = np.zeros((14, 28, 3))
yield image
yield image.tolist()
while True:
for y in range(28):
for j in range(3):
image[0, y, j] = int(random() * 255)
yield image
yield image.tolist()
image = np.roll(image, 1, 0)
image *= 0.85
yield image
yield image.tolist()


g = image_gen()
Expand Down
6 changes: 3 additions & 3 deletions examples/rgbfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

def image_gen():
image = np.zeros((14, 28, 3))
yield image
yield image.tolist()
while True:
for x in range(14):
for y in range(28):
for j in range(3):
image[x, y, j] = 255
yield image
yield image.tolist()
for y in range(28):
for x in range(14):
for j in range(3):
image[x, y, j] = 0
yield image
yield image.tolist()


g = image_gen()
Expand Down
4 changes: 2 additions & 2 deletions examples/rgbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

def image_gen():
image = np.zeros((14, 28, 3))
yield image
yield image.tolist()
while True:
for j in range(3):
for x in range(14):
for y in range(28):
image[x, y, j] = 255
yield image
yield image.tolist()
image[x, y, j] = 0


Expand Down
2 changes: 1 addition & 1 deletion examples/twopoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def callback(self):
fill=tuple(from_hsv(self.hue, 1, 1)))
output = self.img.copy()
output.thumbnail((28, 14))
return np.asarray(output)
return np.asarray(output).tolist()


if __name__ == '__main__':
Expand Down
84 changes: 74 additions & 10 deletions pyghthouse/data/canvas.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
from typing import Union, Iterable
import numpy as np


# import numpy as np
class PyghthouseCanvas:

VALID_IMAGE_TYPE = Union[np.ndarray, Iterable, int]
VALID_IMAGE_TYPE = Union[Iterable, int]
IMAGE_SHAPE = (14, 28, 3)

def __init__(self, initial_image=None):
self.image = np.zeros(self.IMAGE_SHAPE, dtype=np.ubyte)
self.image = self.init_image_array()
if initial_image is None:
self.image[:, :, 0] = 255
self.image[:][:][0] = 255
else:
self.set_image(initial_image)

def set_image(self, new_image: VALID_IMAGE_TYPE) -> np.array:
"""
Flattens the list. Works for nested lists too.
"""
def flatten_list(self,element):
if(type(element)==list):
result = []
for element in element:
result.extend(self.flatten_list(element))
return result
else:
return [element]


"""
Creates a nested list in the shape of IMAGE_SHAPE.
number_cb: function
A function that returns an integer (or to integer castable) element.
"""
def init_image_array(self,number_cb=lambda: 0):
list = []
for x in range(self.IMAGE_SHAPE[0]):
list_y = []
for y in range(self.IMAGE_SHAPE[1]):
list_rgb = []
for rgb in range(self.IMAGE_SHAPE[2]):
list_rgb.append(int(number_cb()))
list_y.append(list_rgb)
list.append(list_y)
return list

"""
Parses user input to image data.
new_image: int | list
If int provided, sets all values to this number. Must be between 0 and 255.
If list provided, it must contain exactly as many elements as the image needs values,
but it may be nested in any way.
"""
def new_image_to_image_array(self,new_image: VALID_IMAGE_TYPE) -> list:
if(type(new_image)==int):
if(0<=new_image<=255):
return self.init_image_array(lambda: new_image)
else:
raise ValueError("Color must be 0<=color<=255")

if(type(new_image)==list):
try:
flat = self.flatten_list(new_image)
result = self.init_image_array(lambda: int(flat.pop(0)))
if len(flat) != 0:
raise ValueError("Input too large")
return result
except IndexError:
raise ValueError("Wrong length of Iterable")
raise ValueError("Invalid input. Must be int or list")

"""
Sets the new image.
"""
def set_image(self, new_image: VALID_IMAGE_TYPE) -> list:
try:
self.image[:] = np.asarray(new_image).reshape(self.IMAGE_SHAPE)
self.image = self.new_image_to_image_array(new_image)
except ValueError as e:
raise ValueError(f"{e}. Most likely, your image does not have the correct dimensions.") from None

return self.image

def get_image_bytes(self):
return self.image.tobytes()
"""
Transforms the image to byte values.
"""
def get_image_bytes(self)->bytes:
b_array = bytearray()
for x in range(self.IMAGE_SHAPE[0]):
for y in range(self.IMAGE_SHAPE[1]):
for rgb in range(self.IMAGE_SHAPE[2]):
b_array.append(self.image[x][y][rgb])
return b_array
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=[
'numpy >= 1.23, < 2',
'websocket-client >= 1.6, < 2',
'msgpack >= 1.0, < 2',
],
Expand Down

0 comments on commit b4e9fe8

Please sign in to comment.