繁体   English   中英

验证功能如何实现?

[英]How verify function implemented?

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

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

在哪里可以找到实现?

(我在这里找到的源代码枕头源代码

在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

更详细的源代码细分

您已经引用的Image类的verify方法的代码显示,默认情况下它不执行任何操作:

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文件,从ImageFile类的源代码可以看出,默认的verify方法将被覆盖,该方法从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

通过不重写verifyPngStream类:

class PngStream(ChunkStream):

    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM