I want to find how verify()
function from Pillow
library implemented. In a source code i found only this:
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
Where i can find implementation?
(source code i found here: Pillow source code )
A comment on GitHub explains:
Image.[v]erify only checks the chunk checksums in png files, and is a no-op elsewhere.
So the short answer is that you already did find the default implementation which does absolutely nothing.
Except for PNG files , for which you can find the implementation in the PngImageFile.verify
method:
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
which in turn through self.png.verify()
calls 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
Your already quoted code for the verify
method of the Image
class shows that it by default does nothing:
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
But in the case of PNG files the default verify
method is overridden, as seen from the source code for the ImageFile
class, which inherits from the Image
class:
class ImageFile(Image.Image):
"Base class for image file format handlers."
...
and the source code for the PNG plugin class PngImageFile
which inherits from ImageFile
:
##
# Image plugin for PNG images.
class PngImageFile(ImageFile.ImageFile):
...
and has this overridden implementation of 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
which in turn through self.png.verify()
calls 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
through the PngStream
class which doesn't override verify
:
class PngStream(ChunkStream):
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.