我想找到如何从verify()库实现Pillow功能。在源代码中,我仅发现以下内容:

def verify(self):
    """
    Verifies the contents of a file. For data read from a file, this
    method attempts to determine if the file is broken, without
    actually decoding the image data.  If this method finds any
    problems, it raises suitable exceptions.  If you need to load
    the image after using this method, you must reopen the image
    file.
    """
    pass


在哪里可以找到实现?

(我在这里找到的源代码:Pillow source code

最佳答案

comment on GitHub解释:


  Image。[v] erify仅检查png文件中的块校验和,并且在其他地方禁止操作。


因此,简短的答案是您已经找到了绝对不执行任何操作的默认实现。

除了PNG文件,可以在PngImageFile.verify方法中找到其实现:

    def verify(self):
        "Verify PNG file"

        if self.fp is None:
            raise RuntimeError("verify must be called directly after open")

        # back up to beginning of IDAT block
        self.fp.seek(self.tile[0][2] - 8)

        self.png.verify()
        self.png.close()

        self.fp = None


依次通过self.png.verify()调用ChunkStream.verify

    def verify(self, endchunk=b"IEND"):

        # Simple approach; just calculate checksum for all remaining
        # blocks.  Must be called directly after open.

        cids = []

        while True:
            cid, pos, length = self.read()
            if cid == endchunk:
                break
            self.crc(cid, ImageFile._safe_read(self.fp, length))
            cids.append(cid)

        return cids


更详细的源代码细分

您已经为verify类的Image方法引用的代码显示,默认情况下它什么都不做:

class Image:

    ...

    def verify(self):
        """
        Verifies the contents of a file. For data read from a file, this
        method attempts to determine if the file is broken, without
        actually decoding the image data.  If this method finds any
        problems, it raises suitable exceptions.  If you need to load
        the image after using this method, you must reopen the image
        file.
        """
        pass


但是对于PNG文件,默认的verify方法会被覆盖,如ImageFile类的源代码所示,该方法继承自Image类:

class ImageFile(Image.Image):
    "Base class for image file format handlers."

    ...


以及从PngImageFile继承的PNG插件类ImageFile的源代码:

##
# Image plugin for PNG images.

class PngImageFile(ImageFile.ImageFile):

    ...


并具有verify的此替代实现:

    def verify(self):
        "Verify PNG file"

        if self.fp is None:
            raise RuntimeError("verify must be called directly after open")

        # back up to beginning of IDAT block
        self.fp.seek(self.tile[0][2] - 8)

        self.png.verify()
        self.png.close()

        self.fp = None


依次通过self.png.verify()调用ChunkStream.verify

    def verify(self, endchunk=b"IEND"):

        # Simple approach; just calculate checksum for all remaining
        # blocks.  Must be called directly after open.

        cids = []

        while True:
            cid, pos, length = self.read()
            if cid == endchunk:
                break
            self.crc(cid, ImageFile._safe_read(self.fp, length))
            cids.append(cid)

        return cids


通过不覆盖PngStreamverify类:

class PngStream(ChunkStream):

    ...

关于python - 如何验证功能的实现?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39637629/

10-12 14:25