def _png_to_nximg(data, image_format): data_nximg = bytes() with BytesIO(data) as io_png: with BytesIO() as io_nximg: png = PngImageFile(io_png) w, h = png.width, png.height if image_format == IMAGE_FORMAT_1555: for y in range(h): for x in range(w): [r, g, b, a] = png.getpixel((x, y)) IOHelper.write_struct(io_nximg, "<2B", *NXColor.to_1555(r, g, b, a)) elif image_format == IMAGE_FORMAT_4444: for y in range(h): for x in range(w): [r, g, b, a] = png.getpixel((x, y)) IOHelper.write_struct(io_nximg, "<2B", *NXColor.to_4444(r, g, b, a)) elif image_format == IMAGE_FORMAT_8888: for y in range(h): for x in range(w): [r, g, b, a] = png.getpixel((x, y)) IOHelper.write_struct(io_nximg, "<4B", b, g, r, a) else: raise Exception('Unsupport image format: %s' % image_format) data_nximg = IOHelper.read_range(io_nximg) return data_nximg, w, h
def ex_oxygen(img): from PIL.PngImagePlugin import PngImageFile as Png coreimg = Png(img) w = coreimg.getbbox()[2] h = coreimg.getbbox()[3] // 2 chars = [coreimg.getpixel((i, h))[0] for i in range(0, w, 7)] message = ''.join(map(chr, chars)) result = ''.join(map(lambda x: chr(int(x)), message[43:-4].split(','))) return 7, result
def indexes_from_image(image: PngImageFile, sprite_idx: int) -> list: assert (0 <= sprite_idx <= 255) sprite_x = (sprite_idx % 16) * 8 sprite_y = (sprite_idx // 16) * 8 assert (0 <= sprite_x <= (128 - 8)) assert (0 <= sprite_y <= (128 - 8)) ret = [0 for _ in range(0, 64)] for y in range(0, 8): for x in range(0, 8): ret[y * 8 + x] = image.getpixel((x + sprite_x, y + sprite_y)) assert (len(ret) == 64) return ret