Skip to content

Commit

Permalink
Merge branch 'feat/gray_scale' into feat/logo_layout
Browse files Browse the repository at this point in the history
  • Loading branch information
loiccoyle committed Jul 1, 2024
2 parents ccc8304 + b610295 commit 59cb743
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 88 deletions.
76 changes: 32 additions & 44 deletions tinyticker/waveshare_lib/epd2in7.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

import numpy as np

from ._base import EPDMonochrome

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -656,50 +658,36 @@ def Init_4Gray(self):
self.send_data(0x57)

def getbuffer_4Gray(self, image):
# logger.debug("bufsiz = ",int(self.width/8) * self.height)
buf = [0xFF] * (int(self.width / 4) * self.height)
image_monocolor = image.convert("L")
imwidth, imheight = image_monocolor.size
pixels = image_monocolor.load()
i = 0
# logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
if imwidth == self.width and imheight == self.height:
logger.debug("Vertical")
for y in range(imheight):
for x in range(imwidth):
# Set the bits for the column of pixels at the current position.
if pixels[x, y] == 0xC0:
pixels[x, y] = 0x80
elif pixels[x, y] == 0x80:
pixels[x, y] = 0x40
i = i + 1
if i % 4 == 0:
buf[int((x + (y * self.width)) / 4)] = (
(pixels[x - 3, y] & 0xC0)
| (pixels[x - 2, y] & 0xC0) >> 2
| (pixels[x - 1, y] & 0xC0) >> 4
| (pixels[x, y] & 0xC0) >> 6
)

elif imwidth == self.height and imheight == self.width:
logger.debug("Horizontal")
for x in range(imwidth):
for y in range(imheight):
newx = y
newy = self.height - x - 1
if pixels[x, y] == 0xC0:
pixels[x, y] = 0x80
elif pixels[x, y] == 0x80:
pixels[x, y] = 0x40
i = i + 1
if i % 4 == 0:
buf[int((newx + (newy * self.width)) / 4)] = (
(pixels[x, y - 3] & 0xC0)
| (pixels[x, y - 2] & 0xC0) >> 2
| (pixels[x, y - 1] & 0xC0) >> 4
| (pixels[x, y] & 0xC0) >> 6
)
return buf
if (self.height, self.width) == image.size:
# image has correct dimensions, but needs to be rotated
image = image.rotate(90, expand=True)
if (self.width, self.height) != image.size:
raise ValueError(
f"Wrong image dimensions, must be {self.width}x{self.height}"
)
if image.mode != "L":
image = image.convert("L")

pixels = np.array(image)

# we process the image in chunks of 4 pixels by reshaping
# we pack the bits of 4 pixels into a single byte
# 00011011 -> black, light gray, dark gray, white
pixels = pixels.reshape((self.height, self.width // 4, 4))
# not really sure why they do this, but it's in the waveshare code
pixels = np.where(pixels == 0x80, 0x40, pixels)
pixels = np.where(pixels == 0xC0, 0x80, pixels)
# keep the first 2 bits, which basically quantizes the image to 4 grey levels
pixels = pixels & 0xC0
# pack the 4 pixels into a single byte
packed_pixels = (
(pixels[:, :, 0])
| (pixels[:, :, 1] >> 2)
| (pixels[:, :, 2] >> 4)
| pixels[:, :, 3] >> 6
)

return bytearray(packed_pixels.flatten())

def display(self, image):
self.send_command(0x13)
Expand Down
76 changes: 32 additions & 44 deletions tinyticker/waveshare_lib/epd2in7_V2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

import numpy as np

from ._base import EPDMonochrome

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -339,50 +341,36 @@ def Init_4Gray(self):
self.ReadBusy()

def getbuffer_4Gray(self, image):
# logger.debug("bufsiz = ",int(self.width/8) * self.height)
buf = [0xFF] * (int(self.width / 4) * self.height)
image_monocolor = image.convert("L")
imwidth, imheight = image_monocolor.size
pixels = image_monocolor.load()
i = 0
# logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
if imwidth == self.width and imheight == self.height:
logger.debug("Vertical")
for y in range(imheight):
for x in range(imwidth):
# Set the bits for the column of pixels at the current position.
if pixels[x, y] == 0xC0:
pixels[x, y] = 0x80
elif pixels[x, y] == 0x80:
pixels[x, y] = 0x40
i = i + 1
if i % 4 == 0:
buf[int((x + (y * self.width)) / 4)] = (
(pixels[x - 3, y] & 0xC0)
| (pixels[x - 2, y] & 0xC0) >> 2
| (pixels[x - 1, y] & 0xC0) >> 4
| (pixels[x, y] & 0xC0) >> 6
)

elif imwidth == self.height and imheight == self.width:
logger.debug("Horizontal")
for x in range(imwidth):
for y in range(imheight):
newx = y
newy = self.height - x - 1
if pixels[x, y] == 0xC0:
pixels[x, y] = 0x80
elif pixels[x, y] == 0x80:
pixels[x, y] = 0x40
i = i + 1
if i % 4 == 0:
buf[int((newx + (newy * self.width)) / 4)] = (
(pixels[x, y - 3] & 0xC0)
| (pixels[x, y - 2] & 0xC0) >> 2
| (pixels[x, y - 1] & 0xC0) >> 4
| (pixels[x, y] & 0xC0) >> 6
)
return buf
if (self.height, self.width) == image.size:
# image has correct dimensions, but needs to be rotated
image = image.rotate(90, expand=True)
if (self.width, self.height) != image.size:
raise ValueError(
f"Wrong image dimensions, must be {self.width}x{self.height}"
)
if image.mode != "L":
image = image.convert("L")

pixels = np.array(image)

# we process the image in chunks of 4 pixels by reshaping
# we pack the bits of 4 pixels into a single byte
# 00011011 -> black, light gray, dark gray, white
pixels = pixels.reshape((self.height, self.width // 4, 4))
# not really sure why they do this, but it's in the waveshare code
pixels = np.where(pixels == 0x80, 0x40, pixels)
pixels = np.where(pixels == 0xC0, 0x80, pixels)
# keep the first 2 bits, which basically quantizes the image to 4 grey levels
pixels = pixels & 0xC0
# pack the 4 pixels into a single byte
packed_pixels = (
(pixels[:, :, 0])
| (pixels[:, :, 1] >> 2)
| (pixels[:, :, 2] >> 4)
| pixels[:, :, 3] >> 6
)

return bytearray(packed_pixels.flatten())

def display(self, image):
self.send_command(0x24)
Expand Down

0 comments on commit 59cb743

Please sign in to comment.