Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcba2bf

Browse files
author
Steve Canny
committed
img: add Image.sha1
1 parent 2b57421 commit fcba2bf

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

docx/image/image.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
from __future__ import absolute_import, division, print_function
99

10+
import hashlib
1011
import os
1112

1213
from ..compat import BytesIO, is_string
14+
from ..shared import lazyproperty
1315
from .exceptions import UnrecognizedImageError
1416

1517

@@ -81,6 +83,13 @@ def vert_dpi(self):
8183
"""
8284
return self._image_header.vert_dpi
8385

86+
@lazyproperty
87+
def sha1(self):
88+
"""
89+
SHA1 hash digest of the image blob
90+
"""
91+
return hashlib.sha1(self._blob).hexdigest()
92+
8493
@classmethod
8594
def _from_stream(cls, stream, blob, filename=None):
8695
"""

tests/image/test_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def it_knows_the_horz_and_vert_dpi_of_the_image(self, dpi_fixture):
7070
assert image.horz_dpi == horz_dpi
7171
assert image.vert_dpi == vert_dpi
7272

73+
def it_knows_the_sha1_of_its_image(self):
74+
blob = b'fO0Bar'
75+
image = Image(blob, None, None)
76+
assert image.sha1 == '4921e7002ddfba690a937d54bda226a7b8bdeb68'
77+
7378
# fixtures -------------------------------------------------------
7479

7580
@pytest.fixture

0 commit comments

Comments
 (0)