我能否使用Python调整图像的大小,并指定高度和宽度?我使用的是Python 2.5版本,我尝试了这个教程http://effbot.org/imagingbook/introduction.htm,并安装了PIL库来处理图像,但是当我尝试写下以下代码时:
import Image
im = Image.open("test.jpg")
我从import:open中得到了未定义的变量,虽然import Image
没有出错?提前感谢。
我能否使用Python调整图像的大小,并指定高度和宽度?我使用的是Python 2.5版本,我尝试了这个教程http://effbot.org/imagingbook/introduction.htm,并安装了PIL库来处理图像,但是当我尝试写下以下代码时:
import Image
im = Image.open("test.jpg")
我从import:open中得到了未定义的变量,虽然import Image
没有出错?提前感谢。
你的导入似乎存在问题。请使用以下代码替换 "import Image":
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)
如果您需要使用:刚在官方Pillow网站上发现了这一点。您可能使用的是Pillow而不是PIL。
警告
Pillow >= 1.0不再支持“import Image”。请改用“from PIL import Image”。
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')
这个脚本可以调整给定文件夹中的所有图片大小:
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()
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)
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])
import Image
应该可以工作。这是新版 PIL 的问题吗?我已经使用 PIL 一年多了,只用import Image
导入过它,从来没有遇到过问题... - ely