Convert images to .raw programs

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
User avatar
nibheis
Posts: 13
Joined: Tue Feb 05, 2019 2:08 pm
Location: FR/CH/ES

Convert images to .raw programs

Post by nibheis »

Hi all,

To follow up on this thread, here is a python3 script that turns any 400x240 image into a .raw program for the DM42 (and free42).

It is basically one single class (Img2Hex) - you only need PIL or pillow Python package to use it.
Here's how to use it:

Code: Select all

from img2hex import Img2Hex

hexcode = Img2Hex(filename = 'matterhorn.bmp', label = 'MATT', negative = False)
hexcode.toRawFile('MATT.raw')
Assuming that 'matterhorn.bmp' is in the same directory, you will get a 'MATT.raw' program for your DM42, with LBL MATT.

You can also add payload into the program, like this:

Code: Select all

    # Prepare payload
    pl = {
        "V1": 1234,
        "V2": -3.1415,
        "VA": "Hello"
    }
    hx = Img2Hex(filename = 'matterhorn.bmp', label = 'MATT', negative = False, verbose = False, payload = pl)
When you execute the program on the DM42, the "V1", "V2" and "VA" variables are set to the corresponding values.
(I use payloads while creating charts for the DM42: the payload contains the scale, center coordinates and name of the displayed chart - more on this chart plotting app in another thread soon.)

Enjoy !
Attachments
img2hex.zip
(2.72 KiB) Downloaded 292 times
cappy
Posts: 93
Joined: Sun Jul 30, 2017 7:30 pm

Re: Convert images to .raw programs

Post by cappy »

nibheis wrote:
Wed Jun 12, 2019 5:26 pm
Hi all,

To follow up on this thread, here is a python3 script that turns any 400x240 image into a .raw program for the DM42 (and free42).
Hi

I'm curious why you declared class variables at all, not to mention why
you declared some with the same as the variable names being passed in the Img2Hex.__init__() call?


class Img2Hex:
label = None
image = None
pix = None
w = None
h = None
b = None
oc = None
verbose = False
negative = False
payload = None


def __init__(self,
filename = None,
label = None,
verbose = False,
negative = False,
width = 400,
height = 240,
payload = None):


Class variables are only needed when you're sharing those variables among all
instances of the same class.

From what I see, there is only 1 instance of this class.

Not a problem, just curious
DM42 SN: 0612
Post Reply