2

I am working on a project which involves displaying images on a 128*64 Oled display. To do this I use Pillow together with Python3.

Basically all works fine, I am able to show the images I like to display. As long as they are exactly 128*64 pixels that is.

But what I really like is have many smaller images. Say 16*16. These smaller images I would then like to write to the display on a specific X,Y location based on some input variables. So overwrite the current 16*16 pixels on that location with the 16*16 image I provide.

When I do that now I get the correct error that my image is smaller then the display size. (Full error below). Well, I know that is correct, but is there a way how I still can achieve this?

I am breaking my head about it. And I can't seem to find in the documentation or Internet.

Traceback (most recent call last):
  File "show_image.py", line 37, in <module>
    show('./images/1_C.png')
  File "show_image.py", line 28, in show
    disp.image(image)
  File "/usr/local/lib/python3.6/dist-packages/Adafruit_SSD1306/SSD1306.py", line 193, in image
    .format(self.width, self.height))
ValueError: Image must be same dimensions as display (128x64).
Derooie
  • 121
  • 2
  • 11

1 Answers1

0

You can do that like this:

from PIL import Image

# Create black background same size as OLED
bg = Image.new('RGB',(128,64),0)

# Load a 16x16 red image from disk and paste into background
red = Image.open('red.png').convert('RGB')
bg.paste(red, (10,20))

# Create 16x16 blue image in memory and paste into background
blue = Image.new('RGB', (16,16), color=(0,0,255))
bg.paste(blue, (80,40))

# Save result
bg.save('result.png')

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Cool, I am going to try that. Would be great. Thanks for your response. – Derooie Mar 31 '20 at 16:31
  • But wait, this results in a new screen-filling png, right? This would mean that every update (about every 2/3 seconds) I need to create a new image, and worse, write the whole new image to the screen. If this was instant and you would see at all that the whole screen is rewritten, fine. But rewriting to the OLED takes some time, you can clearly see the whole screen is rebuilding. That doesn't look good. For that reason, I hoped it was possible to only rewrite that part of the screen where actually changes are to write. – Derooie Mar 31 '20 at 16:50
  • Not sure how your OLED is connected or why it is so slow. Mine updates really quickly and the screen doesn't appear to flicker at all. This is mine... https://www.amazon.co.uk/gp/product/B01N97V0KH/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1 – Mark Setchell Mar 31 '20 at 17:07
  • Really? Cool. So if you change an image, for an exact same image but with a few pixels different than first image, you literally only see that pixels changed? My screen builds up fast, but fast is relative. It isnt instant, So going from image one, to imagestw 2, shows that the full screen is rebuild and not only the changed parts. (yes that goes fast, but is noticeable). Maybe it has to do with my code. I use the adafruit lib and pillow. – Derooie Apr 02 '20 at 11:18