Maybe this is because I'm a newby to Python. But I don't seem to be able to resize and save images.
Can somebody help me by telling me what I'm doing wrong here? I'm resizing first, and secondly cropping an image to 256x256. The output is saved as the original image. Function call like: resizeAndCrop("path/to/image.png")
Current behavior is the script saving the image in the original size...
# function for resizing and cropping to 256x256
def resizeAndCrop(imgPath):
im = Image.open(imgPath)
# remove original
os.remove(imgPath)
# Get size
x, y = im.size
# New sizes
yNew = 256
xNew = yNew # should be equal
# First, set right size
if x > y:
# Y is smallest, figure out relation to 256
xNew = round(x * 256 / y)
else:
yNew = round(y * 256 / x)
# resize
im.resize((int(xNew), int(yNew)), PIL.Image.ANTIALIAS)
# crop
im.crop(((int(xNew) - 256)/2, (int(yNew) - 256)/2, (int(xNew) + 256)/2, (int(yNew) + 256)/2))
# save
print("SAVE", imgPath)
im.save(imgPath)
resize()returns a resized copy of an image, so you need to assign the result of the operation otherwise the new image is lost. The same is true ofcrop()with the added wrinkle that "This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To get a separate copy, call theloadmethod on the cropped copy."newImage = im.resize((int(xNew),.......?