乐闻世界logo
搜索文章和话题

How to auto resize an image while maintaining aspect ratio

2个答案

1
2

Automatically resizing images while maintaining aspect ratio is a common requirement in image processing to prevent distortion during scaling. To achieve this functionality, we typically follow these steps:

  1. Obtain the original aspect ratio: We first obtain the width and height of the original image and calculate its aspect ratio. Aspect ratio is commonly defined as width divided by height, though it can be height divided by width depending on context.

    python
    original_width = original_image.width original_height = original_image.height aspect_ratio = original_width / original_height
  2. Determine the new dimensions: Based on the desired new size, we define a new width or height and calculate the other dimension to maintain the aspect ratio.

    For example, if we want to set the image width to new_width, we can calculate the new height as follows:

    python
    new_width = desired_width # This is the desired new width new_height = new_width / aspect_ratio

    Alternatively, if we have a desired height new_height, we calculate the new width:

    python
    new_height = desired_height # This is the desired new height new_width = new_height * aspect_ratio
  3. Perform image resizing: With the new width and height, we can use an image processing library (such as the Pillow library) to resize the image.

    python
    from PIL import Image resized_image = original_image.resize((int(new_width), int(new_height)), Image.ANTIALIAS)
  4. Validate the result: After resizing, we should verify that the aspect ratio of the new image matches the original.

    python
    assert abs((resized_image.width / resized_image.height) - aspect_ratio) < tolerance

Here is a practical example. Suppose we have an image file example.jpg and we want to resize it to a width of 500 pixels while maintaining the original aspect ratio:

python
from PIL import Image # Open the original image original_image = Image.open('example.jpg') # Calculate the original aspect ratio original_width, original_height = original_image.size aspect_ratio = original_width / original_height # Set the new width and calculate the new height to maintain aspect ratio new_width = 500 new_height = int(new_width / aspect_ratio) # Resize the image resized_image = original_image.resize((new_width, new_height), Image.ANTIALIAS) # Save or display the resized image resized_image.save('resized_example.jpg') # resized_image.show()

In this code, we use the Pillow library to resize the image while ensuring the new height is automatically calculated based on the desired width to maintain the aspect ratio. This ensures that the resized image does not suffer from stretching or compression distortion.

2024年6月29日 12:07 回复

Here's another approach: object-fit.

shell
<img style='height: 100%; width: 100%; object-fit: contain'/>

This will work. Remember to include other necessary attributes, such as src and alt.

2024年6月29日 12:07 回复

你的答案