1

I am trying to resize an image to display on my OLED device.

My image I want to display is: enter image description here

Currently, my OLED display only shows a fraction of the image: enter image description here

Based on How to resize image?, I added new_img = img.resize((128,128)) to my script to resize the image. The same fraction of the image however appears on the screen. When I tried to enter a lower image size for the resize argument (64x64), terminal prints:

pi@raspberrypi:~/project $ python colors.py --display ssd1351 --width 128 --height 128 --interface spi --spi-bus-speed 16000000 --gpio-data-command 20
Version: luma.oled 2.3.1 (luma.core 1.3.0)
Display: ssd1351
Interface: spi
Dimensions: 128 x 128
------------------------------------------------------------
Traceback (most recent call last):
  File "colors.py", line 87, in <module>
    main()
  File "colors.py", line 30, in main
    device.display(new_img)
  File "/usr/local/lib/python2.7/dist-packages/luma/oled/device.py", line 371, in display
    assert(image.size == self.size)
AssertionError

Any ideas on how I can resize the image properly for the OLED display?

My full script is:

#!/usr/bin/env python

import math
import time
import random
import os.path
from demo_opts import get_device
from luma.core.render import canvas
from PIL import Image


def main():
    img_path = os.path.abspath(os.path.join(
        os.path.dirname(__file__), 'plot.jpg'))
    img = Image.open(img_path) \
        .transform(device.size, Image.AFFINE, (1, 0, 0, 0, 1, 0), Image.BILINEAR) \
        .convert(device.mode)
    new_img = img.resize((128,128))

    while True:
        # Image display
        device.display(new_img)
        time.sleep(5)



if __name__ == "__main__":
    try:
        device = get_device()
        main()
    except KeyboardInterrupt:
        pass
Craver2000
  • 433
  • 1
  • 7
  • 24
  • I see pics and graphs, I upvote... – cs95 Jan 22 '18 at 11:34
  • You do `device = get_device()` at the bottom but then try to use it in `main()`. Why is that line not in main anyway? Avoid globals where possible. – Bailey Parker Jan 22 '18 at 11:37
  • @Bailey: I modified the script based on a demo script found on the OLED Luma library repo. I think the reason for that to be at the bottom is because there were other functions in the original script, which I have now removed. Thanks for pointing this out, I might later address that part. – Craver2000 Jan 22 '18 at 11:45
  • Your device display appears to be 128x128 so resizing it to 500x500 is probably not correct... – Mark Setchell Jan 22 '18 at 11:49
  • @ Mark. Thanks for pointing this out. I now resized it to 128x128. I no longer get the error, but only the same fraction of the original image gets displayed. I tried to go lower but then the assertion error returns. – Craver2000 Jan 22 '18 at 11:53
  • I don't speak Python, and am unsure of the purpose of `convert(device.mode)` however, it may be that resizing to 128x128 pixels changes the mode or number of colours to something unacceptable to the OLED. You may need to do it *after* resizing. – Mark Setchell Jan 22 '18 at 13:24

1 Answers1

1

Ok, I solved this by doing:

img = Image.open(img_path)  
img = img.resize((128, 128), Image.ANTIALIAS) \
    .transform(device.size, Image.AFFINE, (1, 0, 0, 0, 1, 0), Image.BILINEAR) \
    .convert(device.mode)

So do image resize first, before running transform and convert.

Craver2000
  • 433
  • 1
  • 7
  • 24