Python PIL";ValueError:图像的模式错误“;调整图像大小时

Python PIL";ValueError:图像的模式错误“;调整图像大小时,python,image,python-imaging-library,image-resizing,Python,Image,Python Imaging Library,Image Resizing,我在Blender工作,所以我想用我在网上找到的凹凸贴图。 这张照片很有趣。然而,当我试图使用它,搅拌机崩溃。我假设这是因为图像的大小。这就是为什么我想创建一个简单的脚本来调整图像的大小 这是我的剧本: from PIL import Image from math import sqrt path = raw_input("Enter file path: ") Image.warnings.simplefilter('ignore', Image.DecompressionBombWar

我在Blender工作,所以我想用我在网上找到的凹凸贴图。 这张照片很有趣。然而,当我试图使用它,搅拌机崩溃。我假设这是因为图像的大小。这就是为什么我想创建一个简单的脚本来调整图像的大小

这是我的剧本:

from PIL import Image
from math import sqrt

path = raw_input("Enter file path: ")

Image.warnings.simplefilter('ignore', Image.DecompressionBombWarning)
img = Image.open(path)
size = img.size
pixelsize = size[0] * size[1]
print "Current pixel size:", pixelsize

maxsize = input("Enter maximum pixel size: ")

img = img.convert(mode="RGB")
square1 = sqrt(pixelsize)
ratio = (size[0] / square1, size[1] / square1)
square2 = sqrt(maxsize)
newsize = (int(round(maxsize * ratio[0])), int(round(maxsize * ratio[1])))
img = img.resize(newsize)

oldname = path.split("/")[-1]
newname = "SMALLER_" + oldname

img.save(newname)
运行脚本时,出现以下错误:

Traceback (most recent call last):
  File "C:/Users/*****/Desktop/smaller/makeImageSmaller.py", line 19, in <module>
    img = img.resize(newsize)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1550, in resize
    return self._new(self.im.resize(size, resample))
ValueError: image has wrong mode
回溯(最近一次呼叫最后一次):
文件“C:/Users/*****/Desktop/small/makeImageSmaller.py”,第19行,在
img=img.resize(新闻大小)
文件“C:\Python27\lib\site packages\PIL\Image.py”,第1550行,在resize中
返回self.\u新建(self.im.resize(大小、重采样))
ValueError:图像的模式错误
正如您从脚本中看到的,我已经尝试将模式更改为“RGB”(第14行),假设这样可以解决问题

图像相当大,但我没有得到内存错误


我被这个问题困扰了很长一段时间,我只是不知道问题出在哪里。任何帮助都将不胜感激。

尝试使用
resize()
方法的
resample
参数。 尽管根据提供的文档字符串,此参数是可选的:

重采样–可选的重采样过滤器。这可能是其中之一 PIL.Image.NEAREST(使用最近邻),PIL.Image.billian(线性 插值),PIL.Image.BICUBIC(三次样条插值),或 PIL.Image.LANCZOS(高质量下采样滤波器)。如果省略,或 如果图像具有模式“1”或“P”,则设置为PIL.image.NEAREST


我想明确指定它可能会有所帮助。至少对我有帮助。

你试过将图像的模式转换为“L”吗?这对我来说是一个“我”;16'图像(参见)。