验证功能如何实现?

时间:2016-09-22 11:21:18

标签: python pillow

我想了解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

1 个答案:

答案 0 :(得分:2)

comment on GitHub解释:

  

图片。[v] erify只检查png文件中的块校验和,并且是其他地方的no-op。

所以简短的回答是你已经找到了绝对没有做到的默认实现。

除了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."

    ...

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

##
# 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

通过不覆盖verify的{​​{3}}类:

class PngStream(ChunkStream):

    ...