python --Batch scaling picture (video) size

method one:

In Pillow, the resize() method will force the image to be scaled to the specified size, which may cause deformation or distortion of the image; while the thumbnail() method will scale the image down while maintaining the original aspect ratio without deformation or distortion .

Specifically, the thumbnail() method will calculate an appropriate scaling ratio based on the specified maximum width and height, and then reduce the image according to this ratio, so that the width and height of the final image will not exceed the specified size. The resize() method directly resizes the image to the specified size. If the specified size is different from the aspect ratio of the original image, it will be deformed or distorted.

Therefore, if you want to keep the original aspect ratio of the picture and shrink the picture, it is recommended to use the thumbnail() method; if you need to force the picture to be scaled to a specified size, you can use the resize() method, but be aware that it may cause distortion or deformation.

from PIL import Image
import os

dir = r'C:\Users\1\Desktop\ffmpeg\bin\final_results'
out = r'C:\Users\1\Desktop\ffmpeg\bin\results'
def resize_image():
    for path in os.listdir(dir):
        img = os.path.join(dir, path)
        print(f'缩放图:【{img}】')
        with Image.open(img) as img:
            img.thumbnail((1280, 720))
            img.save(os.path.join(out, path))

# 调用示例
resize_image()

Method Two

import cv2

def resize_image(input_path, output_path, size):
	img = cv2.imread(input_path)
	height, width, _ = img.shape
	if width > height:
		scale = size / width
	else:
		scale = size / height
		img = cv2.resize(img, (int(width * scale), int(height * scale)))
		cv2.imwrite(output_path, img)
	
	# 调用示例
input_path = 'input.jpg'
output_path = 'output.jpg'
size = 640 # 宽度或高度为 640
resize_image(input_path, output_path, size)

In this sample code, the resize_image function receives three parameters: input file path input_path, output file path output_path, and target size size. Inside the function, first use the cv2.imread function to read the input file, and use the shape property to get the width and height of the image. Then calculate the zoom ratio according to the size relationship between the width and the height. Finally, use the cv2.resize function to scale the picture to the specified size, and use the cv2.imwrite function to save the scaled picture to the output file.

It should be noted that the use of the OpenCV library may require the installation of related dependencies first, and the specific installation method can be found in relevant documents. In addition, when using the OpenCV library to scale images, care should be taken to maintain the aspect ratio and avoid distortion or distortion. Specifically, according to the aspect ratio of the picture, the scaling ratio can be calculated and the picture can be reduced to an appropriate size.

method three

from PIL import Image

def resize_image(input_path, output_path, max_size):
    img = Image.open(input_path)
    width, height = img.size
    if max(width, height) > max_size:
        if width > height:
            new_width = max_size
            new_height = int(height * (max_size / width))
    else:
        new_width = int(width * (max_size / height))
        new_height = max_size
        img = img.resize((new_width, new_height))
        img.save(output_path)

# 调用示例
input_path = 'input.jpg'
output_path = 'output.jpg'
max_size = 640 # 最大边长为 640
resize_image(input_path, output_path, max_size)

In this sample code, the resize_image function receives three parameters: the input file path input_path, the output file path output_path, and the maximum side length max_size. Inside the function, first use the Image.open function to read the input file, and use the size property to get the width and height of the image. Then calculate the zoom ratio according to the relationship between the maximum side length and the image size. Finally, use the resize function to scale the picture to the specified size, and use the save function to save the scaled picture to the output file.

It should be noted that when using the resize method of the Pillow library, you should also pay attention to maintaining the aspect ratio of the image to avoid deformation or distortion. Specifically, according to the aspect ratio of the picture, the scaling ratio can be calculated and the picture can be reduced to an appropriate size.

Method 3 (FFmpeg)

The format of the command to scale the video size using ffmpeg is as follows:

ffmpeg -i input.mp4 -vf scale=: output.mp4

Among them, respectively represent the zoomed video width and height, which can be specified as a specific pixel value or a percentage, such as 50%.

input.mp4For example, to scale an input video file to an output video file with a width of 640 pixels and a height of 360 pixels output.mp4, the following command can be used:

ffmpeg -i input.mp4 -vf scale=640:360 output.mp4

If you want to automatically scale according to the original video aspect ratio, you can specify only width or height and keep the original video aspect ratio:

# 指定宽度为 640 像素,高度按原始比例缩放
ffmpeg -i input.mp4 -vf scale=640:-1 output.mp4

# 指定高度为 360 像素,宽度按原始比例缩放
ffmpeg -i input.mp4 -vf scale=-1:360 output.mp4

You can also let ffmpeg preserve the original video aspect ratio during scaling to avoid distortion by scaleadding options before the parameter :force_original_aspect_ratio

ffmpeg -i input.mp4 -vf "scale=640:360:force_original_aspect_ratio=decrease,pad=640:360:(ow-iw)/2:(oh-ih)/2" output.mp4

In this example, padthe filter is used to fill the scaled video to a size of 640x360 pixels while centering it.

Guess you like

Origin blog.csdn.net/weixin_44634704/article/details/129713362