Convert a image and show in OLED display

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
bergpb
Posts: 16
Joined: Sun Jun 10, 2018 2:51 pm

Convert a image and show in OLED display

Post by bergpb » Fri Jun 15, 2018 1:37 pm

Hello,

Is possible get a image like this (http://icons.wxug.com/i/c/k/partlycloudy.gif), convert, and show in my oled SSD1306?
I'm making a weather station using Weather Underground Api.

bitninja
Posts: 165
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: Convert a image and show in OLED display

Post by bitninja » Fri Jun 15, 2018 5:07 pm

Hello,

I was able to put two and two together to figure out what you may be looking for.
20180615_114602.jpg
20180615_114602.jpg (83.7 KiB) Viewed 8054 times
Based on what I have done for this project https://github.com/joewez/ESP-Widget I use a simple text file that has monochrome bitmap encoded in hex values. For example, the graphic you link might be something like this...

Code: Select all

000000008000
0000000CC800
0000000FD800
0000006FFB80
0000007DBF80
0000027EFF30
000003FFF7F0
000003DFFDE0
0001FFFFFFC0
0007FE3FFF78
000FFE3FFFF8
001FFF1FFFB0
001BFF8C7FB8
0037FFC01FFC
003FFFE38730
002FFFE7E7B0
03EFFFFFF3F8
1F8FFFFFF018
3F0FFFFFF018
7FEFFFFFF3DC
FFFFFFFFFFFE
FFFFFFFFEFFE
FFFFFFFFFFFE
FFFFFFFFFFFE
FFFFFFFFFFFE
FFFFFFFFFFFE
FFFFFFFFFFFE
FFFFFFFFFFFE
7FFFFFFFFFFC
3FFFFFFFFFF8
Then I use this simple routine to display the graphic.

Code: Select all

    def hex2bits(self, hexstr):
        bitstr = ""
        for pos in range(0, len(hexstr) - 1, 2):
            newhex = hexstr[pos] + hexstr[pos + 1]
            newint = int(newhex, 16)
            newbin = bin(newint)[2:]
            newbits = '0' * (8 - len(newbin)) + newbin
            bitstr += newbits
        return bitstr

    def graphic(self, file, origin_x = 0, origin_y = 0, erase = False):
        pic = [line.rstrip('\r\n') for line in open(file)]
        for y, row in enumerate(pic):
            line = self.hex2bits(row)
            for x, col in enumerate(line):
                if erase:
                    self.fb.pixel(origin_x + x, origin_y + y, 0)
                else:
                    self.fb.pixel(origin_x + x, origin_y + y, int(col))
There are other methods, so you should search the forum, but if you are starting from where I think you are, this may be helpful.

BTW: I use this http://www.eran.io/the-dot-factory-an-l ... generator/ to convert from the image to text.

Good luck.
Attachments
partly.zip
(279 Bytes) Downloaded 468 times
Last edited by bitninja on Fri Jun 15, 2018 10:40 pm, edited 1 time in total.

bergpb
Posts: 16
Joined: Sun Jun 10, 2018 2:51 pm

Re: Convert a image and show in OLED display

Post by bergpb » Fri Jun 15, 2018 5:44 pm

:o Awesome thanks!!! I try this im my project. :D

mfitzp
Posts: 3
Joined: Mon Jul 17, 2017 1:56 pm

Re: Convert a image and show in OLED display

Post by mfitzp » Tue Aug 28, 2018 4:52 pm

Might be a little late for you, but you can also do this by converting the image in GIMP and exporting as PBM binary. The PBM format is monochrome, and aside from the header lines (can readline 3x to skip these) is the correct format to load into a framebuffer object.

import framebuf

with open('my_image.pbm', 'rb') as f:
f.readline() # Magic number
f.readline() # Creator comment
f.readline() # Dimensions
data = bytearray(f.read())
fbuf = framebuf.FrameBuffer(data, 128, 64, framebuf.MONO_HLSB)

To display just blit it to the display's framebuffer (note you need to invert, since ON pixels are dark on a normal screen, light on OLED).

display.invert(1)
display.blit(fbuf, 0, 0)
display.show()

I wrote up some the conversion options in more detail here:https://www.replrebl.com/article/displa ... -displays/ if you need them. But "GIMP -> Set dimensions -> Index 1bit image -> Export pbm -> Binary" should get you there :)

Post Reply