1

I'm trying to change the size of an image, turning larger images into smaller images and vice versa, with PIL. I have done various tests, but I cannot get the result I want. Here are the tests I did with a 900x1600 image:

Same input every time: Image

size = (1080, 1080)
userImage = Image.open("./Images/UsersImages/001.png")
userImage.resize(size)
userImage.show()
size = (1080, 1080)
userImage = Image.open("./Images/UsersImages/001.png")
userImage.thumbnail(size, Image.ANTIALIAS)
userImage.show()
userImage = Image.open("./Images/UsersImages/001.png")
userImageWidth, userImageHeight = userImage.size

leftBorder = 4
topBorder = userImageHeight / 5
rightBorder = 154
bottomBorder = 3 * userImageHeight / 5
userImage = userImage.crop((leftBorder, topBorder, rightBorder, bottomBorder))

newsize = (1080, 1080)
userImage = userImage.resize(newsize)
userImage.show()
userImage = Image.open("./Images/UsersImages/001.png")
userImageWidth, userImageHeight = userImage.size
    
if userImageWidth > 1080:
    bottomBorder = (userImageWidth - 1080) / 2
    topBorder = bottomBorder
else:
    bottomBorder, topBorder = 0, 0

if userImageHeight > 1080:
    leftBorder = (userImageHeight - 1080) / 2
    rightBorder = leftBorder
else:
    leftBorder, rightBorder = 0, 0

userImage = userImage.crop((leftBorder, topBorder, rightBorder, bottomBorder))
userImage.show()

In most cases, the photo remains exactly the same as before. In cases with crop, it is cut very small.

How can I resize any image to 1080x1080? I don't care if the photo is stretched. The important thing is that any type of image, whether larger or smaller, is resized to 1080x1080.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Simonalbi
  • 316
  • 2
  • 14
  • 1
    It might be helpful if you tell us what the results of your tests were (i.e. why did they not work). – Allan Wind Sep 30 '21 at 16:19
  • In most cases, the photo remains exactly the same as before. In cases with crop, it is cut very small. – Simonalbi Sep 30 '21 at 16:21
  • Does this answer your question? [How do I resize an image using PIL and maintain its aspect ratio?](https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio) – wovano Oct 22 '21 at 08:27
  • Did you read the [documentation or `resize()`](https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize)? There's an even an example below it. – wovano Oct 22 '21 at 08:28

1 Answers1

4

Use userImage = userImage.resize(size), because resize() returns a copy of the image, resized; it doesn't actively resize the image.

size = (1080, 1080)
userImage = Image.open(f"./Images/UsersImages/001.png")
userImage = userImage.resize(size) ### EDITED LINE
userImage.show()
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
  • Perfect, thank you very much it worked. Just out of curiosity, is there also a way to avoid stretching it? – Simonalbi Sep 30 '21 at 16:32
  • 1
    @Jollyker7, to avoid stretching, see https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio – wovano Oct 22 '21 at 08:29
  • This is the answer, thanks for the reminder that resize returns an image, doesn't affect the image itself. – commadelimited May 07 '23 at 23:15