在python中调整图像大小

时间:2012-04-09 18:25:51

标签: image resize python-2.5

我可以将python中的图像调整到给定的高度和宽度,我使用python 2.5,我尝试了本教程http://effbot.org/imagingbook/introduction.htm,我为图像安装了PIL库,但是当我尝试写:

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

我从导入中得到了未定义的变量:open 虽然import Image不会出错? 提前谢谢。

6 个答案:

答案 0 :(得分:2)

您的导入似乎是问题所在。使用此代替“导入图像”:

from PIL import Image

然后继续这样:

image = Image.open('/example/path/to/image/file.jpg/')
image.thumbnail((80, 80), Image.ANTIALIAS)
image.save('/some/path/thumb.jpg', 'JPEG', quality=88)

答案 1 :(得分:1)

可能对谁有用:刚刚在official Pillow website找到了。你可能使用Pillow而不是PIL。

  

警告

     

Pillow> = 1.0不再支持“导入图片”。请使用“来自PIL   导入图像“而不是。

答案 2 :(得分:1)

此脚本将调整给定文件夹中所有图像的大小:

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

答案 3 :(得分: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)

答案 4 :(得分:0)

如果您遇到PIL问题,另一种选择可能是scipy.misc库。假设您要调整大小为48x48,并且您的图像与脚本

位于同一目录中
from from scipy.misc import imread
from scipy.misc import imresize

然后:

img = imread('./image_that_i_want_to_resize.jpg')
img_resized = imresize(img, [48, 48])

答案 5 :(得分:0)

  • 您可以使用 skimage

    调整图像大小
    from skimage.transform import resize
    import matplotlib.pyplot as plt
    
    img=plt.imread('Sunflowers.jpg')
    image_resized =resize(img, (244, 244))
    
  • 绘制调整大小的图像

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