inline images have low quality

jimbotron picture jimbotron · Aug 20, 2014 · Viewed 33k times · Source

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()

Answer

Francio Rodrigues picture Francio Rodrigues · Sep 11, 2017

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)