inline images have low quality

MatplotlibIpython NotebookScikit Image

Matplotlib Problem Overview


I'm loading a TIF file with scikit-image and displaying it inline in an ipython notebook (version 2.2.0). This works, however, the image is quite small when first displayed, and when I resize it using the draggable handle on the bottom right of the image, it just rescales the image while retaining the resolution of the original, so it's very blurry when enlarged. It's basically as if ipython is converting my image into a thumbnail on the fly.

I've tried using matplotlib's plt.imshow() as well, which has the exact same result. I'm starting the notebook with ipython notebook --pylab inline.

from skimage import io
import matplotlib.pyplot as plt
image_stack = io.MultiImage("my_image.tif")
image = image_stack[0]  # it's a multi-page TIF, this gets the first image in the stack

io.imshow(image)  # or plt.imshow(image)
io.show()  # or plt.show()

Matplotlib Solutions


Solution 1 - Matplotlib

To change the "%matplotlib inline" figure resolution on the notebook do:

import matplotlib as mpl
mpl.rcParams['figure.dpi']= dpi

I recommend setting the dpi somewhere between 150 and 300 if you are going to download/print the notebook. Ensure that %matplotlib inline runs before the mpl.rcParams['figure.dpi']= dpi otherwise the magic command resets the dpi to its default value (credits to @fabioedoardoluigialberto for noticing this).

The snipppet below only changes the dpi of figures saved through the savefig method, not of inline generated figures.

import matplotlib as mpl
mpl.rc("savefig", dpi=dpi)

Solution 2 - Matplotlib

According to https://www.reddit.com/r/IPython/comments/1kg9e2/ipython_tip_for_getting_better_quality_inline/

You could also execute this magic in your cell:

%config InlineBackend.figure_format = 'svg'

The print quality will look significantly better. You can also change svg to retina, to use higher-res PNGs (not as nice). Nevertheless, note that if your picture becomes too complicated, the svg picture will have a much larger size than that of the retina picture

Solution 3 - Matplotlib

The resolution of inline matplotlib figures is downscaled a bit from what you would see in a GUI window or saved image, presumably to save space in the notebook file. To change it, you can do:

import matplotlib as mpl
mpl.rc("figure", dpi=dpi)

Where dpi is some number that will control the size/resolution of the inline plots. I believe the inline default is 80, and the default elsewhere with matplotlib is 100.

The reason scaling the resulting plot by dragging the handle doesn't work is that the plot is rendered as a png, so scaling it zooms but does not change the intrinsic resolution.

Solution 4 - Matplotlib

Assuming this is the same thing that happens with iPython notebook (with %matplotlib inline) when you go to drag and resize the image, the fix is fairly simple.

If you just create a figure with a different default size, then the resolution also increases with the size of the default (https://stackoverflow.com/questions/24185083/change-resolution-of-imshow-in-ipython). For example:

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111)
ax.imshow(array)

Something like this should increase the resolution of the thing you are trying to plot. This seemed to work for me with your code:

from skimage import io
import matplotlib.pyplot as plt
%matplotlib inline

image_stack = io.MultiImage("my_image.tif")
image = image_stack[0] 

fig = plt.figure(figsize= (20,20)) #create an empty figure to plot into with 20x20 size
io.imshow(image)
io.show()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjimbotronView Question on Stackoverflow
Solution 1 - MatplotlibFrancio RodriguesView Answer on Stackoverflow
Solution 2 - MatplotlibCosta HuangView Answer on Stackoverflow
Solution 3 - MatplotlibmwaskomView Answer on Stackoverflow
Solution 4 - MatplotlibnanomanView Answer on Stackoverflow