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

Skip to content

Commit 10d939a

Browse files
committed
Fix JPEG saving bug: only accept the kwargs documented by PIL for JPEG files.
svn path=/trunk/matplotlib/; revision=8727
1 parent 6baa03e commit 10d939a

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2010-10-04 Fix JPEG saving bug: only accept the kwargs documented
2+
by PIL for JPEG files. - JKS
3+
14
2010-09-15 Remove unused _wxagg extension and numerix.h. - EF
25

36
2010-08-25 Add new framework for doing animations with examples.- RM

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,9 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
17841784
agg = self.switch_backends(FigureCanvasAgg)
17851785
buf, size = agg.print_to_buffer()
17861786
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
1787-
return image.save(filename_or_obj, **kwargs)
1787+
options = cbook.restrict_dict(kwargs, ['quality', 'optimize',
1788+
'progressive'])
1789+
return image.save(filename_or_obj, **options)
17881790
print_jpeg = print_jpg
17891791

17901792
filetypes['tif'] = filetypes['tiff'] = 'Tagged Image File Format'

lib/matplotlib/cbook.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,12 @@ def reverse_dict(d):
12101210
'reverse the dictionary -- may lose data if values are not unique!'
12111211
return dict([(v,k) for k,v in d.items()])
12121212

1213+
def restrict_dict(d, keys):
1214+
"""
1215+
Return a dictionary that contains those keys that appear in both
1216+
d and keys, with values from d.
1217+
"""
1218+
return dict([(k,v) for (k,v) in d.iteritems() if k in keys])
12131219

12141220
def report_memory(i=0): # argument may go away
12151221
'return the memory consumed by process'

lib/matplotlib/tests/test_cbook.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ def test_is_string_like():
1212

1313
assert cbook.is_string_like( "hello world" )
1414
assert_equal( cbook.is_string_like(10), False )
15+
16+
def test_restrict_dict():
17+
d = {'foo': 'bar', 1: 2}
18+
d1 = cbook.restrict_dict(d, ['foo', 1])
19+
assert_equal(d1, d)
20+
d2 = cbook.restrict_dict(d, ['bar', 2])
21+
assert_equal(d2, {})
22+
d3 = cbook.restrict_dict(d, {'foo': 1})
23+
assert_equal(d3, {'foo': 'bar'})
24+
d4 = cbook.restrict_dict(d, {})
25+
assert_equal(d4, {})
26+
d5 = cbook.restrict_dict(d, set(['foo',2]))
27+
assert_equal(d5, {'foo': 'bar'})
28+
# check that d was not modified
29+
assert_equal(d, {'foo': 'bar', 1: 2})

0 commit comments

Comments
 (0)