我可以调整在Python图像给定的高度和宽度,我使用Python 2.5,我试图为这个教程http://effbot.org/imagingbook/introduction.htm,我安装了PIL库图像,但是当我尝试写:调整图像在Python
import Image
im = Image.open("test.jpg")
我从导入中得到了未定义的变量:打开 虽然import Image
不会给出错误? 在此先感谢。
我可以调整在Python图像给定的高度和宽度,我使用Python 2.5,我试图为这个教程http://effbot.org/imagingbook/introduction.htm,我安装了PIL库图像,但是当我尝试写:调整图像在Python
import Image
im = Image.open("test.jpg")
我从导入中得到了未定义的变量:打开 虽然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)
向谁可能是有用的:刚发现在official Pillow website。你可能使用枕头而不是PIL。
警告
枕头> = 1.0不再支持“进口图像”。请使用“从PIL 导入图像”代替。
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)
如果你有PIL烦恼其他替代可能是scipy.misc库。假设你要调整到尺寸48×48和图像位于同一目录中的脚本
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])
:是的,这是被这个问题,谢谢 – 2012-04-09 18:33:24
我很惊讶鉴于此,即使在PIL网站上,[甚至是教程](http://www.pythonware.com/library/pil/handbook/introduction.htm)都表明只使用'import Image'应该可以工作。这是一个更新的PIL版本的东西?我已经使用了PIL一年多,并且只有用'import Image'导入它,并且从来没有遇到过问题... – ely 2012-04-09 18:35:20
@EMS:问题可能出现在python版本中,您使用哪个版本?我使用2.5版本 – 2012-04-09 18:38:46