2

I have problem with my basic script which load image, then convert it to array, zooming by interpolation and should save zoomed image to file but this last line don't work. I have no idea how to fix it, i know that problem it is with zoomed array. I will be very glad for any help.

Script Code:

import numpy as np
import scipy.ndimage as ndimage
from scipy import misc
import matplotlib.pyplot as plt

x= misc.imread('img400x400.jpg')
x2= ndimage.zoom(x, 2, order=0)
#print x
#print x2
#plt.imshow(x)
plt.savefig(x2)

Output that I got:

Traceback (most recent call last): File "imgpolar.py", line 11, in plt.savefig(x2) File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 561, in savefig return fig.savefig(*args, **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig self.canvas.print_figure(*args, **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 517, in print_png filename_or_obj, self.figure.dpi) TypeError: Object does not appear to be a 8-bit string path or a Python file-like object

1 Answer 1

2

Firstly, you probably only want to zoom along the first two axes of your image array (not the RGB axis, or you'll end up with an array of the wrong shape, (nx, ny, 6)). You can specify which axes to zoom as a tuple:

x2 = ndimage.interpolation.zoom(x, (2,2,1), order=0)

Secondly, you need to save the image using ndimage.imsave: plt.savefig expects a filename to save the current plot to (and there isn't one), not an array. So, try using:

misc.imsave(x2, 'interp.jpg')

Where interp.jpg is whatever you want to call your zoomed image.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! :) Do you know maybe how to use other interpolation way I mean other methods like hanning, hamming etc. ?
As far as I know, ndimage.interpolation.zoom just does spline interpolation. You might ask a new SO question to elicit a better answer on this...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.