From 67d26ed051a4a12c2203731323f9dd62db862108 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 May 2020 09:24:06 +1000 Subject: [PATCH] Use _accept function in example plugin [ci skip] --- docs/handbook/writing-your-own-file-decoder.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/handbook/writing-your-own-file-decoder.rst b/docs/handbook/writing-your-own-file-decoder.rst index 24e46ff00..4be34eefc 100644 --- a/docs/handbook/writing-your-own-file-decoder.rst +++ b/docs/handbook/writing-your-own-file-decoder.rst @@ -53,6 +53,11 @@ true color. from PIL import Image, ImageFile + + def _accept(prefix): + return prefix[:4] == b"SPAM" + + class SpamImageFile(ImageFile.ImageFile): format = "SPAM" @@ -60,12 +65,7 @@ true color. def _open(self): - # check header - header = self.fp.read(128) - if header[:4] != b"SPAM": - raise SyntaxError("not a SPAM file") - - header = header.split() + header = self.fp.read(128).split() # size in pixels (width, height) self._size = int(header[1]), int(header[2]) @@ -86,7 +86,7 @@ true color. ("raw", (0, 0) + self.size, 128, (self.mode, 0, 1)) ] - Image.register_open(SpamImageFile.format, SpamImageFile) + Image.register_open(SpamImageFile.format, SpamImageFile, _accept) Image.register_extension(SpamImageFile.format, ".spam") Image.register_extension(SpamImageFile.format, ".spa") # dos version