dMZX Forums: Image conversion Python scripts - dMZX Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Image conversion Python scripts

#1 User is offline   nooodl 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 735
  • Joined: 28-October 06
  • Gender:Not Telling
  • Location:Belgium

Posted 11 October 2011 - 08:49 PM

Monochrome image file to .chr file:

INFILE = 'infile.bmp'
OUTFILE = 'outfile' # .chr

from PIL import Image

im = Image.open(INFILE)
ch = open(OUTFILE + '.chr', 'wb')
assert im.size == (256, 112)
l = list(im.getdata())

for i in range(256):
    cx, cy = (i % 32) * 8, (i / 32) * 14
    for j in range(14):
        pos = (cy + j) * 256 + cx
        byte = l[pos:pos+8]
        byte = sum(int(x > 0) << 7 - i for (i, x) in enumerate(byte))
        ch.write(chr(byte))
ch.close()


Image to 80x50 .pal and .mzm:

INFILE = 'infile'
OUTFILE = 'outfile' # .mzm and .pal

import struct
from PIL import Image

def palettecolors(im):
    l = im.resize((16, 1))
    l.putdata(range(16))
    return l.convert('RGB').getdata()

im = Image.open(INFILE).convert('P', palette=Image.ADAPTIVE, colors=16)
width, height = im.size

# .pal
pal = []
for c in palettecolors(im):
    for i in c:
        pal.append(chr(i/4))
pal = ''.join(pal)
open(OUTFILE + '.pal', 'wb').write(pal)

# .mzm
mzm = open(OUTFILE + '.mzm', 'wb')
mzm.write('MZM2')
mzm.write(struct.pack('<2H', width, height / 2))
mzm.write('\0\0\0\0\0\1\0\0')

data = list(im.getdata())
for i in range(0, height, 2):
    top    = data[(i + 0) * width:(i + 1) * width]
    bottom = data[(i + 1) * width:(i + 2) * width]
    for bg, fg in zip(top, bottom):
        mzm.write(struct.pack('2B', 0xDC, fg + (bg << 4)))
mzm.close()


The latter is pretty specific, but hey. Also, you'll need PIL: http://www.pythonwar...m/products/pil/

This post has been edited by nooodl: 11 October 2011 - 08:50 PM

0

#2 User is offline   GetDizzy 

  • Touch Fuzzy.
  • Group: DigiStaff
  • Posts: 3,567
  • Joined: 22-November 01
  • Gender:Other
  • Location:MA

Posted 11 October 2011 - 09:08 PM

Totally using the top one in CharCon Redux. Why reinvent the wheel? :(
- Your Jumpy Neighborhood Admin

<@Tixus> Anyway, I set the year to 1988 for some reason.
<@Tixus> And set the microwave to run for a minute and 28 seconds.
<@Tixus> But it failed to send me back in time, and I was disappointed.
<Insidious> Tixus accidentally microwaved the 80s
<Insidious> that is my takeaway from this
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users