Permalink
Browse files

dmdconvert.py now supports multiple source files on command line. Ign…

…ores missing files (so that a starting offset is not needed). Better recognition of %03x style format specifiers.
  • Loading branch information...
preble committed Apr 25, 2010
1 parent 3d86dff commit 8482d6009a3b81f8624ef620a60126aafdcad8d2
Showing with 54 additions and 34 deletions.
  1. +54 −34 tools/dmdconvert.py
View
@@ -5,6 +5,7 @@
import time
#import pygame.image
import Image
import re
class ImageSequence:
"""Iterates over all images in a sequence (of PIL Images)."""
@@ -56,51 +57,70 @@ def gif_frames(src):
return frames
def image_to_dmd(src_filename, dst_filename):
"""docstring for image_to_dmd"""
last_filename = None
anim = procgame.dmd.Animation()
for frame_index in range(1000):
if '%d' in src_filename:
filename = src_filename % (frame_index)
else:
filename = src_filename
if filename == last_filename:
break
if not os.path.exists(filename):
break
print "Appending ", filename
src = Image.open(filename)
last_filename = filename
def load_and_append(anim, filename):
if not os.path.exists(filename):
#print "Not found:", filename
return False
print "Appending ", filename
src = Image.open(filename)
last_filename = filename
(w, h) = src.size
if len(anim.frames) > 0 and (w != anim.width or h != anim.height):
print "ERROR: Image sizes must be uniform! Anim is %dx%d, image is %dx%d" % (w, h, anim.width, anim.height)
sys.exit(1)
(anim.width, anim.height) = (w, h)
if filename.endswith('.gif'):
anim.frames += gif_frames(src)
else:
reduced = src.convert("L") #.quantize(palette=pal_im).convert("P", palette=Image.ADAPTIVE, colors=4)#
(w, h) = src.size
(anim.width, anim.height) = (w, h)
frame = procgame.dmd.Frame(w, h)
if filename.endswith('.gif'):
anim.frames += gif_frames(src)
else:
reduced = src.convert("L") #.quantize(palette=pal_im).convert("P", palette=Image.ADAPTIVE, colors=4)#
frame = procgame.dmd.Frame(w, h)
for x in range(w):
for y in range(h):
color = int((reduced.getpixel((x,y))/255.0)*15)
frame.set_dot(x=x, y=y, value=color)
anim.frames += [frame]
for x in range(w):
for y in range(h):
color = int((reduced.getpixel((x,y))/255.0)*15)
frame.set_dot(x=x, y=y, value=color)
anim.frames += [frame]
return True
def image_to_dmd(src_filenames, dst_filename):
"""docstring for image_to_dmd"""
last_filename = None
anim = procgame.dmd.Animation()
if len(src_filenames) == 1 and re.search("%[0-9]*d", src_filenames[0]):
pattern = src_filenames[0]
src_filenames = []
for frame_index in range(1000):
src_filenames.append(pattern % (frame_index))
for filename in src_filenames:
load_and_append(anim=anim, filename=filename)
if len(anim.frames) == 0:
print "ERROR: No frames found!"
sys.exit(1)
anim.save(dst_filename)
print "Saved."
def main():
if len(sys.argv) < 3:
print("Usage: %s <image.png> <output.dmd>"%(sys.argv[0]))
print(" image.png may include %d format specifiers to create animations. Example:")
print("Usage: %s <image1.png> [... <imageN.png>] <output.dmd>"%(sys.argv[0]))
print(" If only one image name is used it may include %d format specifiers to")
print(" create animations. Example:")
print(" %s Animation%%03d.png Animation.dmd" % (sys.argv[0]))
print(" Creates an animation of up to 999 frames with sequential names.")
print(" ")
print(" Note that in UNIX-like shells that support wildcard expansion you can")
print(" enter image*.png as the one image filename and the shell will expend it")
print(" to include all filenames matching that wildcard.")
return
image_to_dmd(src_filename=sys.argv[1], dst_filename=sys.argv[2])
image_to_dmd(src_filenames=sys.argv[1:-1], dst_filename=sys.argv[-1])
if __name__ == "__main__":

0 comments on commit 8482d60

Please sign in to comment.