def main(argv=None): import sys if argv is None: argv = sys.argv if (argvTestingMode and len(argv) < 2 and sys.stdin.isatty() and sys.stdout.isatty()): argv.extend(input('args:').split()) try: (infilename, outfilename, tileWidth, tileHeight, usePackBits, planes, hflip, little, rearrange16x16, columnMajor) = parse_argv(argv) except Exception as e: sys.stderr.write("%s: %s\n" % (argv[0], str(e))) raise sys.exit(1) im = Image.open(infilename) # Rearrange rows for the purpose of displaying a 32x32 metasprite out of 16x16 sprites on SNES if rearrange16x16: rearranged = im.copy() if im.height%32 != 0: raise ValueError("Image height must be a multiple of 32 if --rearrange-16x16 is on") for frame in range(im.height//32): base = frame*32 rearranged.paste(im.crop((0, base+16, im.width, base+16+8)), (0, base+8)) rearranged.paste(im.crop((0, base+8, im.width, base+8+8)), (0, base+16)) rearranged.paste(im.crop((0, base+24, im.width, base+24+8)), (0, base+24)) im.close() im = rearranged outdata = pilbmp2chr(im, tileWidth, tileHeight, columnMajor, lambda im: formatTilePlanar(im, planes, hflip, little)) outdata = b''.join(outdata) if usePackBits: from packbits import PackBits sz = len(outdata) % 0x10000 outdata = PackBits(outdata).flush().tostring() outdata = b''.join([chr(sz >> 8), chr(sz & 0xFF), outdata]) # Read input file outfp = None try: if outfilename != '-': outfp = open(outfilename, 'wb') else: outfp = sys.stdout make_stdout_binary() outfp.write(outdata) finally: if outfp and outfilename != '-': outfp.close()
def main(argv=None): import sys if argv is None: argv = sys.argv if (argvTestingMode and len(argv) < 2 and sys.stdin.isatty() and sys.stdout.isatty()): argv.extend(input('args:').split()) try: (infilename, outfilename, tileWidth, tileHeight, usePackBits, planes, hflip, little, addamt, addamt0) = parse_argv(argv) except Exception as e: sys.stderr.write("%s: %s\n" % (argv[0], str(e))) sys.exit(1) im = Image.open(infilename) # Subpalette shift if addamt or addamt0: px = bytearray(im.getdata()) for i in range(len(px)): thispixel = px[i] px[i] = thispixel + (addamt if thispixel else addamt0) im.putdata(px) outdata = pilbmp2chr( im, tileWidth, tileHeight, lambda im: formatTilePlanar(im, planes, hflip, little)) outdata = b''.join(outdata) if usePackBits: from packbits import PackBits sz = len(outdata) % 0x10000 outdata = PackBits(outdata).flush().tostring() outdata = b''.join([chr(sz >> 8), chr(sz & 0xFF), outdata]) # Write output file outfp = None try: if outfilename != '-': outfp = open(outfilename, 'wb') else: outfp = sys.stdout make_stdout_binary() outfp.write(outdata) finally: if outfp and outfilename != '-': outfp.close()
def main(argv=None): import sys if argv is None: argv = sys.argv if (argvTestingMode and len(argv) < 2 and sys.stdin.isatty() and sys.stdout.isatty()): argv.extend(raw_input('args:').split()) try: (infilename, outfilename, tileWidth, tileHeight, usePackBits, planes, hflip, little) = parse_argv(argv) except Exception as e: sys.stderr.write("%s: %s\n" % (argv[0], str(e))) sys.exit(1) im = Image.open(infilename) outdata = pilbmp2chr( im, tileWidth, tileHeight, lambda im: formatTilePlanar(im, planes, hflip, little)) outdata = b''.join(outdata) if usePackBits: from packbits import PackBits sz = len(outdata) % 0x10000 outdata = PackBits(outdata).flush().tostring() outdata = b''.join([chr(sz >> 8), chr(sz & 0xFF), outdata]) # Read input file outfp = None try: if outfilename != '-': outfp = open(outfilename, 'wb') else: outfp = sys.stdout make_stdout_binary() outfp.write(outdata) finally: if outfp and outfilename != '-': outfp.close()
try: (infilename, outfilename, tileWidth, tileHeight, usePackBits, planes) = parse_argv(argv) except StandardError, e: sys.stderr.write("%s: %s\n" % (argv[0], str(e))) sys.exit(1) im = Image.open(infilename) outdata = pilbmp2chr(im, tileWidth, tileHeight, lambda im: formatTilePlanar(im, planes)) outdata = ''.join(outdata) if usePackBits: from packbits import PackBits sz = len(outdata) % 0x10000 outdata = PackBits(outdata).flush().tostring() outdata = ''.join([chr(sz >> 8), chr(sz & 0xFF), outdata]) # Read input file outfp = None try: if outfilename != '-': outfp = open(outfilename, 'wb') else: outfp = sys.stdout outfp.write(outdata) finally: if outfp and outfilename != '-': outfp.close() if __name__=='__main__':
def main(argv=None): global printStats import sys from pilbmp2nes import formatTilePlanar, pilbmp2chr if argv is None: argv = sys.argv if (argvTestingMode and len(argv) < 2 and sys.stdin.isatty() and sys.stdout.isatty()): argv.extend(raw_input('args:').split()) try: (infilename, outfilename, maxTiles, useDims, chrCodec, useIndexRLE, printStats) = parse_argv(argv) except Exception as e: sys.stderr.write("%s: %s\n" % (argv[0], str(e))) sys.exit(1) if printStats: print("filename: %s" % infilename, file=sys.stderr) im = Image.open(infilename) (w, h) = im.size if printStats: print >> sys.stderr, "size: %dx%d" % im.size if w % 8 != 0: raise ValueError("image width %d is not a multiple of 8" % w) if h % 8 != 0: raise ValueError("image height %d is not a multiple of 8" % h) ochrdata = pilbmp2chr(im, 8, 8, lambda im: formatTilePlanar(im, 2)) del im (chrdata, ntdata) = dedupe_chr(ochrdata) numTiles = len(chrdata) if numTiles > maxTiles: raise ValueError("%d distinct tiles exceed maximum %d" % (numTiles, maxTiles)) ochrdata = ''.join(ochrdata) chrdata = ''.join(chrdata) if printStats: print("CHR size before dedupe: %d" % len(ochrdata), file=sys.stderr) print("distinct tiles: %d of %d" % (numTiles, len(ntdata)), file=sys.stderr) print("unpacked CHR size: %d" % len(chrdata), file=sys.stderr) if useIndexRLE: cntdata = compress_nt(ntdata) if printStats: print("compressed nametable size: %s" % len(cntdata), file=sys.stderr) ntdata = cntdata else: ntdata = ''.join(chr(x) for x in ntdata) if chrCodec == 'packbits': from packbits import PackBits sz = len(chrdata) % 0x10000 pchrdata = PackBits(chrdata).flush().tostring() pchrdata = bytes([chr(sz >> 8), chr(sz & 0xFF)]) + pchrdata if printStats: print("packed CHR size: %d" % len(pchrdata), file=sys.stderr) chrdata = pchrdata elif chrCodec == 'pb8': from pb8 import pb8 sz = len(chrdata) // 16 pchrdata = pb8(chrdata) pchrdata = bytes([sz & 0xFF]) + pchrdata if printStats: print("packed CHR size: %d" % len(pchrdata), file=sys.stderr) chrdata = pchrdata if useDims: dimsdata = bytes([w // 8, h // 8, numTiles & 0xFF, 0]) else: dimsdata = '' outdata = b''.join([dimsdata, ntdata, chrdata]) # Write output file outfp = None try: if outfilename != '-': outfp = open(outfilename, 'wb') else: outfp = sys.stdout outfp.write(outdata) finally: if outfp and outfilename != '-': outfp.close()