Skip to content

Commit

Permalink
Add PIL docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Sep 11, 2023
1 parent 45d1624 commit 391435b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tivars/PIL/TI8caPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@


class TI8caImageFile(TIImageFile):
"""
`ImageFile` handler for 8ca files (`TIImage`)
"""

_T = TIImage

format = "8ca"
format_description = "TI (e)Z80 Image Format"


class TI8caEncoder(TIEncoder):
"""
Encoder for 8ca files (`TIImage`)
"""

_T = TIImage


register(TI8caImageFile, TI8caEncoder)

__all__ = ["TI8caEncoder", "TI8caImageFile"]
10 changes: 10 additions & 0 deletions tivars/PIL/TI8ciPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@


class TI8ciImageFile(TIImageFile):
"""
`ImageFile` handler for 8ci files (`TIPicture`)
"""

_T = TIPicture

format = "8ci"
format_description = "TI (e)Z80 Color Picture Format"


class TI8ciEncoder(TIEncoder):
"""
Encoder for 8ci files (`TIPicture`)
"""

_T = TIPicture


register(TI8ciImageFile, TI8ciEncoder)

__all__ = ["TI8ciEncoder", "TI8ciImageFile"]
10 changes: 10 additions & 0 deletions tivars/PIL/TI8xiPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@


class TI8xiImageFile(TIImageFile):
"""
`ImageFile` handler for 8xi files (`TIMonoPicture`)
"""

_T = TIMonoPicture

format = "8xi"
format_description = "TI (e)Z80 Monochrome Picture Format"


class TI8xiEncoder(TIEncoder):
"""
Encoder for 8xi files (`TIMonoPicture`)
"""

_T = TIMonoPicture


register(TI8xiImageFile, TI8xiEncoder)

__all__ = ["TI8xiEncoder", "TI8xiImageFile"]
41 changes: 41 additions & 0 deletions tivars/PIL/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ def register(file, encoder):


class TIImageFile(ImageFile.ImageFile):
"""
Base class for PIL plugin `ImageFile` types
"""

_T = PictureEntry

format = "8??"

def _open(self):
"""
Loads necessary image information from an opened file
"""

with warnings.catch_warnings():
warnings.simplefilter("error")

Expand All @@ -42,11 +50,33 @@ def _open(self):

@classmethod
def _save(cls, im, fp, format=None, **params):
"""
Saves an image to a file pointer
This function is used to register formats with PIL and should not be called manually.
:param im: The image to save
:param fp: The file pointer
:param format: The format to save with (defaults to the image's known format)
:param params: Additional encoder parameters (empty)
"""

ImageFile._save(im, fp, format, [(cls.format, (0, 0) + im.size, 0, im.mode)])


class TIDecoder(ImageFile.PyDecoder):
"""
Base class for PIL plugin decoders
"""

def decode(self, buffer):
"""
Decodes an input buffer and sets the image to its contents
:param buffer: The input bytestream
:return: The number of bytes consumed and the error code (``-1, 0`` on success)
"""

var = TIVar()
var.load_bytes(buffer)
self.set_as_raw(np.asarray(var.entries[0].array(), dtype=np.uint8))
Expand All @@ -55,11 +85,22 @@ def decode(self, buffer):


class TIEncoder(ImageFile.PyEncoder):
"""
Base class for PIL plugin encoders
"""

_pushes_fd = True

_T = PictureEntry

def encode(self, bufsize):
"""
Encodes the image to an output bytestream
:param bufsize: The size of the output buffer (unused)
:return: The length of the output, the error code (``0`` on success), and the output
"""

img = self._T()
img.load_array(np.asarray(self.im).reshape((img.height, img.width)).tolist())
data = img.export().bytes()
Expand Down

0 comments on commit 391435b

Please sign in to comment.