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

Skip to content

Commit f262892

Browse files
committed
Merge pull request #2044 from mdboom/svg-rasterize
Svg rasterize (rebased)
2 parents 22c3978 + f288b4e commit f262892

File tree

8 files changed

+743
-119
lines changed

8 files changed

+743
-119
lines changed

CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2013-05-18 Added support for arbitrary rasterization resolutions to the
2+
SVG backend. Previously the resolution was hard coded to 72
3+
dpi. Now the backend class takes a image_dpi argument for
4+
its constructor, adjusts the image bounding box accordingly
5+
and forwards a magnification factor to the image renderer.
6+
The code and results now resemble those of the PDF backend.
7+
- MW
8+
19
2013-05-08 Changed behavior of hist when given stacked=True and normed=True.
210
Histograms are now stacked first, then the sum is normalized.
311
Previously, each histogram was normalized, then they were stacked.

doc/users/whats_new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,3 +1126,7 @@ Here are the 0.98.4 notes from the CHANGELOG::
11261126
off automatically when infs or NaNs are present. Also masked
11271127
arrays are now converted to arrays with NaNs for consistent
11281128
handling of masks and NaNs - MGD and EF
1129+
1130+
Added support for arbitrary rasterization resolutions to the SVG
1131+
backend. - MW
1132+

lib/matplotlib/backends/backend_svg.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ class RendererSVG(RendererBase):
249249
FONT_SCALE = 100.0
250250
fontd = maxdict(50)
251251

252-
def __init__(self, width, height, svgwriter, basename=None):
252+
def __init__(self, width, height, svgwriter, basename=None, image_dpi=72):
253253
self.width = width
254254
self.height = height
255255
self.writer = XMLWriter(svgwriter)
256+
self.image_dpi = image_dpi # the actual dpi we want to rasterize stuff with
256257

257258
self._groupd = {}
258259
if not rcParams['svg.image_inline']:
@@ -747,6 +748,9 @@ def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
747748
def option_scale_image(self):
748749
return True
749750

751+
def get_image_magnification(self):
752+
return self.image_dpi / 72.0
753+
750754
def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
751755
attrib = {}
752756
clipid = self._get_clip(gc)
@@ -769,6 +773,17 @@ def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
769773
im.resize(numcols, numrows)
770774

771775
h,w = im.get_size_out()
776+
777+
if dx is None:
778+
w = 72.0*w/self.image_dpi
779+
else:
780+
w = dx
781+
782+
if dy is None:
783+
h = 72.0*h/self.image_dpi
784+
else:
785+
h = dy
786+
772787
oid = getattr(im, '_gid', None)
773788
url = getattr(im, '_url', None)
774789
if url is not None:
@@ -1154,25 +1169,17 @@ def print_svgz(self, filename, *args, **kwargs):
11541169

11551170
def _print_svg(self, filename, svgwriter, fh_to_close=None, **kwargs):
11561171
try:
1172+
image_dpi = kwargs.pop("dpi", 72)
11571173
self.figure.set_dpi(72.0)
11581174
width, height = self.figure.get_size_inches()
11591175
w, h = width*72, height*72
11601176

11611177
if rcParams['svg.image_noscale']:
1162-
renderer = RendererSVG(w, h, svgwriter, filename)
1178+
renderer = RendererSVG(w, h, svgwriter, filename, image_dpi)
11631179
else:
1164-
# setting mixed renderer dpi other than 72 results in
1165-
# incorrect size of the rasterized image. It seems that the
1166-
# svg internally uses fixed dpi of 72 and seems to cause
1167-
# the problem. I hope someone who knows the svg backends
1168-
# take a look at this problem. Meanwhile, the dpi
1169-
# parameter is ignored and image_dpi is fixed at 72. - JJL
1170-
1171-
#image_dpi = kwargs.pop("dpi", 72)
1172-
image_dpi = 72
11731180
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
11741181
renderer = MixedModeRenderer(self.figure,
1175-
width, height, image_dpi, RendererSVG(w, h, svgwriter, filename),
1182+
width, height, image_dpi, RendererSVG(w, h, svgwriter, filename, image_dpi),
11761183
bbox_inches_restore=_bbox_inches_restore)
11771184

11781185
self.figure.draw(renderer)

lib/matplotlib/testing/compare.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,36 @@ def compare_images( expected, actual, tol, in_decorator=False ):
306306
expectedImage = expectedImage.astype(np.int16)
307307
actualImage = actualImage.astype(np.int16)
308308

309+
# compare the resulting image histogram functions
310+
expected_version = version.LooseVersion("1.6")
311+
found_version = version.LooseVersion(np.__version__)
312+
313+
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
314+
# using histogram
315+
if found_version >= expected_version:
316+
rms = 0
317+
318+
for i in xrange(0, 3):
319+
h1p = expectedImage[:,:,i]
320+
h2p = actualImage[:,:,i]
321+
322+
h1h = np.bincount(h1p.ravel(), minlength=256)
323+
h2h = np.bincount(h2p.ravel(), minlength=256)
324+
325+
rms += np.sum(np.power((h1h-h2h), 2))
326+
else:
327+
rms = 0
328+
ns = np.arange(257)
329+
330+
for i in xrange(0, 3):
331+
h1p = expectedImage[:,:,i]
332+
h2p = actualImage[:,:,i]
333+
334+
h1h = np.histogram(h1p, bins=ns)[0]
335+
h2h = np.histogram(h2p, bins=ns)[0]
336+
337+
rms += np.sum(np.power((h1h-h2h), 2))
338+
309339
rms = calculate_rms(expectedImage, actualImage)
310340

311341
diff_image = make_test_filename(actual, 'failed-diff')

0 commit comments

Comments
 (0)