Skip to content

Commit d219fcc

Browse files
authored
Merge pull request #4630 from radarhere/decoder
Use _accept function in example plugin
2 parents a995cc0 + 3b62151 commit d219fcc

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

docs/handbook/writing-your-own-file-decoder.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ itself. Such plug-ins usually have names like
1717
Pillow decodes files in 2 stages:
1818

1919
1. It loops over the available image plugins in the loaded order, and
20-
calls the plugin's ``accept`` function with the first 16 bytes of
21-
the file. If the ``accept`` function returns true, the plugin's
20+
calls the plugin's ``_accept`` function with the first 16 bytes of
21+
the file. If the ``_accept`` function returns true, the plugin's
2222
``_open`` method is called to set up the image metadata and image
2323
tiles. The ``_open`` method is not for decoding the actual image
2424
data.
@@ -53,19 +53,19 @@ true color.
5353

5454
from PIL import Image, ImageFile
5555

56+
57+
def _accept(prefix):
58+
return prefix[:4] == b"SPAM"
59+
60+
5661
class SpamImageFile(ImageFile.ImageFile):
5762

5863
format = "SPAM"
5964
format_description = "Spam raster image"
6065

6166
def _open(self):
6267

63-
# check header
64-
header = self.fp.read(128)
65-
if header[:4] != b"SPAM":
66-
raise SyntaxError("not a SPAM file")
67-
68-
header = header.split()
68+
header = self.fp.read(128).split()
6969

7070
# size in pixels (width, height)
7171
self._size = int(header[1]), int(header[2])
@@ -86,7 +86,7 @@ true color.
8686
("raw", (0, 0) + self.size, 128, (self.mode, 0, 1))
8787
]
8888

89-
Image.register_open(SpamImageFile.format, SpamImageFile)
89+
Image.register_open(SpamImageFile.format, SpamImageFile, _accept)
9090

9191
Image.register_extension(SpamImageFile.format, ".spam")
9292
Image.register_extension(SpamImageFile.format, ".spa") # dos version

0 commit comments

Comments
 (0)