Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing an image in python

I have an image of size (288, 352). I want to resize it to (160, 240). I tried the following code:

im = imread('abc.png')
img = im.resize((160, 240), Image.ANTIALIAS)

But it gives an error TypeError: an integer is required Please tell me the best way to do it.

like image 415
Khushboo Avatar asked Apr 29 '13 10:04

Khushboo


People also ask

How do I manually resize an image in Python?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.

How do I resize an image without cropping in Python?

resize_contain resize the image so that it can fit in the specified area, keeping the ratio and without crop (same behavior as background-size: contain). resize_height resize the image to the specified height adjusting width to keep the ratio the same.

How do I resize an image in a list in Python?

You can resize multiple images in Python with the awesome PIL library and a small help of the os (operating system) library. By using os. listdir() function you can read all the file names in a directory. After that, all you have to do is to create a for loop to open, resize and save each image in the directory.


1 Answers

matplotlib.pyplot.imread (or scipy.ndimage.imread) returns a NumPy array, not a PIL Image.

Instead try:

In [25]: import Image
In [26]: img = Image.open(FILENAME)
In [32]: img.size
Out[32]: (250, 250)

In [27]: img = img.resize((160, 240), Image.ANTIALIAS)

In [28]: img.size
Out[28]: (160, 240)
like image 196
unutbu Avatar answered Oct 06 '22 18:10

unutbu