Resized the folder image in Python.

Asked 2 years ago, Updated 2 years ago, 314 views

I would like to resize the images in the folder at once in Python.
When I google, the resize of the specified size will appear, but

If it is within the scope of the IF, it will not be resized.

if outside the scope of the regulations (less than or equal to) Resize vertically, horizontally, or vertically according to regulations.

I would like to make a decision that

I'm an idiot, so I'm getting confused with IF, so I'd appreciate it if you could let me know.

The regulations are
I want both sides to fit between 3000px and 4000px.
(maintaining aspect ratio) If it doesn't fit, I'd like to adjust the larger one to the upper limit.

Thank you for your cooperation.

python3 image

2022-09-30 22:03

1 Answers

Perhaps it gets complicated and confusing because you try to put them together in one if statement as much as possible.
Originally, it cannot be a simple condition, so you just have to divide it into several steps.

Here's an example of how to think, since it's a bit cheap to present the source code as it is.

By the way, suppose that you have already searched and can process files one by one, or that you can obtain vertical and horizontal sizes from image files or change vertical and horizontal sizes.

You can break down the vertical and horizontal sizes of the images and separate them into the following groups.

And if you add up the vertical as the number of 10 and the horizontal as the number of 1, you'll see the following groupings:

0010203

10 11 12 13

20 21 22 23

30 31 32 33

Among the above, those belonging to 11, 12, 21, and 22 groups are between the upper and lower limits, so they do not change the size.
Also, since the aspect ratio must be maintained and the larger one must be the upper limit, groups of 02,20 whose vertical and horizontal values are exactly the upper limit must not be resized.

The rest of the group is divided into vertical or horizontal values.

  • A. Horizontal is larger than vertical
  • B.Same width and height
  • C. Vertical is larger than Horizontal

Use each of the following actions to derive new values for resizing:

    <li>A. Multiply the vertical size (a quotient obtained by dividing the upper limit value into a floating point value and dividing it by the horizontal size), round off the result and convert it into an integer.
    Horizontal size is set to upper limit (4000)
  • B.Set both horizontal and vertical to the upper limit (4000)
  • <li>C. Multiply the horizontal size (a quotient obtained by dividing the upper limit value by the vertical size) and round the result to an integer.
    Set the vertical size to the upper limit (4000)

You can resize them to the vertical and horizontal values shown in those calculations.

If you program the above as a resize process for each file, you should be able to do what you want.


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.