resize images in python
Asked Answered
N

6

7

Can i resize images in python to given height and width,i use python 2.5, and i tried as this tutorial http://effbot.org/imagingbook/introduction.htm, and i installed PIL library for images,but when i try to write:

import Image
im = Image.open("test.jpg")

i got undefined variable from import:open although import Imagedoesn't give errors? Thanks in advance.

Novation answered 9/4, 2012 at 18:25 Comment(0)
F
7

Your import appears to be the problem. Use this instead of "import Image":

from PIL import Image

Then go on like so:

image = Image.open('/example/path/to/image/file.jpg/')
image.thumbnail((80, 80), Image.ANTIALIAS)
image.save('/some/path/thumb.jpg', 'JPEG', quality=88)
Foreconscious answered 9/4, 2012 at 18:31 Comment(10)
I'm very surprised by this, given that even over at the PIL website, even the tutorials show that using just import Image should work. Is this a newer PIL version thing? I've used PIL for over a year and have only ever imported it with import Image and have never had a problem...Unbreathed
@EMS:the problem may be in the version of python,which ver you use?i use 2.5 ver.Novation
I'm using 2.6 and 2.7. I don't think it's the Python version, unless you're using an older version of PIL. What is your PIL version, mine is 1.1.7.Unbreathed
Strange indeed. Possibly it depends on the way PIL has been installed. In my case, I need this type of import.Foreconscious
Using Python 2.7 with PIL 1.1.7Foreconscious
If you are using a Linux system, it might be as simple as making sure that the PIL .so file appears in the right distutils folder. This might be taken care of automatically with the Linux install script, but perhaps not on other platforms.Unbreathed
@EMS:i installed the latest version these day,but when i tried to resize image using the above tutorial as: 'out = im.resize((128, 128))'the resize method is undefined??Novation
@Nasmoni: used windows OS and intalled Python Imaging Library 1.1.7 for Python 2.5Novation
Yeah, PIL has always been a headache to deal with for me. I try to use only the Mahotas library combined with scikits.image to avoid the need for "special" type images, like what PIL and OpenCV require you to use. I want my images to always just be NumPy arrays and not fool with it.Unbreathed
@Nasmon:when i tried to write image.thumbnail((80, 80), Image.ANTIALIAS) i get that thumbnail is undefined variable from import??Novation
D
3

To whom it may be of use: Just found that on the official Pillow website. You probably used Pillow and not PIL.

Warning

Pillow >= 1.0 no longer supports “import Image”. Please use “from PIL import Image” instead.

Dela answered 30/6, 2015 at 4:17 Comment(0)
H
1

This script resizes all images in a given folder:

import PIL
from PIL import Image
import os, sys
path = "path"
dirs = os.listdir( path )
def resize():
    for item in dirs:
        if os.path.isfile(path+item):
            img = Image.open(path+item)
            f, e = os.path.splitext(path+item)
            img = img.resize((width,hight ), Image.ANTIALIAS)
            img.save(f + '.jpg') 
resize()
Herta answered 16/9, 2019 at 6:24 Comment(0)
A
1
  • you can resize image using skimage

    from skimage.transform import resize
    import matplotlib.pyplot as plt
    
    img=plt.imread('Sunflowers.jpg')
    image_resized =resize(img, (244, 244))
    
  • plotting resized image

    plt.subplot(1,2,1)
    plt.imshow(img)
    plt.title('original image')
    
    plt.subplot(1,2,2)
    plt.imshow(image_resized)
    plt.title('image_resized')
    

Airmail answered 3/1, 2021 at 11:18 Comment(1)
This does not really answer the question. It proposes another method. It could be useful, yes, but is not a direct answer.Aetiology
R
0
import os
from PIL import Image

imagePath = os.getcwd() + 'childFolder/myImage.png'
newPath = os.getcwd() + 'childFolder/newImage.png'
cropSize = 150, 150

img = Image.open(imagePath)
img.thumbnail(cropSize, Image.ANTIALIAS)
img.save(newPath)
Ragwort answered 8/11, 2015 at 21:41 Comment(0)
S
0

if you have troubles with PIL the other alternative could be scipy.misc library. Assume that you want to resize to size 48x48 and your image located in same directory as script

from from scipy.misc import imread
from scipy.misc import imresize

and then:

img = imread('./image_that_i_want_to_resize.jpg')
img_resized = imresize(img, [48, 48])
Schoolmarm answered 13/6, 2017 at 18:36 Comment(1)
Note that on scipy.org it says imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.Whale

© 2022 - 2024 — McMap. All rights reserved.