Skip to content
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

WebAgg Saving JPEG Raises Error #3979

Closed
blink1073 opened this issue Jan 7, 2015 · 4 comments · Fixed by #3981
Closed

WebAgg Saving JPEG Raises Error #3979

blink1073 opened this issue Jan 7, 2015 · 4 comments · Fixed by #3981
Milestone

Comments

@blink1073
Copy link
Member

To reproduce, run the following script (in master, on Python 2.7):

import matplotlib
matplotlib.use('webagg')
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
ERROR:tornado.application:Uncaught exception GET /1/download.jpeg (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8988', method='GET', uri='/1/download.jpeg', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Connection': 'keep-alive', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://127.0.0.1:8988/', 'Host': '127.0.0.1:8988', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0'})
Traceback (most recent call last):
  File "/home/afrl/anaconda/lib/python2.7/site-packages/tornado/web.py", line 1332, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_webagg.py", line 201, in get
    manager.canvas.print_figure(buff, format=fmt)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backend_bases.py", line 2216, in print_figure
    **kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_agg.py", line 581, in print_jpg
    return image.save(filename_or_obj, format='jpeg', **options)
  File "/home/afrl/anaconda/lib/python2.7/site-packages/PIL/Image.py", line 1439, in save
    save_handler(self, fp, filename)
  File "/home/afrl/anaconda/lib/python2.7/site-packages/PIL/JpegImagePlugin.py", line 471, in _save
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
  File "/home/afrl/anaconda/lib/python2.7/site-packages/PIL/ImageFile.py", line 476, in _save
    fh = fp.fileno()
UnsupportedOperation: fileno
ERROR:tornado.access:500 GET /1/download.jpeg (127.0.0.1) 27.78ms

It looks like PIL is looking for a fileno attribute on the object but the io.BytesIO obj passed by backend_webagg has no fileno.

@blink1073
Copy link
Member Author

PIL is checking for an AttributeError, but this is raising the rather odd UnsupportedOperation error.

@blink1073
Copy link
Member Author

Other filetype problems:

PGF:

ERROR:tornado.application:Uncaught exception GET /1/download.pgf (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8988', method='GET', uri='/1/download.pgf', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Connection': 'keep-alive', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://127.0.0.1:8988/', 'Host': '127.0.0.1:8988', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0'})
Traceback (most recent call last):
  File "/home/afrl/anaconda/lib/python2.7/site-packages/tornado/web.py", line 1332, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_webagg.py", line 201, in get
    manager.canvas.print_figure(buff, format=fmt)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backend_bases.py", line 2216, in print_figure
    **kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 838, in print_pgf
    if not os.path.exists(fname_or_fh.name):
AttributeError: '_io.BytesIO' object has no attribute 'name'

TIF (similar to JPEG problem):

ERROR:tornado.application:Uncaught exception GET /1/download.tif (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8988', method='GET', uri='/1/download.tif', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Connection': 'keep-alive', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://127.0.0.1:8988/', 'Host': '127.0.0.1:8988', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0'})
Traceback (most recent call last):
  File "/home/afrl/anaconda/lib/python2.7/site-packages/tornado/web.py", line 1332, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_webagg.py", line 201, in get
    manager.canvas.print_figure(buff, format=fmt)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backend_bases.py", line 2216, in print_figure
    **kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_agg.py", line 592, in print_tif
    dpi=dpi)
  File "/home/afrl/anaconda/lib/python2.7/site-packages/PIL/Image.py", line 1439, in save
    save_handler(self, fp, filename)
  File "/home/afrl/anaconda/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 841, in _save
    ("raw", (0,0)+im.size, offset, (rawmode, stride, 1))
  File "/home/afrl/anaconda/lib/python2.7/site-packages/PIL/ImageFile.py", line 476, in _save
    fh = fp.fileno()
UnsupportedOperation: fileno
ERROR:tornado.access:500 GET /1/download.tif (127.0.0.1) 39.17ms

@blink1073
Copy link
Member Author

The following fixes the BytesIO problems:

diff --git a/lib/matplotlib/backends/backend_webagg.py b/lib/matplotlib/backends/backend_webagg.py
index 9583906..16ef87b 100644
--- a/lib/matplotlib/backends/backend_webagg.py
+++ b/lib/matplotlib/backends/backend_webagg.py
@@ -197,7 +197,13 @@ class WebAggApplication(tornado.web.Application):

             self.set_header('Content-Type', mimetypes.get(fmt, 'binary'))

-            buff = io.BytesIO()
+            # override fileno to raise AttributeError so PIL doesn't error
+            class BytesIO(io.BytesIO):
+                @property
+                def fileno(self):
+                    raise AttributeError
+
+            buff = BytesIO()
             manager.canvas.print_figure(buff, format=fmt)
             self.write(buff.getvalue())

@blink1073
Copy link
Member Author

I tried working around the pgf error, but then got this error:

ERROR:tornado.application:Uncaught exception GET /1/download.pgf (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:8988', method='GET', uri='/1/download.pgf', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Connection': 'keep-alive', 'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://127.0.0.1:8988/', 'Host': '127.0.0.1:8988', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0'})
Traceback (most recent call last):
  File "/home/afrl/anaconda/lib/python2.7/site-packages/tornado/web.py", line 1332, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_webagg.py", line 207, in get
    manager.canvas.print_figure(buff, format=fmt)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backend_bases.py", line 2216, in print_figure
    **kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 843, in print_pgf
    self._print_pgf_to_fh(fname_or_fh, *args, **kwargs)
  File "/home/afrl/workspace/matplotlib/lib/matplotlib/backends/backend_pgf.py", line 805, in _print_pgf_to_fh
    fh.write(header_text)
TypeError: 'unicode' does not have the buffer interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants