Skip to content

Commit ab9e5af

Browse files
authored
Merge pull request #20 from rkoopmans/transcoding
Add convert / transcoding of images
2 parents eb2cb93 + f626b47 commit ab9e5af

File tree

8 files changed

+77
-4
lines changed

8 files changed

+77
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
__pycache__/
33
build/
44
dist/
5-
*.egg-info/
5+
*.egg-info/
6+
.tox

CHANGES.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## 1.6.0
2+
* Updated runtime support
3+
* Dropped 2.6
4+
* Added python 3.7
5+
* Added python 3.8
6+
* Added python 3.9
7+
* Added python 3.10
8+
* Added python 3.11
9+
* Fixed tests on windows
10+
* Add methods for the transcoding and transformation API
11+
* Add a method for getting the file extension from a Result object
12+
113
## 1.5.2
214
Remove letsencrypt DST Root from ca bundle for openssl 1.0.0 compatibility
315

test/integration.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ def test_should_preserve_metadata(self):
8282

8383
# width == 137
8484
self.assertIn(b'\x00\x00\x00\x89', contents)
85-
self.assertIn(b'Copyright Voormedia', contents)
85+
self.assertIn(b'Copyright Voormedia', contents)
86+
87+
def test_should_transcode_image(self):
88+
with create_named_tmpfile() as tmp:
89+
a = self.optimized.convert(type=["image/webp"]).to_file(tmp)
90+
with open(tmp, 'rb') as f:
91+
content = f.read()
92+
93+
self.assertEqual(b'RIFF', content[:4])
94+
self.assertEqual(b'WEBP', content[8:12])

test/tinify_result_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def test_content_type_should_return_mime_type(self):
3535
def test_to_buffer_should_return_image_data(self):
3636
self.assertEqual(b'image data', self.result.to_buffer())
3737

38+
def test_extension(self):
39+
self.assertEqual('png', self.result.extension)
40+
41+
42+
3843
class TinifyResultWithoutMetaAndDataTest(TestHelper):
3944
def setUp(self):
4045
self.result = Result({}, None)
@@ -59,3 +64,6 @@ def test_content_type_should_return_none(self):
5964

6065
def test_to_buffer_should_return_none(self):
6166
self.assertEqual(None, self.result.to_buffer())
67+
68+
def test_extension(self):
69+
self.assertEqual(None, self.result.extension)

test/tinify_source_test.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from helper import *
1212

13+
1314
class TinifySourceWithInvalidApiKey(TestHelper):
1415
def setUp(self):
1516
super(type(self), self).setUp()
@@ -55,6 +56,10 @@ def return_file(request, uri, headers):
5556
response = b'copyrighted file'
5657
elif 'resize' in data:
5758
response = b'small file'
59+
elif 'convert' in data:
60+
response = b'converted file'
61+
elif 'transform' in data:
62+
response = b'transformed file'
5863
else:
5964
response = b'compressed file'
6065
return (200, headers, response)
@@ -124,6 +129,22 @@ def test_resize_should_return_source_with_data(self):
124129
self.assertEqual(b'small file', Source.from_buffer('png file').resize(width=400).to_buffer())
125130
self.assertJsonEqual('{"resize":{"width":400}}', httpretty.last_request().body.decode('utf-8'))
126131

132+
def test_transform_should_return_source(self):
133+
self.assertIsInstance(Source.from_buffer('png file').transform(background='black'), Source)
134+
self.assertEqual(b'png file', httpretty.last_request().body)
135+
136+
def test_transform_should_return_source_with_data(self):
137+
self.assertEqual(b'transformed file', Source.from_buffer('png file').transform(background='black').to_buffer())
138+
self.assertJsonEqual('{"transform":{"background":"black"}}', httpretty.last_request().body.decode('utf-8'))
139+
140+
def test_convert_should_return_source(self):
141+
self.assertIsInstance(Source.from_buffer('png file').resize(width=400).convert(type=['image/webp']), Source)
142+
self.assertEqual(b'png file', httpretty.last_request().body)
143+
144+
def test_convert_should_return_source_with_data(self):
145+
self.assertEqual(b'converted file', Source.from_buffer('png file').convert(type='image/jpg').to_buffer())
146+
self.assertJsonEqual('{"convert": {"type": "image/jpg"}}', httpretty.last_request().body.decode('utf-8'))
147+
127148
def test_store_should_return_result_meta(self):
128149
self.assertIsInstance(Source.from_buffer('png file').store(service='s3'), ResultMeta)
129150
self.assertJsonEqual('{"store":{"service":"s3"}}', httpretty.last_request().body.decode('utf-8'))
@@ -150,4 +171,14 @@ def test_to_file_with_file_object_should_store_image_data(self):
150171
with create_named_tmpfile() as name:
151172
Source.from_buffer('png file').to_file(name)
152173
with open(name, 'rb') as f:
153-
self.assertEqual(b'compressed file', f.read())
174+
self.assertEqual(b'compressed file', f.read())
175+
176+
def test_all_options_together(self):
177+
self.assertEqual('https://bucket.s3-region.amazonaws.com/some/location',
178+
Source.from_buffer('png file').resize(width=400)\
179+
.convert(type=['image/webp', 'image/png'])\
180+
.transform(background="black")\
181+
.preserve("copyright", "location")\
182+
.store(service='s3').location)
183+
self.assertJsonEqual('{"store":{"service":"s3"},"resize":{"width":400},"preserve": ["copyright", "location"], "transform": {"background": "black"}, "convert": {"type": ["image/webp", "image/png"]}}', httpretty.last_request().body.decode('utf-8'))
184+

tinify/result.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def size(self):
2727
def media_type(self):
2828
return self._meta.get('Content-Type')
2929

30+
@property
31+
def extension(self):
32+
media_type = self._meta.get('Content-Type')
33+
if media_type:
34+
return media_type.split('/')[-1]
35+
3036
@property
3137
def content_type(self):
3238
return self.media_type

tinify/source.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def preserve(self, *options):
3636
def resize(self, **options):
3737
return type(self)(self.url, **self._merge_commands(resize=options))
3838

39+
def convert(self, **options):
40+
return type(self)(self.url, **self._merge_commands(convert=options))
41+
42+
def transform(self, **options):
43+
return type(self)(self.url, **self._merge_commands(transform=options))
44+
3945
def store(self, **options):
4046
response = tinify.get_client().request('POST', self.url, self._merge_commands(store=options))
4147
return ResultMeta(response.headers)

tinify/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.5.2'
1+
__version__ = '1.6.0'

0 commit comments

Comments
 (0)