Last active
January 8, 2022 17:27
-
-
Save Bejofo/d5377956515af9134fdb0958e30f99c5 to your computer and use it in GitHub Desktop.
Convert a png image into into 2BPP format, useful for Gameboy development .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import skimage.io | |
import numpy as np | |
from skimage.color import rgb2gray | |
im = 255-skimage.io.imread("YOUR_IMAGE_HERE") | |
im = rgb2gray(im) | |
im_mono = np.zeros_like(im) | |
im_mono[im < 0.25] = 0 | |
im_mono[(0.25 < im ) & (im< 0.50)] = 1 | |
im_mono[(0.50 < im ) & (im< 0.75)] = 2 | |
im_mono[(0.75 < im )] = 3 | |
tiledata = [] | |
mapgb = np.zeros((18,20)) | |
def encode_tile(chunk): | |
ans = [] | |
assert chunk.shape == (8,8) | |
for row in chunk: | |
high,low = 0,0 | |
for idx,pix in enumerate(row): | |
if pix in (0,2): | |
low |= 1<< (7-idx) | |
if pix in (2,3): | |
high |= 1<<(7-idx) | |
ans+= [low,high] | |
return ans | |
for tileX in range(20): | |
for tileY in range(18): | |
chunk = im_mono[tileY*8:tileY*8+8,tileX*8:tileX*8+8] | |
encoded = encode_tile(chunk) | |
if encoded in tiledata: | |
mapgb[tileY,tileX] = tiledata.index(encoded) | |
else: | |
mapgb[tileY,tileX] = len(tiledata) | |
tiledata.append(encoded) | |
print(tiledata) | |
print(mapgb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment