summaryrefslogtreecommitdiff
path: root/dedup/image.py
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2023-05-09 15:10:25 +0200
committerHelmut Grohne <helmut@subdivi.de>2023-05-09 15:12:01 +0200
commit924f0c734a7accb87e2ac911cee6e24dd463f237 (patch)
treeeb1bcaa2f25933374d28905bcb56e2e8aabeec62 /dedup/image.py
parent8a05a6d8bacea0643a4967eed4cd67019ee0b6d7 (diff)
downloaddebian-dedup-924f0c734a7accb87e2ac911cee6e24dd463f237.tar.gz
add type annotations to most of the codeHEADmaster
Diffstat (limited to 'dedup/image.py')
-rw-r--r--dedup/image.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/dedup/image.py b/dedup/image.py
index 91321f4..a417528 100644
--- a/dedup/image.py
+++ b/dedup/image.py
@@ -3,6 +3,8 @@ import struct
import PIL.Image
+from .hashing import HashlibLike
+
class ImageHash:
"""A hash on the contents of an image data type supported by PIL. This
disregards mode, depth and meta information. Note that due to limitations
@@ -11,8 +13,9 @@ class ImageHash:
maxsize = 1024 * 1024 * 32
# max memory usage is about 5 * maxpixels in bytes
maxpixels = 1024 * 1024 * 32
+ name_prefix: str
- def __init__(self, hashobj):
+ def __init__(self, hashobj: HashlibLike) -> None:
"""
@param hashobj: a hashlib-like object
"""
@@ -20,23 +23,26 @@ class ImageHash:
self.imagedetected = False
self.content = io.BytesIO()
- def detect(self):
+ def detect(self) -> bool:
raise NotImplementedError
- def update(self, data):
+ def update(self, data: bytes) -> None:
self.content.write(data)
if self.content.tell() > self.maxsize:
raise ValueError("maximum image size exceeded")
if not self.imagedetected:
self.imagedetected = self.detect()
- def copy(self):
+ def copy(self) -> "ImageHash":
new = self.__class__(self.hashobj.copy())
new.imagedetected = self.imagedetected
new.content = io.BytesIO(self.content.getvalue())
return new
- def hexdigest(self):
+ def digest(self) -> bytes:
+ raise ValueError("an ImageHash cannot produce a raw digest")
+
+ def hexdigest(self) -> str:
if not self.imagedetected:
raise ValueError("not a image")
hashobj = self.hashobj.copy()
@@ -70,7 +76,7 @@ class ImageHash:
return "%s%8.8x%8.8x" % (hashobj.hexdigest(), width, height)
@property
- def name(self):
+ def name(self) -> str:
return self.name_prefix + self.hashobj.name
@@ -78,7 +84,7 @@ class PNGHash(ImageHash):
"""A hash on the contents of a PNG image."""
name_prefix = "png_"
- def detect(self):
+ def detect(self) -> bool:
if self.content.tell() < 33: # header + IHDR
return False
curvalue = self.content.getvalue()
@@ -93,7 +99,7 @@ class GIFHash(ImageHash):
"""A hash on the contents of the first frame of a GIF image."""
name_prefix = "gif_"
- def detect(self):
+ def detect(self) -> bool:
if self.content.tell() < 10: # magic + logical dimension
return False
curvalue = self.content.getvalue()