How do I specify Resize in Pillow?

Asked 2 years ago, Updated 2 years ago, 142 views

Nice to meet you, Python. I'm an amateur on my first day.

When I read the image, I wrote as follows, and I want to resize it without changing the aspect ratio, but I tried various ways of doing the site, but it didn't work.
If possible, I'd like to specify it as a percentage.Thank you for your cooperation.

import PIL.Image
import PIL.ImageTk

defdispPhoto(path):
 newImage=PIL.Image.open(path).resize(300,300))

# ====Continued ===#

python pillow

2022-09-29 21:22

1 Answers

The reason why I can't do it well is because I'm trying to do both the open file and the size conversion that keeps the aspect ratio (aspect ratio) in one line.

PIL.Image.open

PIL.Image.size/PIL.Image.width/PIL.Image.height

PIL.Image.Image.resize

Parameters: size – The requested size in pixels, asa2-tuple: (width, height).

In order to maintain the aspect ratio at resize(), information on the aspect size (in pixels) of the original data is required, but it cannot be retrieved or specified in the same line as Open().
You need to divide it into two lines, such as How to change the image size with Python/Pillow as introduced in the comment.

To specify the destination size as a percentage, you can convert the result to an integer by multiplying 100% by 1.0 and multiplying the original data lengthwise and horizontally by the same value.
The above references have been converted to 0.8 (=80%).

There are other ways to convert either horizontal or horizontal pixels, but not percentage them, or to convert them to the maximum size by specifying the number of vertical and horizontal pixels.
Pillow (PIL)—Provides information on how to resize the image, including the same process as above.


2022-09-29 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.