shepherdingelectrons

Generate 16x16 RGB images for RAM

Mar 28th, 2021 (edited)
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # Generates a Python file ("RAMimages.py") with bytearrays from a list of images.
  2. # The loop opens an image in the list, then resamples it for a 16x16 image, and exacts
  3. # the RGB data in the necessary way for injecting into the RAM on the 8-bit computer later.
  4.  
  5. from PIL import Image
  6.  
  7. def RAMaddress(x,y):
  8.     if x&1==0: # even columns
  9.         addr = (x*16)+y
  10.     else:   # odd columns
  11.         addr = (x*16)+(15-y)
  12.     return 3*addr
  13.  
  14. def resample(px,py):
  15.     # px must be between 0 and wn-1
  16.     # py must be between 0 and hn-1
  17.     wn = 16
  18.     hn = 16
  19.     w_div = im.width/wn
  20.     h_div = im.height/wn
  21.  
  22.     spx = int(w_div * (px+0.5))
  23.     spy = int(h_div * (py+0.5))
  24.  
  25.     return im.getpixel((spx,spy))
  26.    
  27. #Read image
  28. folder = ""
  29. imagelist = ["mariohead.png", "mario.png","toadracer.png","gameboy.png","rainbow_square.png"]
  30.  
  31. f = open(folder+"RAMimages.py","w") # Generates a Python file with bytearrays that can be imported
  32.  
  33. for image in imagelist:
  34.     im = Image.open( folder+image)
  35.     im = im.convert('RGB')
  36.  
  37.     RAMsize = 256*3
  38.     RAM = bytearray(RAMsize)
  39.  
  40.     for x in range(0,16):
  41.         for y in range(0,16):
  42.             r,g,b = resample(x,y)
  43.             RAMaddr = RAMaddress(x,y)
  44.  
  45.             RAM[RAMaddr]=g
  46.             RAM[RAMaddr+1]=r
  47.             RAM[RAMaddr+2]=b
  48.     name=image.split('.')[0]
  49.    
  50.     f.write(name+"="+str(RAM)+"\n")
  51. f.close()
  52.  
  53.  
  54.  
Add Comment
Please, Sign In to add comment