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

Rarez
  • 129
  • 2
  • 11

1 Answers1

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.

xnx
  • 24,509
  • 11
  • 70
  • 109
  • Thank you! :) Do you know maybe how to use other interpolation way I mean other methods like hanning, hamming etc. ? – Rarez Nov 06 '15 at 15:47
  • 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... – xnx Nov 06 '15 at 15:50