Last active
April 25, 2020 17:01
-
-
Save tripulse/6b55ded4b0643a952fde7f9b9b8c90af 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
""" | |
Plugin for both encoding and decoding Suckless Farbfeld files | |
for the PIL library (compression not supported). | |
""" | |
from PIL import Image | |
from PIL.ImageFile import ImageFile | |
from struct import unpack, pack | |
class FarbfeldImageFile(ImageFile): | |
format = 'farbfeld' | |
format_description = 'Suckless Farbfeld' | |
def _open(self): | |
if self.fp.read(8) != b'farbfeld': | |
raise SyntaxError('not a Farbfeld file!') | |
self._size = unpack('>II', self.fp.read(8)) | |
self.mode = 'RGBA' # always 32bit color. | |
self.tile = [('raw', (0,0,*self.size), 16, (self.mode,0,1))] | |
@staticmethod | |
def _save(self, fp, filename): | |
self = self.convert('RGBA') | |
fp.write(b'farbfeld' + pack('>II', *self.size)) | |
fp.write(self.tobytes()) | |
Image.register_open('farbfeld', FarbfeldImageFile) | |
Image.register_save('farbfeld', FarbfeldImageFile._save) | |
# NOTE: this mimetype is not registered with IANA, asking the author | |
# 'image/farbfeld' was recommended. | |
Image.register_extension('farbfeld', '.ff') | |
Image.register_mime('farbfeld', 'image/farbfeld') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment