Merge pull request #2 from bkosciow/draw_image

Draw image
This commit is contained in:
Bartosz 2017-04-29 16:17:36 +02:00 committed by GitHub
commit 56b343c5d3
15 changed files with 186 additions and 4 deletions

View File

@ -99,3 +99,8 @@ class Chip(metaclass=abc.ABCMeta):
def fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2): def fill_rect(self, pos_x1, pos_y1, pos_x2, pos_y2):
"""draw a filled rectangle""" """draw a filled rectangle"""
pass pass
@abc.abstractmethod
def draw_image(self, pos_x, pos_y, image):
"""draw a PIL image"""
pass

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

23
gfxlcd/demos/ili_image.py Normal file
View File

@ -0,0 +1,23 @@
import RPi.GPIO
import sys
from PIL import Image
sys.path.append("../../")
from gfxlcd.driver.ili9325.gpio import GPIO as ILIGPIO
from gfxlcd.driver.ili9325.ili9325 import ILI9325
RPi.GPIO.setmode(RPi.GPIO.BCM)
lcd_tft = ILI9325(240, 320, ILIGPIO())
lcd_tft.init()
# bypass of missing +3v line to power backlight
LED = 6
RPi.GPIO.setup(LED, RPi.GPIO.OUT)
RPi.GPIO.output(LED, 1)
image_file = Image.open("assets/japan_temple_240x320.jpg")
lcd_tft.draw_image(0, 0, image_file)
numbers_image = Image.open("assets/dsp2017_101_64.png")
lcd_tft.transparency_color = (0, 0, 0)
lcd_tft.draw_image(10, 10, numbers_image)

20
gfxlcd/demos/nju_image.py Normal file
View File

@ -0,0 +1,20 @@
import RPi.GPIO
import sys
import random
from PIL import Image
sys.path.append("../../")
from gfxlcd.driver.nju6450.gpio import GPIO
from gfxlcd.driver.nju6450.nju6450 import NJU6450
RPi.GPIO.setmode(RPi.GPIO.BCM)
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)

23
gfxlcd/demos/ssd_image.py Normal file
View File

@ -0,0 +1,23 @@
import RPi.GPIO
import sys
from PIL import Image
sys.path.append("../../")
from gfxlcd.driver.ssd1306.spi import SPI
from gfxlcd.driver.ssd1306.ssd1306 import SSD1306
RPi.GPIO.setmode(RPi.GPIO.BCM)
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)

View File

@ -121,3 +121,59 @@ class Area(Pixel):
color = self._converted_background_color() color = self._converted_background_color()
for _ in range(0, size): for _ in range(0, size):
self.driver.data(color, None) self.driver.data(color, None)
def draw_image(self, pos_x, pos_y, image):
"""draw a PIL image"""
image_file = image.convert('RGB')
width, height = image_file.size
self._set_area(
pos_x,
pos_y,
pos_x + width - 1,
pos_y + height - 1
)
row = 0
col = 0
area = None
temporary_area = None
for red, green, blue in list(image_file.getdata()):
if self._is_transparent((red, green, blue)):
area = (
pos_x,
pos_y + row + 1,
pos_x + width - 1,
pos_y + height - 1
)
temporary_area = (
pos_x + col + 1,
pos_y + row,
pos_x + width - 1,
pos_y + row
)
else:
if temporary_area is not None:
self._set_area(*temporary_area)
temporary_area = None
self.color = (red, green, blue)
self.driver.data(self._converted_color(), None)
col += 1
if col > width - 1:
col = 0
row += 1
if area is not None:
self._set_area(*area)
area = None
def _is_transparent(self, color):
"""check if color is a transparency color"""
if self.options['transparency_color'] is None:
return False
elif type(self.options['transparency_color'][0]) == int \
and color == self.options['transparency_color']:
return True
elif type(self.options['transparency_color'][0]) == list \
and color in self.options['transparency_color']:
return True
return False

View File

@ -113,3 +113,29 @@ class Page(Pixel, metaclass=abc.ABCMeta):
def flush(self, force=None): def flush(self, force=None):
"""flush buffer to the screen""" """flush buffer to the screen"""
pass 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

View File

@ -12,6 +12,28 @@ class Pixel(object):
self.options['background_color'] = { self.options['background_color'] = {
'R': 0, 'G': 0, 'B': 0, '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): def draw_pixel(self, pos_x, pos_y):
"""dummy fuction""" """dummy fuction"""

View File

@ -1,2 +1,2 @@
"""driver/ssd1306 module""" """driver/ssd1306 module"""
__author__ = 'Bartosz Kosciow' __author__ = 'Bartosz Kosciow'

View File

@ -13,6 +13,7 @@ On NJU and SSD uses buffer to keep current content as help for page operations.
Wiring is below Wiring is below
Demos are in demos directory
Initialization Initialization
=== ===
@ -111,12 +112,18 @@ draw_arc(x1, y1, radius, from_angle, to_angle
fill_rect(x1, y1, x2, y2) fill_rect(x1, y1, x2, y2)
draw_image(x, y, PIL.Image)
Colours Colours
=== ===
lcd.color = (r, g, b) lcd.color = (r, g, b)
lcd.background_color = (r, g ,b) lcd.background_color = (r, g ,b)
lcd.threshold = 255 - for images a threshold between black and white (on monochrome)
lcd.transparency_color = [110, 57] #110 - color(s) that are skipped during drawing an image
Wiring Wiring
=== ===

View File

@ -13,11 +13,11 @@ def read(*paths):
setup( setup(
name='gfxlcd', name='gfxlcd',
version='0.1.0', version='0.1.2',
description='gfxlcd is a handler for grpahical lcds: ILI9328, SSD1306, NJU6450 @ Raspberry Pi.', description='gfxlcd is a handler for grpahical lcds: ILI9328, SSD1306, NJU6450 @ Raspberry Pi.',
keywords=['gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd'], keywords=['gfxlcd', 'raspberry pi' ,'ili9328' ,'ssd1306', 'nju6450', 'lcd', 'graphical lcd'],
long_description=(read('readme.md')), long_description=(read('readme.md')),
url='https://bitbucket.org/kosci/charlcd.git', url='https://github.com/bkosciow/gfxlcd',
license='MIT', license='MIT',
author='Bartosz Kościów', author='Bartosz Kościów',
author_email='kosci1@gmail.com', author_email='kosci1@gmail.com',

View File

@ -16,5 +16,5 @@ commands= nosetests --with-xunit --xunit-file=junit-{envname}.xml gfxlcd/tests
/bin/bash -c "pylint gfxlcd > pylint-{envname}.log || :" /bin/bash -c "pylint gfxlcd > pylint-{envname}.log || :"
[flake8] [flake8]
show-source = True show-source = True
exclude = .git,.venv,.tox,dist,doc,build,*egg,*/tests/* exclude = .git,.venv,.tox,dist,doc,build,*egg,*/tests/*,*/demos/*