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
>
>
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
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to