"John Hunter" <jdh2...@gmail.com> writes:

>> I'll take a closer look at this later.
>
> Thanks for looking into this Jouni -- please make sure to fix in the
> branch and merge to the trunk, as described in
> http://matplotlib.sourceforge.net/devel/coding_guide.html#using-svnmerge

I have a patch (attached) that fixes this case but breaks figimage. It
seems to me that the bug is in figimage, but with the current pdf
backend (which forces dpi=72 for all images) it happens to work right.
I'm a little wary of making this change on the maintenance branch
without also fixing figimage, and I don't really know how to fix
figimage.

The bug in the pdf backend is that it forces dpi=72, because in PDF 1.4
there are always 72 points to an inch. (In some later versions the scale
can be changed, but never mind that for now.) This is a problem because
sometimes people want to have their figures at a higher resolution. The
attached patch adds an image_dpi variable to the pdf backend and uses it
like the ps backend does: get_image_magnification() returns
image_dpi/72.0, and the width and height of images are divided by this
magnification factor.

The problem with figimage is that it doesn't use the magnification
factor at all, so when it calls draw_image, the image as drawn by the
backend is scaled wrong. See also the following comment in image.py
(class FigureImage):

  2798     nnemec     def make_image(self, magnification=1.0):
  2904    efiring         # had to introduce argument magnification to satisfy 
the unit test
  2904    efiring         # figimage_demo.py. I have no idea, how magnification 
should be used
  5883    jdh2358         # within the function. It should be !=1.0 only for 
non-default DPI<
  2904    efiring         # settings in the PS backend, as introduced by patch 
#1562394
  2904    efiring         # Probably Nicholas Young should look over this code 
and see, how
  2904    efiring         # magnification should be handled correctly.

(When I say that this patch "breaks" figimage, I mean that before the
patch the pdf output of figimage_demo.py looks similar to the Agg
output, but after applying the patch it looks different.)

Ideas, anyone?

-- 
Jouni K. Seppänen
http://www.iki.fi/jks

Index: lib/matplotlib/backends/backend_pdf.py
===================================================================
--- lib/matplotlib/backends/backend_pdf.py	(revision 6612)
+++ lib/matplotlib/backends/backend_pdf.py	(working copy)
@@ -1184,13 +1184,14 @@
     truetype_font_cache = maxdict(50)
     afm_font_cache = maxdict(50)
 
-    def __init__(self, file, dpi):
+    def __init__(self, file, dpi, image_dpi):
         RendererBase.__init__(self)
         self.file = file
         self.gc = self.new_gc()
         self.file.used_characters = self.used_characters = {}
         self.mathtext_parser = MathTextParser("Pdf")
         self.dpi = dpi
+        self.image_dpi = image_dpi
         self.tex_font_map = None
 
     def finalize(self):
@@ -1230,9 +1231,10 @@
                 stat_key, (realpath, set()))
             used_characters[1].update(charset)
 
+    def get_image_magnification(self):
+        return self.image_dpi/72.0
+            
     def draw_image(self, x, y, im, bbox, clippath=None, clippath_trans=None):
-        #print >>sys.stderr, "draw_image called"
-
         # MGDTODO: Support clippath here
         gc = self.new_gc()
         if bbox is not None:
@@ -1240,6 +1242,7 @@
         self.check_gc(gc)
 
         h, w = im.get_size_out()
+        h, w = 72.0*h/self.image_dpi, 72.0*w/self.image_dpi
         imob = self.file.imageObject(im)
         self.file.output(Op.gsave, w, 0, 0, h, x, y, Op.concat_matrix,
                          imob, Op.use_xobject, Op.grestore)
@@ -1873,13 +1876,13 @@
         return 'pdf'
 
     def print_pdf(self, filename, **kwargs):
-        dpi = 72 # there are 72 Postscript points to an inch
-        # TODO: use the dpi kwarg for images
-        self.figure.set_dpi(dpi)
+        ppi = 72 # Postscript points in an inch
+        image_dpi = kwargs.get('dpi', 72) # dpi to use for images
+        self.figure.set_dpi(ppi)
         width, height = self.figure.get_size_inches()
-        file = PdfFile(width, height, dpi, filename)
+        file = PdfFile(width, height, ppi, filename)
         renderer = MixedModeRenderer(
-            width, height, dpi, RendererPdf(file, dpi))
+            width, height, ppi, RendererPdf(file, ppi, image_dpi))
         self.figure.draw(renderer)
         renderer.finalize()
         file.close()
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to