Skip to content

Instantly share code, notes, and snippets.

@JettMonstersGoBoom
Created September 19, 2020 00:44
Show Gist options
  • Save JettMonstersGoBoom/bf66d52a92e145d759576059b7a46649 to your computer and use it in GitHub Desktop.
Save JettMonstersGoBoom/bf66d52a92e145d759576059b7a46649 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageSequence
import array as arr
#assume gif is 4 colors ( and the 1st 4 )
#assume gif is 128x128
def frame2nes(input,basename,index):
(w, h) = input.size
# parse image by 8x8's
outname = basename + "_" + str(index) + ".chr"
chrFile = open(outname, "wb")
# for height in steps of 8
for y in range(0, h, 8):
# for width in steps of 8
for x in range(0, w, 8):
# clear the bitset
bitset0=[]
bitset1=[]
# for each pixel Y
for iy in range(0,8):
# reset the byte
bitsa = 0
bitsb = 0
# for each pixel X
for ix in range(0,8):
# flip the index
fx = 7-ix
# get the color ( we assume 4 color gif )
pixel = input.getpixel((x+ix,y+iy))
# set the bits
if pixel&1==1:
bitsa|=1<<fx
if pixel&2==2:
bitsb|=1<<fx
# store the bytes per Y
bitset0.append(bitsa)
bitset1.append(bitsb)
# save the set out as bytes
# nes stores bitplanes 8 bytes per plane * 2
for iy in range(0,8):
chrFile.write(bitset0[iy].to_bytes(1,byteorder='big'))
for iy in range(0,8):
chrFile.write(bitset1[iy].to_bytes(1,byteorder='big'))
chrFile.close()
def gif2nes(name):
newname = name + ".gif"
im = Image.open(newname)
index = 0
for frame in ImageSequence.Iterator(im):
frame2nes(frame,name,index)
index += 1
gif2nes("runnes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment