def convert_nametable(image, sprs, palette = palette): pixels = image.load() colors = [] for i in range(image.size[0]): for j in range(image.size[1]): if pixels[i,j] not in colors: colors.append(pixels[i,j]) assert len(colors) == 4, "Image has %i colors, it can only have 4" % len(colors) assert image.size[0] % 8 == 0 assert image.size[1] % 8 == 0 default = ( (0,0,0) in colors and (255,0,0) in colors and (0,255,0) in colors and (0,0,255) in colors ) if default: nes_palette = palette #todo nes_palette = palette nametable = [] if sprite.length(sprs) == 512: start = 256 else: start = 0 for y in range(image.size[0] / 8 ): for x in range(image.size[1] / 8 ): spr = fetch_chr(pixels, y, x, nes_palette) index = sprite.find_sprite(sprs, spr, start) nametable.append(index) return nametable
def read_nametable(image, sprs, palette=palette): pixels = image.load() colors = get_colors(image) default = ( (0, 0, 0) in colors and (255, 0, 0) in colors and (0, 255, 0) in colors and (0, 0, 255) in colors ) if default: nes_palette = palette else: nes_palette = colors nametable = [] # TODO huge stealing here if sprite.length(sprs) == 512: start = 256 else: start = 0 if isinstance(sprs, tuple): sprs = sprs[0] for y in range(image.size[0] / 8): for x in range(image.size[1] / 8): spr = fetch_chr(pixels, y, x, nes_palette) index = sprite.find_sprite(sprs, spr, start) if index != -1: nametable.append(index) else: show_sprite(spr) raise Exception('Sprite not found') # TODO: # encoded = sprite.encode_sprite(spr) # key = ''.join([chr(e) for e in encoded]) # if key in sprs: # if key > 256: # show_sprite(spr) # pass # print sprs[key] # print sprs[key] # nametable.append(sprs[key]) # else: # show_sprite(spr) # print x # print y # print '===' + key + '====' # raise Exception('Sprite not found') return nametable
def read_nametable(image, sprs, palette=palette): pixels = image.load() colors = get_colors(image) default = ((0, 0, 0) in colors and (255, 0, 0) in colors and (0, 255, 0) in colors and (0, 0, 255) in colors) if default: nes_palette = palette else: nes_palette = colors nametable = [] # TODO huge stealing here if sprite.length(sprs) == 512: start = 256 else: start = 0 if isinstance(sprs, tuple): sprs = sprs[0] for y in range(image.size[0] / 8): for x in range(image.size[1] / 8): spr = fetch_chr(pixels, y, x, nes_palette) index = sprite.find_sprite(sprs, spr, start) if index != -1: nametable.append(index) else: show_sprite(spr) raise Exception('Sprite not found') # TODO: # encoded = sprite.encode_sprite(spr) # key = ''.join([chr(e) for e in encoded]) # if key in sprs: # if key > 256: # show_sprite(spr) # pass # print sprs[key] # print sprs[key] # nametable.append(sprs[key]) # else: # show_sprite(spr) # print x # print y # print '===' + key + '====' # raise Exception('Sprite not found') return nametable
def export_chr(chr_file, png_file, palette=palette, width=8): sprs = sprite.load_sprites(chr_file) spr_len = sprite.length(sprs) height = spr_len / width size = (width * 8, height * 8) img = Image.new('RGB', size) draw = ImageDraw.Draw(img) for s_index in range(spr_len): spr = sprite.get_sprite(s_index, sprs) dx = s_index % width dy = s_index / width for y in range(8): for x in range(8): color = spr[y][x] draw.point((x+(8*dx),y+(8*dy)), palette[color]) img.save(png_file, 'PNG')