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

Skip to content

Commit f395f0d

Browse files
author
Steve Canny
committed
img: add Image.from_blob()
1 parent 374b505 commit f395f0d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

docx/image/image.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def __init__(self, blob, filename, image_header):
2626
self._filename = filename
2727
self._image_header = image_header
2828

29+
@classmethod
30+
def from_blob(cls, blob):
31+
"""
32+
Return a new |Image| subclass instance parsed from the image binary
33+
contained in *blob*.
34+
"""
35+
stream = BytesIO(blob)
36+
return cls._from_stream(stream, blob)
37+
2938
@classmethod
3039
def from_file(cls, image_descriptor):
3140
"""

tests/image/test_image.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727

2828
class DescribeImage(object):
2929

30+
def it_can_construct_from_an_image_blob(self, from_blob_fixture):
31+
blob_, BytesIO_, _from_stream_, stream_, image_ = from_blob_fixture
32+
image = Image.from_blob(blob_)
33+
BytesIO_.assert_called_once_with(blob_)
34+
_from_stream_.assert_called_once_with(stream_, blob_)
35+
assert image is image_
36+
3037
def it_can_construct_from_an_image_path(self, from_path_fixture):
3138
image_path, _from_stream_, stream_, blob, filename, image_ = (
3239
from_path_fixture
@@ -115,6 +122,11 @@ def dpi_fixture(self, image_header_):
115122
def filename_(self, request):
116123
return instance_mock(request, str)
117124

125+
@pytest.fixture
126+
def from_blob_fixture(
127+
self, blob_, BytesIO_, _from_stream_, stream_, image_):
128+
return blob_, BytesIO_, _from_stream_, stream_, image_
129+
118130
@pytest.fixture
119131
def from_filelike_fixture(self, _from_stream_, image_):
120132
image_path = test_file('python-icon.png')

0 commit comments

Comments
 (0)