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

Skip to content

Commit 9a2296e

Browse files
author
Steve Canny
committed
img: add GIF support
1 parent 07f9013 commit 9a2296e

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

docx/image/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class MIME_TYPE(object):
9797
"""
9898
Image content types
9999
"""
100+
GIF = 'image/gif'
100101
JPEG = 'image/jpeg'
101102
PNG = 'image/png'
102103
TIFF = 'image/tiff'

docx/image/gif.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from __future__ import absolute_import, division, print_function
44

5+
from struct import Struct
6+
7+
from .constants import MIME_TYPE
58
from .image import BaseImageHeader
69

710

@@ -17,4 +20,21 @@ def from_stream(cls, stream):
1720
Return |Gif| instance having header properties parsed from GIF image
1821
in *stream*.
1922
"""
20-
return cls(None, None, None, None)
23+
px_width, px_height = cls._dimensions_from_stream(stream)
24+
return cls(px_width, px_height, 72, 72)
25+
26+
@property
27+
def content_type(self):
28+
"""
29+
MIME content type for this image, unconditionally `image/png` for
30+
PNG images.
31+
"""
32+
return MIME_TYPE.GIF
33+
34+
@classmethod
35+
def _dimensions_from_stream(cls, stream):
36+
stream.seek(6)
37+
bytes_ = stream.read(4)
38+
struct = Struct('<HH')
39+
px_width, px_height = struct.unpack(bytes_)
40+
return px_width, px_height

features/img-characterize-image.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ Feature: Characterize an image file
2222
| sample.tif | image/tiff | 1600 | 2100 | 200 | 200 |
2323
| jpeg420exif.jpg | image/jpeg | 2048 | 1536 | 72 | 72 |
2424
| court-exif.jpg | image/jpeg | 500 | 375 | 256 | 256 |
25+
| lena.gif | image/gif | 256 | 256 | 72 | 72 |

features/steps/test_files/lena.gif

71.3 KB
Loading

tests/image/test_gif.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test suite for docx.image.gif module
5+
"""
6+
7+
from __future__ import absolute_import, print_function
8+
9+
import pytest
10+
11+
from docx.compat import BytesIO
12+
from docx.image.constants import MIME_TYPE
13+
from docx.image.gif import Gif
14+
15+
from ..unitutil import initializer_mock
16+
17+
18+
class DescribeGif(object):
19+
20+
def it_can_construct_from_a_gif_stream(self, from_stream_fixture):
21+
stream, Gif__init__, cx, cy = from_stream_fixture
22+
gif = Gif.from_stream(stream)
23+
Gif__init__.assert_called_once_with(cx, cy, 72, 72)
24+
assert isinstance(gif, Gif)
25+
26+
def it_knows_its_content_type(self):
27+
gif = Gif(None, None, None, None)
28+
assert gif.content_type == MIME_TYPE.GIF
29+
30+
# fixtures -------------------------------------------------------
31+
32+
@pytest.fixture
33+
def from_stream_fixture(self, Gif__init__):
34+
cx, cy = 42, 24
35+
bytes_ = b'filler\x2A\x00\x18\x00'
36+
stream = BytesIO(bytes_)
37+
return stream, Gif__init__, cx, cy
38+
39+
@pytest.fixture
40+
def Gif__init__(self, request):
41+
return initializer_mock(request, Gif)

0 commit comments

Comments
 (0)