Skip to content

Commit

Permalink
Add grayscale image conversion (txoof#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasR committed Feb 2, 2025
1 parent 02bb904 commit 33178b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
25 changes: 22 additions & 3 deletions epdlib/Screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,18 +666,18 @@ def _full_writeEPD_hd(self, image):
def _full_writeEPD_non_hd(self, image):
'''wipe screen and write an image'''
if self.screen_type == ScreenType.FOUR_GRAYS:
image_buffer = self.epd.getbuffer_4Gray(image)
image_buffer = self.epd.getbuffer_4Gray(Screen.image_to_4_grays(image))
else:
image_buffer = self.epd.getbuffer(image)

try:
if self.screen_type == ScreenType.FOUR_GRAYS:
logging.debug('one-bit display')
logging.debug('4 grayscale display')
self.epd.display_4Gray(image_buffer)
elif self.screen_type == ScreenType.THREE_COLORS: # displays that require multiple images
logging.debug('bi-color display')
self.epd.display(image_buffer, self.buffer_no_image)
else: # 7 color displays
else: # 7 color or monochrome displays
logging.debug('seven-color or monochrome display')
self.epd.display(image_buffer)

Expand Down Expand Up @@ -771,6 +771,25 @@ def module_exit(self):
logging.warning(f'failed to sleep module: {e}')
raise ScreenError(e)

@staticmethod

This comment has been minimized.

Copy link
@txoof

txoof Feb 2, 2025

Great documentation here. Thanks

def image_to_4_grays(image):
logging.debug('converting image to 4 grays')

'''
Waveshare displays do not render colors accurately.
For example, an input color of "#808080" looks more like "#a9aca3" on the ePaper.
Therefore, we convert our images to grayscale in 2 steps:
1. Convert colors to their actual displayed output
2. Update palette to reverse the error
'''
palette = Screen.colors2palette(constants.COLORS_4GRAY_NATURAL.values())
image_gs = Screen.reduce_palette(image, palette, True)

ws_palette = Screen.colors2palette(constants.COLORS_4GRAY_WS.values())
image_gs.putpalette(ws_palette)
return image_gs



# + code_folding=[]
def list_compatible_modules(print_modules=True, reasons=False):
Expand Down
20 changes: 20 additions & 0 deletions epdlib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@
'ORANGE': (255, 128, 0)
}

COLORS_4GRAY_WS = {
'BLACK': (0, 0, 0),
'DARK_GRAY': (0x80, 0x80, 0x80),
'BRIGHT_GRAY': (0xc0, 0xc0, 0xc0),
'WHITE': (0xff, 0xff, 0xff)
}

'''
"natural" display colors of 4 grayscale displays.
Tweaking these may improve image rendering.
The present values are based on experiments with a 2.7" e-Paper HAT,
using various photographs and graphics as test inputs.
'''
COLORS_4GRAY_NATURAL = {
'BLACK': (0x1b, 0x1b, 0x13),
'DARK_GRAY': (0x4d, 0x4d, 0x44),
'BRIGHT_GRAY': (0xa9, 0xac, 0xa3),
'WHITE': (0xeb, 0xea, 0xdf)
}

CLEAR_COLOR = 0xFF

LAYOUT_DEFAULTS = {
Expand Down

0 comments on commit 33178b6

Please sign in to comment.