Computer_Engineer
Computer_Engineer

Reputation: 403

resize images in python

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.

Upvotes: 7

Views: 20655

Answers (6)

Nourhan_selim
Nourhan_selim

Reputation: 111

  • 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')
    

Upvotes: 1

Khemaji Thakor
Khemaji Thakor

Reputation: 11

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()

Upvotes: 1

Yura Vasiliuk
Yura Vasiliuk

Reputation: 101

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])

Upvotes: 0

Buldumac
Buldumac

Reputation: 1

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)

Upvotes: 0

flaudre
flaudre

Reputation: 2298

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.

Upvotes: 3

Simon Steinberger
Simon Steinberger

Reputation: 6825

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)

Upvotes: 7

Related Questions