8000 Let dpi be set when saving JPEG using Agg backend by dstansby · Pull Request #9066 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Let dpi be set when saving JPEG using Agg backend #9066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,13 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
background = Image.new('RGB', size, color)
background.paste(image, image)
options = {k: kwargs[k]
for k in ['quality', 'optimize', 'progressive']
for k in ['quality', 'optimize', 'progressive', 'dpi']
if k in kwargs}
options.setdefault('quality', rcParams['savefig.jpeg_quality'])
if 'dpi' in options:
# Set the same dpi in both x and y directions
options['dpi'] = (options['dpi'], options['dpi'])

return background.save(filename_or_obj, format='jpeg', **options)
print_jpeg = print_jpg

Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,16 @@ def test_chunksize():
rcParams['agg.path.chunksize'] = 105
ax.plot(x, np.sin(x))
fig.canvas.draw()


@pytest.mark.backend('Agg')
def test_jpeg_dpi():
try:
from PIL import Image
except Exception:
pytest.skip("Could not import PIL")
# Check that dpi is set correctly in jpg files
plt.plot([0, 1, 2], [0, 1, 0])
plt.savefig('test.jpg', dpi=200)
im = Image.open("test.jpg")
assert im.info['dpi'] == (200, 200)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should delete test.jpg after it is done (or, more simply, just write the file to a bytesio).

0