I'm trying to crop images to 1000, 1000 using PIL in Python.
However, it always stretches the image to match the dimensions rather than cropping. Current code below.
Preferably, I would like to crop the original images evenly on the right and left while extending or reducing the height to 1000.
from PIL import Image
img = Image.open('image.jpg')
new_img = img.resize((1000,1000))
new_img.save("image.jpg", "JPEG", optimize=True)
new_img.show()
You can use the image.crop
method to crop the sides, followed by a image.resize
to extend or reduce the height to 1000:
(width, height) = img.size
left = int((width - 1000)/2)
right = left + 1000
new_img = img.crop((left, 0, right, height))
new_img = new_img.resize((1000,1000))
In the python you can use the resize image with PILL library in this you want to import the PILL module
from PIL import Image
im = Image.open("images/cat.jpg")
im.show()
resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))
resized_im.show()
resized_im.save('resizedBeach1.jpg')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.