[英]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
在哪里可以找到实现?
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
通过不重写verify
的PngStream
类:
class PngStream(ChunkStream):
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.