cleaner test logic

This commit is contained in:
Remy Moll 2023-11-08 22:26:01 +01:00
parent 6846a17243
commit 2fb48c494b
3 changed files with 62 additions and 21 deletions

View File

@ -1,6 +1,5 @@
from PIL import Image, ImageOps from PIL import Image, ImageOps
import io import io
import os
black_base = [0, 0, 0] black_base = [0, 0, 0]
white_base = [255, 255, 255] white_base = [255, 255, 255]
@ -33,16 +32,16 @@ class ImageShrink:
"""Shrinks a given image (bytearray) to a given resolution (width, height)""" """Shrinks a given image (bytearray) to a given resolution (width, height)"""
resolution = (480, 800) resolution = (480, 800)
def __init__(self) -> None: def __init__(self, dither=True, colors=7) -> None:
pass self.dither = dither
self.colors = colors
def convert(self, image: bytearray) -> Image: def convert(self, image: bytearray) -> Image:
# load image from bytearray # load image from bytearray
image = Image.open(io.BytesIO(image)) image = Image.open(io.BytesIO(image))
image = self.shrink(image) image = self.shrink(image)
image = self.convert_to_reduced_colors(image) image = self.convert_to_reduced_colors(image)
if os.uname().machine == "x86_64":
image.save("test.png")
return image return image
@ -62,5 +61,8 @@ class ImageShrink:
if image.mode != "RGB": if image.mode != "RGB":
print("Converting image to RGB") print("Converting image to RGB")
image = image.convert("RGB") image = image.convert("RGB")
new_image = image.quantize(colors = len(palette), palette=ref_image, dither=True) if self.colors == -1:
print("Skipping color reduction")
return image
new_image = image.quantize(colors = len(palette), palette=ref_image, dither=self.dither)
return new_image return new_image

View File

@ -1,5 +1,37 @@
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
try:
import epaper import epaper
except ModuleNotFoundError:
# generate dummy class for testing
class epaper:
class epaper:
def __init__(self, name):
pass
class EPD:
width = 480
height = 800
WHITE = 0
BLUE = 0
ORANGE = 0
BLACK = 0
GREEN = 0
RED = 0
YELLOW = 0
def __init__(self):
pass
def init(self):
pass
def Clear(self):
pass
def display(self, image):
print("Dummy output")
def sleep(self):
pass
def getbuffer(self, image:Image):
image.save("test.png")
return image
# BLACK = 0x000000 # 0000 BGR # BLACK = 0x000000 # 0000 BGR
# WHITE = 0xffffff # 0001 # WHITE = 0xffffff # 0001
# GREEN = 0x00ff00 # 0010 # GREEN = 0x00ff00 # 0010

27
main.py
View File

@ -1,20 +1,27 @@
import sys
from image_convert import ImageShrink from image_convert import ImageShrink
from image_get import ImageGetter from image_get import ImageGetter
try:
from image_show import ImageShow from image_show import ImageShow
show_image = True
except ImportError: if len(sys.argv) == 2 and sys.argv[1] == "test":
print("ImageShow not found. Image will not be displayed.") print("Running test")
show_image = False show = ImageShow()
show.draw_sample_image()
sys.exit()
shrink_kwargs = {}
if "nodither" in sys.argv:
print("Disabling dithering")
shrink_kwargs["dither"] = False
if "noreduce" in sys.argv:
print("Disabling color reduction")
shrink_kwargs["colors"] = -1
get = ImageGetter() get = ImageGetter()
convert = ImageShrink() convert = ImageShrink(**shrink_kwargs)
if show_image:
show = ImageShow() show = ImageShow()
image = get.get_random_image() image = get.get_random_image()
image = convert.convert(image) image = convert.convert(image)
if show_image:
show.show_image(image) show.show_image(image)
else:
print("Would have shown image now.")