Skip to content

Commit

Permalink
Merge pull request #13 from pimoroni/patch-np-array
Browse files Browse the repository at this point in the history
Tidy up numpy image handling and fix tests
  • Loading branch information
Gadgetoid committed Apr 30, 2021
2 parents df4439f + eaf0885 commit f81b020
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 20 deletions.
7 changes: 3 additions & 4 deletions library/ST7789/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,10 @@ def image_to_data(self, image, rotation=0):
"""Generator function to convert a PIL image to 16-bit 565 RGB bytes."""
# NumPy is much faster at doing this. NumPy code provided by:
# Keith (https://www.blogger.com/profile/02555547344016007163)
if type(image) == np.ndarray:
pb = np.rot90(image, rotation // 90).astype('uint8')
else:
pb = np.rot90(np.array(image.convert('RGB')), rotation // 90).astype('uint8')
if not isinstance(image, np.ndarray):
image = np.array(image.convert('RGB'))

pb = np.rot90(image, rotation // 90).astype('uint8')

result = np.zeros((self._width, self._height, 2), dtype=np.uint8)
result[..., [0]] = np.add(np.bitwise_and(pb[..., [0]], 0xF8), np.right_shift(pb[..., [1]], 5))
Expand Down
11 changes: 0 additions & 11 deletions library/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,3 @@ def spidev():
sys.modules['spidev'] = spidev
yield spidev
del sys.modules['spidev']


@pytest.fixture(scope='function', autouse=False)
def numpy():
"""Mock numpy module."""

numpy = mock.MagicMock()

sys.modules['numpy'] = numpy
yield numpy
del sys.modules['numpy']
2 changes: 1 addition & 1 deletion library/tests/test_dimensions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_240_240(GPIO, spidev, numpy):
def test_240_240(GPIO, spidev):
import ST7789
display = ST7789.ST7789(port=0, cs=0, dc=24, width=240, height=240, rotation=0)
assert display.width == 240
Expand Down
4 changes: 1 addition & 3 deletions library/tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ def test_display_numpy_array(GPIO, spidev):
display = ST7789.ST7789(port=0, cs=0, dc=24)

image = numpy.empty((display.width, display.height, 3))

with pytest.raises(AttributeError):
display.display(image)
display.display(image)
2 changes: 1 addition & 1 deletion library/tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_setup(GPIO, spidev, numpy):
def test_setup(GPIO, spidev):
import ST7789
display = ST7789.ST7789(port=0, cs=0, dc=24)
del display

0 comments on commit f81b020

Please sign in to comment.