Last active
September 9, 2019 21:26
-
-
Save creallfluharty/1c26bcd42b82760f4e419a41679ef795 to your computer and use it in GitHub Desktop.
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 io | |
import requests | |
from PIL import Image | |
from itertools import chain | |
BIGCHUNK_CHUNK_WIDTH = 15 | |
CHUNK_PIXEL_WIDTH = 64 | |
BYTE_BIT_SIZE = 8 | |
COLOR_BIT_SIZE = 4 | |
CHUNK_BYTE_SIZE = int((CHUNK_PIXEL_WIDTH ** 2) * (COLOR_BIT_SIZE / BYTE_BIT_SIZE)) | |
BIGCHUNK_PIXEL_WIDTH = BIGCHUNK_CHUNK_WIDTH * CHUNK_PIXEL_WIDTH | |
def get_rect(x, y, width, height): | |
return (x, y, x+width, y+height) | |
def get_big_chunk(pixel_x, pixel_y): | |
chunk_x = pixel_x // CHUNK_PIXEL_WIDTH | |
chunk_y = pixel_y // CHUNK_PIXEL_WIDTH | |
data = requests.get(f'https://api.pixelcanvas.io/api/bigchunk/{chunk_x}.{chunk_y}.bmp') | |
return data.content | |
pallet_colors = [ | |
(255, 255, 255), # White | |
(228, 228, 228), # Light Grey | |
(136, 136, 136), # Dark Grey | |
( 34, 34, 34), # Black | |
(255, 167, 209), # Pink | |
(229, 0, 0), # Red | |
(229, 149, 0), # Orange | |
(160, 106, 66), # Brown | |
(229, 217, 0), # Yellow | |
(148, 224, 68), # Light Green | |
( 2, 190, 1), # Green | |
( 0, 211, 221), # Cyan | |
( 0, 131, 199), # Teal | |
( 0, 0, 234), # Blue | |
(207, 110, 228), # Light Purple | |
(130, 0, 128) # Purple | |
] | |
image = Image.new('RGB', (BIGCHUNK_PIXEL_WIDTH,)*2) | |
data = get_big_chunk(960, 960) | |
chunk_pixel_offsets = range(0, BIGCHUNK_PIXEL_WIDTH, CHUNK_PIXEL_WIDTH) | |
with io.BytesIO(data) as bio: | |
for y_offset in chunk_pixel_offsets: | |
for x_offset in chunk_pixel_offsets: | |
img = Image.frombuffer('P', (CHUNK_PIXEL_WIDTH,)*2, bio.read(CHUNK_BYTE_SIZE), 'raw', f'P;{COLOR_BIT_SIZE}') | |
img.putpalette(list(chain(*pallet_colors))) | |
image.paste(img, get_rect(x_offset, y_offset, *(CHUNK_PIXEL_WIDTH,)*2)) | |
image.save('attempt.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adapted from:
https://github.com/DiamondIceNS/StarlightGlimmer/blob/master/objects/chunks.py#L88
http://effbot.org/zone/creating-palette-images.htm