Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7488b3f

Browse files
committed
added Jouni's pdf image dpi patch; apply fix to figimage
svn path=/branches/v0_98_5_maint/; revision=6644
1 parent 09c9d33 commit 7488b3f

4 files changed

Lines changed: 37 additions & 21 deletions

File tree

examples/pylab_examples/figimage_demo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
im1 = plt.figimage(Z, xo=50, yo=0, cmap=cm.jet, origin='lower')
1616
im2 = plt.figimage(Z, xo=100, yo=100, alpha=.8, cmap=cm.jet, origin='lower')
1717

18+
dpi = 200
19+
plt.savefig('figimage.png', dpi=dpi)
20+
plt.savefig('figimage.pdf', dpi=dpi)
21+
plt.savefig('figimage.svg', dpi=dpi)
22+
plt.savefig('figimage.eps', dpi=dpi)
1823
plt.show()
1924

2025

lib/matplotlib/backends/backend_pdf.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,13 +1184,14 @@ class RendererPdf(RendererBase):
11841184
truetype_font_cache = maxdict(50)
11851185
afm_font_cache = maxdict(50)
11861186

1187-
def __init__(self, file, dpi):
1187+
def __init__(self, file, dpi, image_dpi):
11881188
RendererBase.__init__(self)
11891189
self.file = file
11901190
self.gc = self.new_gc()
11911191
self.file.used_characters = self.used_characters = {}
11921192
self.mathtext_parser = MathTextParser("Pdf")
11931193
self.dpi = dpi
1194+
self.image_dpi = image_dpi
11941195
self.tex_font_map = None
11951196

11961197
def finalize(self):
@@ -1230,16 +1231,18 @@ def merge_used_characters(self, other):
12301231
stat_key, (realpath, set()))
12311232
used_characters[1].update(charset)
12321233

1234+
def get_image_magnification(self):
1235+
return self.image_dpi/72.0
1236+
12331237
def draw_image(self, x, y, im, bbox, clippath=None, clippath_trans=None):
1234-
#print >>sys.stderr, "draw_image called"
1235-
12361238
# MGDTODO: Support clippath here
12371239
gc = self.new_gc()
12381240
if bbox is not None:
12391241
gc.set_clip_rectangle(bbox)
12401242
self.check_gc(gc)
12411243

12421244
h, w = im.get_size_out()
1245+
h, w = 72.0*h/self.image_dpi, 72.0*w/self.image_dpi
12431246
imob = self.file.imageObject(im)
12441247
self.file.output(Op.gsave, w, 0, 0, h, x, y, Op.concat_matrix,
12451248
imob, Op.use_xobject, Op.grestore)
@@ -1873,13 +1876,13 @@ def get_default_filetype(self):
18731876
return 'pdf'
18741877

18751878
def print_pdf(self, filename, **kwargs):
1876-
dpi = 72 # there are 72 Postscript points to an inch
1877-
# TODO: use the dpi kwarg for images
1878-
self.figure.set_dpi(dpi)
1879+
ppi = 72 # Postscript points in an inch
1880+
image_dpi = kwargs.get('dpi', 72) # dpi to use for images
1881+
self.figure.set_dpi(ppi)
18791882
width, height = self.figure.get_size_inches()
1880-
file = PdfFile(width, height, dpi, filename)
1883+
file = PdfFile(width, height, ppi, filename)
18811884
renderer = MixedModeRenderer(
1882-
width, height, dpi, RendererPdf(file, dpi))
1885+
width, height, ppi, RendererPdf(file, ppi, image_dpi))
18831886
self.figure.draw(renderer)
18841887
renderer.finalize()
18851888
file.close()

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ def draw(self, renderer):
755755
# make a composite image blending alpha
756756
# list of (_image.Image, ox, oy)
757757
mag = renderer.get_image_magnification()
758-
ims = [(im.make_image(mag), im.ox*mag, im.oy*mag)
758+
ims = [(im.make_image(mag), im.ox, im.oy)
759759
for im in self.images]
760760

761761
im = _image.from_images(self.bbox.height * mag,

lib/matplotlib/image.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ def __init__(self, fig,
630630
self.ox = offsetx
631631
self.oy = offsety
632632
self.update(kwargs)
633+
self.magnification = 1.0
633634

634635
def contains(self, mouseevent):
635636
"""Test whether the mouse event occured within the image.
@@ -659,22 +660,30 @@ def get_extent(self):
659660
-0.5+self.oy, numrows-0.5+self.oy)
660661

661662
def make_image(self, magnification=1.0):
662-
# had to introduce argument magnification to satisfy the unit test
663-
# figimage_demo.py. I have no idea, how magnification should be used
664-
# within the function. It should be !=1.0 only for non-default DPI<
665-
# settings in the PS backend, as introduced by patch #1562394
666-
# Probably Nicholas Young should look over this code and see, how
667-
# magnification should be handled correctly.
668663
if self._A is None:
669664
raise RuntimeError('You must first set the image array')
670665

671666
x = self.to_rgba(self._A, self._alpha)
672-
673-
im = _image.fromarray(x, 1)
667+
self.magnification = magnification
668+
# if magnification is not one, we need to resize
669+
ismag = magnification!=1
670+
#if ismag: raise RuntimeError
671+
if ismag:
672+
isoutput = 0
673+
else:
674+
isoutput = 1
675+
im = _image.fromarray(x, isoutput)
674676
fc = self.figure.get_facecolor()
675677
im.set_bg( *mcolors.colorConverter.to_rgba(fc, 0) )
676678
im.is_grayscale = (self.cmap.name == "gray" and
677679
len(self._A.shape) == 2)
680+
681+
if ismag:
682+
numrows, numcols = self.get_size()
683+
numrows *= magnification
684+
numcols *= magnification
685+
im.set_interpolation(_image.NEAREST)
686+
im.resize(numcols, numrows)
678687
if self.origin=='upper':
679688
im.flipud_out()
680689

@@ -683,9 +692,8 @@ def make_image(self, magnification=1.0):
683692
def draw(self, renderer, *args, **kwargs):
684693
if not self.get_visible(): return
685694
# todo: we should be able to do some cacheing here
686-
im = self.make_image()
687-
688-
renderer.draw_image(round(self.ox), round(self.oy), im, self.figure.bbox,
695+
im = self.make_image(renderer.get_image_magnification())
696+
renderer.draw_image(round(self.ox/self.magnification), round(self.oy/self.magnification), im, self.figure.bbox,
689697
*self.get_transformed_clip_path_and_affine())
690698

691699
def write_png(self, fname):
@@ -772,7 +780,7 @@ def toarray(im):
772780
x.shape = im.size[1], im.size[0], 4
773781
return x
774782

775-
def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
783+
def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
776784
preview=False):
777785
"""
778786
make a thumbnail of image in *infile* with output filename

0 commit comments

Comments
 (0)