mapping onto correct color space implemented
This commit is contained in:
parent
6555319cee
commit
159b617e1e
@ -1,6 +1,33 @@
|
||||
from PIL import Image
|
||||
import io
|
||||
|
||||
black_base = [0, 0, 0]
|
||||
white_base = [255, 255, 255]
|
||||
green_base = [0, 255, 0]
|
||||
blue_base = [0, 0, 255]
|
||||
red_base = [255, 0, 0]
|
||||
yellow_base = [255, 255, 0]
|
||||
orange_base = [255, 128, 0]
|
||||
|
||||
|
||||
# leaves roughly 42 shades of each color
|
||||
def int_mult(list, factor):
|
||||
return [int(x * factor) for x in list]
|
||||
|
||||
whites = [int_mult(white_base, x/42) for x in range(1,43)]
|
||||
greens = [int_mult(green_base, x/42) for x in range(1,43)]
|
||||
blues = [int_mult(blue_base, x/42) for x in range(1,43)]
|
||||
reds = [int_mult(red_base, x/42) for x in range(1,43)]
|
||||
yellows = [int_mult(yellow_base, x/42) for x in range(1,43)]
|
||||
oranges = [int_mult(orange_base, x/42) for x in range(1,43)]
|
||||
|
||||
palette_unflat = [black_base, *whites, *greens, *blues, *reds, *yellows, *oranges]
|
||||
palette = [item for sublist in palette_unflat for item in sublist]
|
||||
# print(len(palette))
|
||||
|
||||
ref_image = Image.new("P", (1,1))
|
||||
ref_image.putpalette(palette)
|
||||
|
||||
|
||||
class ImageShrink:
|
||||
"""Shrinks a given image (bytearray) to a given resolution (width, height)"""
|
||||
@ -12,11 +39,9 @@ class ImageShrink:
|
||||
def convert(self, image: bytearray) -> Image:
|
||||
# load image from bytearray
|
||||
image = Image.open(io.BytesIO(image))
|
||||
image.show()
|
||||
image = self.shrink(image)
|
||||
image.show()
|
||||
image = self.convert_to_black_and_white(image)
|
||||
image.show()
|
||||
image = self.convert_to_reduced_colors(image)
|
||||
# image.save("test.png")
|
||||
|
||||
|
||||
def shrink(self, image: Image) -> Image:
|
||||
@ -25,7 +50,6 @@ class ImageShrink:
|
||||
return image
|
||||
|
||||
|
||||
def convert_to_black_and_white(self, image: Image) -> Image:
|
||||
# img = Image.open(image)
|
||||
image = image.convert("L")
|
||||
return image
|
||||
def convert_to_reduced_colors(self, image: Image) -> Image:
|
||||
new_image = image.quantize(colors = len(palette), palette=ref_image, dither=True)
|
||||
return new_image
|
||||
|
101
image_show.py
101
image_show.py
@ -1,69 +1,52 @@
|
||||
import time
|
||||
from PIL import Image,ImageDraw,ImageFont
|
||||
import traceback
|
||||
from PIL import Image, ImageDraw
|
||||
from waveshare-epaper import epd7in3f
|
||||
|
||||
|
||||
epd = epd7in5_V2.EPD()
|
||||
|
||||
logging.info("init and Clear")
|
||||
epd.init()
|
||||
epd.Clear()
|
||||
# BLACK = 0x000000 # 0000 BGR
|
||||
# WHITE = 0xffffff # 0001
|
||||
# GREEN = 0x00ff00 # 0010
|
||||
# BLUE = 0xff0000 # 0011
|
||||
# RED = 0x0000ff # 0100
|
||||
# YELLOW = 0x00ffff # 0101
|
||||
# ORANGE = 0x0080ff # 0110
|
||||
|
||||
font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
|
||||
font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
|
||||
class ImageShowError(Exception):
|
||||
pass
|
||||
|
||||
# Drawing on the Horizontal image
|
||||
logging.info("1.Drawing on the Horizontal image...")
|
||||
Himage = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
|
||||
draw = ImageDraw.Draw(Himage)
|
||||
draw.text((10, 0), 'hello world', font = font24, fill = 0)
|
||||
draw.text((10, 20), '7.5inch e-Paper', font = font24, fill = 0)
|
||||
draw.text((150, 0), u'微雪电子', font = font24, fill = 0)
|
||||
draw.line((20, 50, 70, 100), fill = 0)
|
||||
draw.line((70, 50, 20, 100), fill = 0)
|
||||
draw.rectangle((20, 50, 70, 100), outline = 0)
|
||||
draw.line((165, 50, 165, 100), fill = 0)
|
||||
draw.line((140, 75, 190, 75), fill = 0)
|
||||
draw.arc((140, 50, 190, 100), 0, 360, fill = 0)
|
||||
draw.rectangle((80, 50, 130, 100), fill = 0)
|
||||
draw.chord((200, 50, 250, 100), 0, 360, fill = 0)
|
||||
epd.display(epd.getbuffer(Himage))
|
||||
time.sleep(2)
|
||||
|
||||
# Drawing on the Vertical image
|
||||
logging.info("2.Drawing on the Vertical image...")
|
||||
Limage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
|
||||
draw = ImageDraw.Draw(Limage)
|
||||
draw.text((2, 0), 'hello world', font = font18, fill = 0)
|
||||
draw.text((2, 20), '7.5inch epd', font = font18, fill = 0)
|
||||
draw.text((20, 50), u'微雪电子', font = font18, fill = 0)
|
||||
draw.line((10, 90, 60, 140), fill = 0)
|
||||
draw.line((60, 90, 10, 140), fill = 0)
|
||||
draw.rectangle((10, 90, 60, 140), outline = 0)
|
||||
draw.line((95, 90, 95, 140), fill = 0)
|
||||
draw.line((70, 115, 120, 115), fill = 0)
|
||||
draw.arc((70, 90, 120, 140), 0, 360, fill = 0)
|
||||
draw.rectangle((10, 150, 60, 200), fill = 0)
|
||||
draw.chord((70, 150, 120, 200), 0, 360, fill = 0)
|
||||
epd.display(epd.getbuffer(Limage))
|
||||
time.sleep(2)
|
||||
class ImageShow:
|
||||
epd = epd7in3f.EPD()
|
||||
|
||||
logging.info("3.read bmp file")
|
||||
Himage = Image.open(os.path.join(picdir, '7in5_V2.bmp'))
|
||||
epd.display(epd.getbuffer(Himage))
|
||||
time.sleep(2)
|
||||
def __init__(self) -> None:
|
||||
self.epd.init()
|
||||
self.epd.Clear()
|
||||
|
||||
logging.info("4.read bmp file on window")
|
||||
Himage2 = Image.new('1', (epd.width, epd.height), 255) # 255: clear the frame
|
||||
bmp = Image.open(os.path.join(picdir, '100x100.bmp'))
|
||||
Himage2.paste(bmp, (50,10))
|
||||
epd.display(epd.getbuffer(Himage2))
|
||||
time.sleep(2)
|
||||
|
||||
logging.info("Clear...")
|
||||
epd.init()
|
||||
epd.Clear()
|
||||
def show_image(self, image: Image) -> None:
|
||||
if image.size != (self.epd.width, self.epd.height):
|
||||
raise ImageShowError("Image does not match screen size")
|
||||
|
||||
logging.info("Goto Sleep...")
|
||||
epd.sleep()
|
||||
|
||||
# self.__init__()
|
||||
# possibly include a blank image to clear screen
|
||||
|
||||
self.epd.display(self.epd.getbuffer(image))
|
||||
self.epd.sleep()
|
||||
|
||||
|
||||
def draw_sample_image(self):
|
||||
Himage = Image.new('RGB', (self.epd.width, self.epd.height), self.epd.WHITE) # 255: clear the frame
|
||||
draw = ImageDraw.Draw(Himage)
|
||||
# draw.text((5, 0), 'hello world', font = font18, fill = self.epd.RED)
|
||||
# draw.text((5, 20), '7.3inch e-Paper (F)', font = font24, fill = self.epd.YELLOW)
|
||||
# draw.text((5, 45), u'微雪电子', font = font40, fill = self.epd.GREEN)
|
||||
# draw.text((5, 85), u'微雪电子', font = font40, fill = self.epd.BLUE)
|
||||
# draw.text((5, 125), u'微雪电子', font = font40, fill = self.epd.ORANGE)
|
||||
|
||||
draw.line((5, 170, 80, 245), fill = self.epd.BLUE)
|
||||
draw.line((80, 170, 5, 245), fill = self.epd.ORANGE)
|
||||
draw.rectangle((5, 170, 80, 245), outline = self.epd.BLACK)
|
||||
draw.rectangle((90, 170, 165, 245), fill = self.epd.GREEN)
|
||||
draw.arc((5, 250, 80, 325), 0, 360, fill = self.epd.RED)
|
||||
draw.chord((90, 250, 165, 325), 0, 360, fill = self.epd.YELLOW)
|
||||
self.epd.display(self.epd.getbuffer(Himage))
|
||||
|
Loading…
x
Reference in New Issue
Block a user