Python Forum
Help me get this image converter code working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me get this image converter code working?
#1
Hello, I have been trying to convert 8-bit images into the proper binary format required by the Arduino IDE library I am using. The library included this python code for converting the images, and I have tried to get it to run with no success. I have had minor success with the software created by MIT called, "The Dot Factory" but the closest I have come to displaying my image correctly, is still unacceptable. The python image converter code below must be mixing up the data in some strange way to display it correctly on my SSD1306 OLED display. Here you can see my best attempt with The Dot Factory converting my image to binary:(the yellow/blue color is normal for this 2-color display)

[Image: Image-Display-Failure.jpg]

Here is the original image before binary conversion:

[Image: fbtest.png]

This SSD1306 display has 8 pages of memory, each page refers to a row of 8 pixels on the display. Somehow these rows or pages in memory are being swapped around, and exactly how it is swapping them should tell me what I need to do to my binary image data to make it compatible, but I am at a loss by now. I have tried changing the bit-order and layout in The Dot Factory, but I am thinking the setting I need is not available in this software. The above image was converted with these settings:

[Image: The-Dot-Factory-Settings.png]

If any kind soul has some recommendation for me to explore, I would be very grateful. If I cannot solve this, I will have to go back to the drawing board to find another suitable library for my Teensy 3.2 and these SSD1306 I2C displays. "EvilOLED" was the first library I found that actually worked; I didn't realize it'd be so difficult to find one. The reason it is difficult is that the breakout board for the display has no "RST" reset pin, which all the other SSD1306 libraries require. Anyhow, here is that code for the image converter written in python, that I could not get working:

#You must add numpy and Image packages to Python and verify your Python install
#Syntax: c:\Scripts\Python> converter.py <yourFileName>
import sys
import numpy
from PIL import Image

im = Image.open(sys.argv[1])
im = im.convert("1") 
rows = (im.size[1] // 8)
cols = im.size[0]
val = ""

print("const int bmap[8]= {")

for p in range(rows):
    for xx in range(cols):
        for yy in range(8):
            pix = im.getpixel((xx,(p * 8) + yy))
            if (pix == 0):
                val = "1" + val
            else:
                val = "0" + val
            val = "B" + val
            print(val + ",", end='')
            val = ""
            if (xx % 8 == 7):
                print("")

print("}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  currency converter using forex-python preethy12ka4 8 548 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 908 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 834 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 971 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 698 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,030 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  Help with basic weight converter PythonSquizzel 3 1,340 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  Correct the algorithm of image filter code saoko 6 1,922 May-08-2022, 05:06 PM
Last Post: saoko
  My Code isn't working... End3r 4 1,863 Mar-21-2022, 10:12 AM
Last Post: End3r
  code decode, string, image ... teckow 2 2,025 Aug-20-2021, 07:02 PM
Last Post: teckow

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020