Skip to content

Commit 33178b6

Browse files
committed
Add grayscale image conversion (#71)
1 parent 02bb904 commit 33178b6

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

epdlib/Screen.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,18 +666,18 @@ def _full_writeEPD_hd(self, image):
666666
def _full_writeEPD_non_hd(self, image):
667667
'''wipe screen and write an image'''
668668
if self.screen_type == ScreenType.FOUR_GRAYS:
669-
image_buffer = self.epd.getbuffer_4Gray(image)
669+
image_buffer = self.epd.getbuffer_4Gray(Screen.image_to_4_grays(image))
670670
else:
671671
image_buffer = self.epd.getbuffer(image)
672672

673673
try:
674674
if self.screen_type == ScreenType.FOUR_GRAYS:
675-
logging.debug('one-bit display')
675+
logging.debug('4 grayscale display')
676676
self.epd.display_4Gray(image_buffer)
677677
elif self.screen_type == ScreenType.THREE_COLORS: # displays that require multiple images
678678
logging.debug('bi-color display')
679679
self.epd.display(image_buffer, self.buffer_no_image)
680-
else: # 7 color displays
680+
else: # 7 color or monochrome displays
681681
logging.debug('seven-color or monochrome display')
682682
self.epd.display(image_buffer)
683683

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

774+
@staticmethod
775+
def image_to_4_grays(image):
776+
logging.debug('converting image to 4 grays')
777+
778+
'''
779+
Waveshare displays do not render colors accurately.
780+
For example, an input color of "#808080" looks more like "#a9aca3" on the ePaper.
781+
Therefore, we convert our images to grayscale in 2 steps:
782+
1. Convert colors to their actual displayed output
783+
2. Update palette to reverse the error
784+
'''
785+
palette = Screen.colors2palette(constants.COLORS_4GRAY_NATURAL.values())
786+
image_gs = Screen.reduce_palette(image, palette, True)
787+
788+
ws_palette = Screen.colors2palette(constants.COLORS_4GRAY_WS.values())
789+
image_gs.putpalette(ws_palette)
790+
return image_gs
791+
792+
774793

775794
# + code_folding=[]
776795
def list_compatible_modules(print_modules=True, reasons=False):

epdlib/constants.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@
2323
'ORANGE': (255, 128, 0)
2424
}
2525

26+
COLORS_4GRAY_WS = {
27+
'BLACK': (0, 0, 0),
28+
'DARK_GRAY': (0x80, 0x80, 0x80),
29+
'BRIGHT_GRAY': (0xc0, 0xc0, 0xc0),
30+
'WHITE': (0xff, 0xff, 0xff)
31+
}
32+
33+
'''
34+
"natural" display colors of 4 grayscale displays.
35+
Tweaking these may improve image rendering.
36+
The present values are based on experiments with a 2.7" e-Paper HAT,
37+
using various photographs and graphics as test inputs.
38+
'''
39+
COLORS_4GRAY_NATURAL = {
40+
'BLACK': (0x1b, 0x1b, 0x13),
41+
'DARK_GRAY': (0x4d, 0x4d, 0x44),
42+
'BRIGHT_GRAY': (0xa9, 0xac, 0xa3),
43+
'WHITE': (0xeb, 0xea, 0xdf)
44+
}
45+
2646
CLEAR_COLOR = 0xFF
2747

2848
LAYOUT_DEFAULTS = {

0 commit comments

Comments
 (0)