-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix to "exported SVG files blurred in viewers" #17062
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
Conversation
Change `image-rendering` from `optimizeSpeed` to `pixelated`. `optimizeSpeed` is used by Inkscape only
I agree svg is important in and of its self, however you can fix the blurry text in the notebook by using ipympl (which also gets you live figures!) or by increasing the dpi that inline uses. |
I am concerned that we are applying this change both un-conditionally and to only one of the two branches that write out image elements to the svg. I can see the argument that in all cases, even if we are already re-sampling the image, that we don't want the viewer further re-sampling (which I assume it would do in RGB(A) space which in the case of false-color images leads to "wrong" colors showing up) but I am less sure about the aspect ratio. This will definitely require some tests. |
Can you post before/after examples where this improves things? |
# generate an SVG image with 4 pixels
import matplotlib.pylab as plt
a = [[0,0],[0,1]]
# interpolation='none' is the only way to avoid resampling images in saved SVG files.
# interpolation=None will produce a new image with much more pixels in SVG files.
plt.imshow(a,interpolation='none')
plt.savefig('a.svg') Before Below is the <image> part in a text editor. The image has only 4 pixels. It's scaled up to fit the axes. However, SVG viewers will do interpolations such as bilinear for the scaled-up images (Ref). <image height="2" id="imageb494a1e85e" transform="matrix(109 0 0 109 112.68 34)" width="2" xlink:href="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAABHNCSVQICAgIfAhkiAAAABpJREFUCJljdGEM+c/AwMDAxMDAwLDz2QUGAB7eBAuDpB2sAAAAAElFTkSuQmCC"/> Below is the <image> part in a text editor, two new attributes are added. style="image-rendering:pixelated" works. preserveAspectRatio="none" looks not necessary here because <image height="2" id="image2ef7ee41b4" preserveAspectRatio="none" style="image-rendering:pixelated" transform="matrix(109 0 0 109 112.68 34)" width="2" xlink:href="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAGklEQVR4nGN0YQz5z8DAwMDEwMDAsPPZBQYAHt4ECzzPS7MAAAAASUVORK5CYII="/> |
Yes, there may be many different cases... The |
OK, but if I understand correctly, this change turns off antialiasing for svg viewers for full-resolution images as well, not just those with |
I believe it affects |
Reading the discussion and looking at the diff, my understanding is that this comes down to a single line, since the aspect ratio line is not needed now. The basic idea sounds reasonable, and my guess is that this should go in. I think it needs a test, though, verifying that it works as intended for images with interpolation='none' and has no other effect. I don't know what form that test should take. |
Looking at Firefox 75, it doesn't look like 'pixelated' is a valid setting, at least looking at the style editor when viewing an SVG directly. Using 'crisp-edges', 'optimizespeed', or '-moz-crisp-edges' works. I expect you would have to set this multiple times to hit both old and new names. |
The idea looks fine, but I can't review this carefully (in particular browser compat) for now, so unassigning myself. |
A full table of browser compatibility can be found here. It looks like Firefox and IE are different from the rest browsers. I did some tests with Firefox 76, Chrome 81, and Inkscape 0.92 on Win7:
Setting the style to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix flake8.
Update backend_svg.py
Update backend_svg.py
Any reason this shouldn't go in for 3.3? I'll re-milestone, but feel free to undo... |
Thanks a lot for this fix @cover-me ! |
PR Summary
As mentioned in #12065 and #10112, SVG viewers (Chrome, Inkscape...) perform interpolation (blurring) by default. However, for files generated by
imshow(interpolation='none')
, there shouldn't be any interpolation on the viewer's side.image-rendering:pixelated
disable the blurred-interpolation in SVG viewers.'preserveAspectRatio':'none'
allow pixels to be rectangles in SVG viewers. Otherwise, pixels may be rendered as squares only, leading to problems if the image is not 1:1 scaled.SVG is important because the text of PNG in Jupyter notebooks inline mode looks blurred.
PR Checklist
Just a change of two lines.