Skip to content

Commit

Permalink
Merge pull request ipython#5277 from minrk/check-jpeg-test
Browse files Browse the repository at this point in the history
check that PIL can save JPEG to BytesIO
  • Loading branch information
takluyver committed Mar 5, 2014
2 parents e2ed373 + c4041c1 commit 8bc2639
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions IPython/core/tests/test_pylabtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
#-----------------------------------------------------------------------------
from __future__ import print_function

from io import UnsupportedOperation, BytesIO

import matplotlib
matplotlib.use('Agg')
from matplotlib.figure import Figure

from nose import SkipTest
import nose.tools as nt

from matplotlib import pyplot as plt
Expand Down Expand Up @@ -57,15 +60,29 @@ def test_figure_to_svg():
svg = pt.print_figure(fig, 'svg')[:100].lower()
nt.assert_in(b'doctype svg', svg)

def _check_pil_jpeg_bytes():
"""Skip if PIL can't write JPEGs to BytesIO objects"""
# PIL's JPEG plugin can't write to BytesIO objects
# Pillow fixes this
from PIL import Image
buf = BytesIO()
img = Image.new("RGB", (4,4))
try:
img.save(buf, 'jpeg')
except Exception as e:
ename = e.__class__.__name__
raise SkipTest("PIL can't write JPEG to BytesIO: %s: %s" % (ename, e))

@dec.skip_without("PIL.Image")
def test_figure_to_jpg():
# simple check for at least jpg-looking output
def test_figure_to_jpeg():
_check_pil_jpeg_bytes()
# simple check for at least jpeg-looking output
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3])
plt.draw()
jpg = pt.print_figure(fig, 'jpg', quality=50)[:100].lower()
assert jpg.startswith(_JPEG)
jpeg = pt.print_figure(fig, 'jpeg', quality=50)[:100].lower()
assert jpeg.startswith(_JPEG)

def test_retina_figure():
fig = plt.figure()
Expand Down

0 comments on commit 8bc2639

Please sign in to comment.