Allow draw_image to consume bytes.

Make path and bytes optional parameters where only one can be specified.
pull/4/head
Mayur Saxena 4 years ago committed by GitHub
parent 6c2f8ca250
commit fc6b148e56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@ from time import sleep
from math import cos, sin, pi, radians from math import cos, sin, pi, radians
from sys import implementation from sys import implementation
import ustruct import ustruct
from uio import BytesIO
def color565(r, g, b): def color565(r, g, b):
@ -315,12 +316,13 @@ class Display(object):
return return
line = color.to_bytes(2, 'big') * w line = color.to_bytes(2, 'big') * w
self.block(x, y, x + w - 1, y, line) self.block(x, y, x + w - 1, y, line)
def draw_image(self, path, x=0, y=0, w=320, h=240): def draw_image(self, path=None, bytes=None, x=0, y=0, w=320, h=240):
"""Draw image from flash. """Draw image from flash.
Args: Args:
path (string): Image file path. path (string): Image file path.
bytes (bytes): Image bytes.
x (int): X coordinate of image left. Default is 0. x (int): X coordinate of image left. Default is 0.
y (int): Y coordinate of image top. Default is 0. y (int): Y coordinate of image top. Default is 0.
w (int): Width of image. Default is 320. w (int): Width of image. Default is 320.
@ -330,7 +332,11 @@ class Display(object):
y2 = y + h - 1 y2 = y + h - 1
if self.is_off_grid(x, y, x2, y2): if self.is_off_grid(x, y, x2, y2):
return return
with open(path, "rb") as f:
if (path is None and bytes is None) or (path is not None and bytes is not None):
return
with open(path, "rb") if path else BytesIO(bytes) as f:
chunk_height = 1024 // w chunk_height = 1024 // w
chunk_count, remainder = divmod(h, chunk_height) chunk_count, remainder = divmod(h, chunk_height)
chunk_size = chunk_height * w * 2 chunk_size = chunk_height * w * 2

Loading…
Cancel
Save