diff --git a/gfxlcd/demos/dsp2017_101_64.png b/gfxlcd/demos/assets/dsp2017_101_64.png similarity index 100% rename from gfxlcd/demos/dsp2017_101_64.png rename to gfxlcd/demos/assets/dsp2017_101_64.png diff --git a/gfxlcd/demos/dsp2017_122_29.png b/gfxlcd/demos/assets/dsp2017_122_29.png similarity index 100% rename from gfxlcd/demos/dsp2017_122_29.png rename to gfxlcd/demos/assets/dsp2017_122_29.png diff --git a/gfxlcd/demos/japan_temple_240x320.jpg b/gfxlcd/demos/assets/japan_temple_240x320.jpg similarity index 100% rename from gfxlcd/demos/japan_temple_240x320.jpg rename to gfxlcd/demos/assets/japan_temple_240x320.jpg diff --git a/gfxlcd/demos/ili_image.py b/gfxlcd/demos/ili_image.py index a96ab44..a1274f2 100644 --- a/gfxlcd/demos/ili_image.py +++ b/gfxlcd/demos/ili_image.py @@ -15,6 +15,5 @@ LED = 6 RPi.GPIO.setup(LED, RPi.GPIO.OUT) RPi.GPIO.output(LED, 1) -image_file = Image.open("japan_temple_240x320.jpg") -image_file = image_file.convert('RGB') +image_file = Image.open("assets/japan_temple_240x320.jpg") lcd_tft.draw_image(0, 0, image_file) diff --git a/gfxlcd/demos/nju_image.py b/gfxlcd/demos/nju_image.py new file mode 100644 index 0000000..08ea118 --- /dev/null +++ b/gfxlcd/demos/nju_image.py @@ -0,0 +1,20 @@ +import RPi.GPIO +import sys +import random +sys.path.append("../../") +from gfxlcd.driver.nju6450.gpio import GPIO +from gfxlcd.driver.nju6450.nju6450 import NJU6450 +RPi.GPIO.setmode(RPi.GPIO.BCM) + +from PIL import Image + +lcd_nju = NJU6450(122, 32, GPIO()) +lcd_nju.init() +lcd_nju.auto_flush = False + +image_file = Image.open("assets/dsp2017_122_29.png") +# lcd_nju.threshold = 50 +# lcd_nju.transparency_color = [110, 57] +lcd_nju.draw_image(0, 0, image_file) + +lcd_nju.flush(True) \ No newline at end of file diff --git a/gfxlcd/demos/ssd_image.py b/gfxlcd/demos/ssd_image.py new file mode 100644 index 0000000..5e83d9b --- /dev/null +++ b/gfxlcd/demos/ssd_image.py @@ -0,0 +1,23 @@ +import RPi.GPIO +import sys +sys.path.append("../../") +from gfxlcd.driver.ssd1306.spi import SPI +from gfxlcd.driver.ssd1306.ssd1306 import SSD1306 +RPi.GPIO.setmode(RPi.GPIO.BCM) +from PIL import Image + +lcd_oled = SSD1306(128, 64, SPI()) +lcd_oled.init() +lcd_oled.auto_flush = False + +image_file = Image.open("assets/dsp2017_101_64.png") + +lcd_oled.threshold = 50 + +lcd_oled.threshold = 0 +# lcd_oled.transparency_color = [110, 57] #110 #[110, 57] +# lcd_oled.threshold = 255 + +lcd_oled.draw_image(10, 0, image_file) + +lcd_oled.flush(True) \ No newline at end of file diff --git a/gfxlcd/drawing/page.py b/gfxlcd/drawing/page.py index 6e6ca58..b5bf4c6 100644 --- a/gfxlcd/drawing/page.py +++ b/gfxlcd/drawing/page.py @@ -113,3 +113,26 @@ class Page(Pixel, metaclass=abc.ABCMeta): def flush(self, force=None): """flush buffer to the screen""" pass + + def draw_image(self, pos_x, pos_y, image): + """draw a PIL image""" + image_file = image.convert('L') + width, height = image_file.size + offset_x = 0 + offset_y = 0 + for stream in list(image_file.getdata()): + if stream > self.options['threshold'] and not self._is_transparent(stream): + self.draw_pixel(pos_x + offset_x, pos_y + offset_y) + offset_x += 1 + if offset_x > width - 1: + offset_x = 0 + offset_y += 1 + + def _is_transparent(self, color): + """check if color is a transparency color""" + if type(self.options['transparency_color']) == int and color == self.options['transparency_color']: + return True + elif type(self.options['transparency_color']) == list and color in self.options['transparency_color']: + return True + + return False diff --git a/gfxlcd/drawing/pixel.py b/gfxlcd/drawing/pixel.py index c2c22ac..a3a7339 100644 --- a/gfxlcd/drawing/pixel.py +++ b/gfxlcd/drawing/pixel.py @@ -12,6 +12,28 @@ class Pixel(object): self.options['background_color'] = { 'R': 0, 'G': 0, 'B': 0, } + self.options['threshold'] = 50 + self.options['transparency_color'] = None + + @property + def threshold(self): + """get threshold for B&W conversion""" + return self.options['threshold'] + + @threshold.setter + def threshold(self, threshold): + """set B&W threshold for conversion """ + self.options['threshold'] = threshold + + @property + def transparency_color(self): + """get transparency color""" + return self.options['transparency_color'] + + @transparency_color.setter + def transparency_color(self, transparency_color): + """set transparency color """ + self.options['transparency_color'] = transparency_color def draw_pixel(self, pos_x, pos_y): """dummy fuction"""