Created
December 30, 2017 00:07
-
-
Save u1735067/31a755a084b533a61de17302c81c45a6 to your computer and use it in GitHub Desktop.
Test
This file contains hidden or 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 sys, io, PIL, PIL.Image, PIL.ImageFile | |
from PIL import Image | |
from struct import unpack, pack | |
''' | |
https://gbatemp.net/threads/open-a-ttp-ttpf-image-vc-edit.442031/ | |
https://pillow.readthedocs.io/en/4.3.x/reference/Image.html?highlight=rgba#PIL.Image.frombytes | |
https://pillow.readthedocs.io/en/4.3.x/handbook/writing-your-own-file-decoder.html#file-decoders | |
https://pillow.readthedocs.io/en/stable/handbook/writing-your-own-file-decoder.html | |
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.register_decoder | |
https://pillow.readthedocs.io/en/latest/reference/ImageFile.html | |
https://pillow.readthedocs.io/en/4.3.x/handbook/concepts.html#concept-modes | |
https://pillow.readthedocs.io/en/3.4.x/handbook/image-file-formats.html | |
https://stackoverflow.com/questions/3397157/how-to-read-a-raw-image-using-pil | |
https://en.wikipedia.org/wiki/Endianness | |
https://docs.python.org/3/library/struct.html | |
https://stackoverflow.com/questions/7983684/how-to-switch-byte-order-of-binary-data | |
https://stackoverflow.com/questions/846038/convert-a-python-int-into-a-big-endian-string-of-bytes | |
https://stackoverflow.com/questions/1934624/item-assignment-on-bytes-object-in-python | |
https://imgur.com/a/5SEEo | |
''' | |
class Test(PIL.ImageFile.PyDecoder): | |
def decode(self, buffer): | |
print(self.mode) | |
buffer = bytearray(buffer) | |
i = 0 | |
while i < len(buffer) or (i > 0 and i+4 < len(buffer)): | |
buffer[i:i+4] = pack('>I', unpack('<I', buffer[i:i+4])[0]) | |
i = i+4 | |
self.set_as_raw(bytes(buffer)) | |
print(i, len(buffer)-i-1) | |
return (len(buffer)-i-1, 0) | |
PIL.Image.register_decoder('Test', Test) | |
with open('./face_cgb.ttp', 'rb') as f: | |
f.seek(0x1c) | |
size = unpack('<HH', f.read(4)) | |
img = Image.frombytes('RGBA', size, f.read(), 'Test') | |
with open('test.png', 'wb') as out: | |
img.save(out) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment