import os, sys import Image in_width = 500 in_height = 100 out_width = 1024 out_height = 600 horiz_space = 16 vert_space = 16 in_dir = sys.argv[1] out_dir = sys.argv[2] count = 0 while True: count += 1 frames = [] while True: infile = '{}\\pokey{}_{}.gif'.format(in_dir, count, len(frames) + 1) if not os.path.isfile(infile): break frames.append(Image.open(infile).convert("RGB")) if len(frames) == 0: break if len(frames) > 12: print "skipping comic %d with %d frames" % (count, len(frames)) continue outimg = Image.new("RGB", (out_width, out_height), (255, 255, 255)) # if len(frames) <= 3: # # one column, 2x zoom # height = len(frames) * in_height * 2 + (len(frames) - 1) * vert_space * 2 # if height > out_height: # height = out_height # top = (out_height - height) / 2 # left = (out_width - in_width * 2) / 2 # for i in range(0, len(frames)): # outimg.paste(frames[i].resize((in_width * 2, in_height * 2)), (left, top + height * i / len(frames))) if len(frames) <= 6: # one column height = len(frames) * in_height + (len(frames) - 1) * vert_space if height > out_height: height = out_height top = (out_height - height) / 2 left = (out_width - in_width) / 2 for i in range(0, len(frames)): outimg.paste(frames[i], (left, top + height * i / len(frames))) else: # two column middle = (len(frames) + 1) / 2 height = middle * in_height + (middle - 1) * vert_space if height > out_height: height = out_height top = (out_height - height) / 2 left = (out_width - (in_width * 2 + horiz_space)) / 2 for i in range(0, middle): outimg.paste(frames[i], (left, top + height * i / middle)) left += in_width + horiz_space for i in range(0, len(frames) - middle): outimg.paste(frames[i + middle], (left, top + height * i / middle)) outfile = '{}\\pokey{}.jpg'.format(out_dir, count) outimg.save(outfile, "JPEG", quality=95) print "created %s (%d frames)" % (outfile, len(frames))