Prevent matplotlib (imshow) from interpolating at all

Dear list,

I am trying to plot an image using imshow for which i want to have full control of the interpolation taking place. I would rather not have any interpolation at all, but if the input image is very small, it may be interpolated (nearest neighbor) with an integer factor of my choice (2x,3x etc.).

To achieve this i decided that the first and most simple case would be to output exactly the image which i load by using plt.imread(). If this succeeds i would expand it by adding a buffer around the image to allow some annotation like for example a title and a colorbar.

Here is my initial try:

import matplotlib.pyplot as plt

img = plt.imread(‘D:\input_image.png’)

get x and y dimension of the image

xpixels = img.shape[1]
ypixels = img.shape[0]

calculate the dimensions in inch for a given dpi

zoomfactor = 1
dpi = 72
xinch = ( xpixels * zoomfactor) / dpi
yinch = (ypixels * zoomfactor) / dpi

create the figure

fig = plt.figure(figsize=(xinch,yinch))

stretch the axes to the full extent of the figure

ax = plt.axes([0.0, 0.0, 1.0, 1.0])

ax.imshow(img, interpolation=‘nearest’)

hide axis stuff

ax.axes.get_yaxis().set_visible(False)
ax.axes.get_xaxis().set_visible(False)

remove the border of the axes

plt.setp(ax, frame_on=False)

save the figure with the given dpi

plt.savefig(‘D:\output_image.png’, dpi=dpi)

The resulting output image is equal in dimensions to the input image, so far so good. However, when i overlay them in a graphics program, i can clearly see that some nearest neighbor interpolation has taken place. It appears as if the top-right corner of the image is plotted 1 or 2 pixels beyond the edge of the figure. Does anyone have any idea how to get this right?

Thanks in advance!

Regards,
Rutger

If you are using v1.1.0 or later, you can set interpolation to “none”. Note the difference between providing the string “none”, which means to do no interpolation at all, while the python None means to do the default.

Ben Root

···

On Wednesday, July 25, 2012, Rutger Kassies wrote:

Dear list,

I am trying to plot an image using imshow for which i want to have full control of the interpolation taking place. I would rather not have any interpolation at all, but if the input image is very small, it may be interpolated (nearest neighbor) with an integer factor of my choice (2x,3x etc.).

To achieve this i decided that the first and most simple case would be to output exactly the image which i load by using plt.imread(). If this succeeds i would expand it by adding a buffer around the image to allow some annotation like for example a title and a colorbar.

Here is my initial try:

import matplotlib.pyplot as plt

img = plt.imread(‘D:\input_image.png’)

get x and y dimension of the image

xpixels = img.shape[1]
ypixels = img.shape[0]

calculate the dimensions in inch for a given dpi

zoomfactor = 1
dpi = 72
xinch = ( xpixels * zoomfactor) / dpi
yinch = (ypixels * zoomfactor) / dpi

create the figure

fig = plt.figure(figsize=(xinch,yinch))

stretch the axes to the full extent of the figure

ax = plt.axes([0.0, 0.0, 1.0, 1.0])

ax.imshow(img, interpolation=‘nearest’)

hide axis stuff

ax.axes.get_yaxis().set_visible(False)

ax.axes.get_xaxis().set_visible(False)

remove the border of the axes

plt.setp(ax, frame_on=False)

save the figure with the given dpi

plt.savefig(‘D:\output_image.png’, dpi=dpi)

The resulting output image is equal in dimensions to the input image, so far so good. However, when i overlay them in a graphics program, i can clearly see that some nearest neighbor interpolation has taken place. It appears as if the top-right corner of the image is plotted 1 or 2 pixels beyond the edge of the figure. Does anyone have any idea how to get this right?

Thanks in advance!

Regards,
Rutger

Great tip, alot simpler than i expected. Although you still need to specify the correct dimensions of the axes, otherwise it wil be interpolated after all. If in my example i increase the figsize by a factor of 2, the image stretches along with it.

Thanks!

Rutger

···

On 25 July 2012 19:11, Benjamin Root <ben.root@…1304…> wrote:

If you are using v1.1.0 or later, you can set interpolation to “none”. Note the difference between providing the string “none”, which means to do no interpolation at all, while the python None means to do the default.

Ben Root