Reputation: 321
I am working in Blender and so I wanted to use a bump map I found on the internet. The image is here. However, when I tried to use it, Blender crashed. I am assuming that happened because of the size of the image. That's why I wanted to create a simple script that would resize an image.
This is my script:
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)
When I run the script I get the following error:
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
As you can see from the script, I already tried changing the mode to "RGB" (line 14), assuming that would fix the problem.
The image is quite large but I don't get the memory error.
I am stuck on that problem for quite a while and I just don't know what the problem is. Any help would be appreciated.
Upvotes: 3
Views: 15371
Reputation: 1168
Try to play with resample
parameter of resize()
method.
Although this parameter is optional according to the provided docstring:
resample – An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC (cubic spline interpolation), or PIL.Image.LANCZOS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.
I guess explicitly specifying it may help. At least it helped me.
Upvotes: 1