Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

strange output from wx and wxagg when trying to render to JPEG or TIFF #1770

Merged
merged 1 commit into from

4 participants

@dhyams

This patch has been tested under Windows, OSX, and Linux. If PIL is present, it gives an accurate rendering of the graph, whereas the wx backend's print_jpeg and print_tiff don't. I've attached before and after pictures (before and after this patch).

Other backends that might exhibit similar behavior, but I am not in a position to test, are:

  • gdk
  • gtk
  • macosx

A test script that generates the images is below.

#!/usr/bin/env python
import matplotlib
matplotlib.use('WxAgg')
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.0,2.0*np.pi,50)
y = np.sin(x)  

plt.plot(x,y,color='red')
plt.title("Any text can have math: $E = m\sqrt{c^4}$")
plt.xlim(0.0,2.0*np.pi)


canvas = plt.gcf().get_canvas()
canvas.print_tiff('out.tif')
canvas.print_png('out.png')
canvas.print_jpeg('out.jpg')

plt.show()

Before, png
before
Before,jpeg
before
Before, tif
[Could not display because file type unsupported...it looked very similar to the "before" jpeg]

After, png
after

After, jpeg
after

After,tif
[Could not display because file type unsupported...it looked very similar to the "after" jpeg]

@mdboom
Owner

What's happening here is that for JPEG, TIF, etc., it's using the Wx, not the WxAgg backend for rendering (with all of the bugs associated with that backend). I think the true fix for this is to add a get_renderer method which would return a WxRenderer on Wx and an agg renderer on WxAgg and then use that from _print_image. This model is used in the Gtk/GtkAgg backends to great effect. This would allow for using the Agg renderer but wxWidgets file outputting code, thus not requiring PIL.

@dhyams

Ah, I see. I didn't see the WxAgg specific print_png code...I'll look over things again and try to implement the fix that you recommend above.

@pelson
Collaborator

Looks like this PR is superceeded by #1771. Is that correct? If so, please feel free to close.

@dhyams

No, it's not. It's a separate issue.

@dhyams

And I'm not sure that I am going to have a chance to sit down and figure out how to fix this as mdboom recommends...is there a way to formally express this pull request as an issue?

@mdboom
Owner

I'm milestoning it as 1.3.x so we don't lose track of the underlying issue. If you or someone else (perhaps myself) submits an alternative PR, we can close this in favor of it then.

@tacaswell
Owner

This (on quick inspection) looks to be the same as issue #1877

@dhyams

I agree tacaswell.

Daniel Hyams wx backend should not attempt to render jpeg/tiff if PIL is present.
If PIL is present, the wx backend needs to allow the base class to get the
job done for print_jpeg and print_tiff, instead of trying to take over
duties itself.
9192c2e
@mdboom
Owner

@dhyams: Thanks, I think this is good to go. It requires #1998 to be merged first, however, to get past a show-stopping Wx bug.

@mdboom mdboom merged commit 3b2a722 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 24, 2013
  1. wx backend should not attempt to render jpeg/tiff if PIL is present.

    Daniel Hyams authored
    If PIL is present, the wx backend needs to allow the base class to get the
    job done for print_jpeg and print_tiff, instead of trying to take over
    duties itself.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 6 deletions.
  1. +9 −6 lib/matplotlib/backends/backend_wx.py
View
15 lib/matplotlib/backends/backend_wx.py
@@ -128,6 +128,7 @@ def bind(actor,event,action,id=None):
FigureCanvasBase, FigureManagerBase, NavigationToolbar2, \
cursors, TimerBase
from matplotlib.backend_bases import ShowBase
+from matplotlib.backend_bases import _has_pil
from matplotlib._pylab_helpers import Gcf
from matplotlib.artist import Artist
@@ -1132,9 +1133,10 @@ def print_figure(self, filename, *args, **kwargs):
def print_bmp(self, filename, *args, **kwargs):
return self._print_image(filename, wx.BITMAP_TYPE_BMP, *args, **kwargs)
- def print_jpeg(self, filename, *args, **kwargs):
- return self._print_image(filename, wx.BITMAP_TYPE_JPEG, *args, **kwargs)
- print_jpg = print_jpeg
+ if not _has_pil:
+ def print_jpeg(self, filename, *args, **kwargs):
+ return self._print_image(filename, wx.BITMAP_TYPE_JPEG, *args, **kwargs)
+ print_jpg = print_jpeg
def print_pcx(self, filename, *args, **kwargs):
return self._print_image(filename, wx.BITMAP_TYPE_PCX, *args, **kwargs)
@@ -1142,9 +1144,10 @@ def print_pcx(self, filename, *args, **kwargs):
def print_png(self, filename, *args, **kwargs):
return self._print_image(filename, wx.BITMAP_TYPE_PNG, *args, **kwargs)
- def print_tiff(self, filename, *args, **kwargs):
- return self._print_image(filename, wx.BITMAP_TYPE_TIF, *args, **kwargs)
- print_tif = print_tiff
+ if not _has_pil:
+ def print_tiff(self, filename, *args, **kwargs):
+ return self._print_image(filename, wx.BITMAP_TYPE_TIF, *args, **kwargs)
+ print_tif = print_tiff
def print_xpm(self, filename, *args, **kwargs):
return self._print_image(filename, wx.BITMAP_TYPE_XPM, *args, **kwargs)
Something went wrong with that request. Please try again.