Modify image quality and resolution in batches (python)

How to use Python's OpenCV to modify image quality and resolution in batches

When deep learning is making data sets, in most cases, it does not need high-resolution pictures as the original data set. The following program uses python and opencv libraries to realize image degradation processing. Help us modify the quality and resolution of images in batches. This article will show how to use Python's OpenCV library to achieve this function.

1. Install the OpenCV library

Before we start, we need to make sure we have installed Python and the OpenCV library. If you have not installed the OpenCV library, you can use the following command to install it:

pip install opencv-python

2. Import the OpenCV library and other necessary libraries

Before we start writing code, we need to import the OpenCV library and other necessary libraries:

import cv2
import os

3. Set the input and output directories

Before we start batch modifying image quality and resolution, we need to set up the input and output directories. The input directory is the path to the folder containing the images to be processed, and the output directory is the path to the folder where the processed images are saved. We can use osthe library to create a directory:

input_folder = 'input/'
output_folder = 'output/'

os.makedirs(output_folder, exist_ok=True)

4. Modify image quality and resolution in batches

Now, we can start modifying image quality and resolution in batches. We can cv2.imread()read images using methods, cv2.imwrite()save images using methods, and cv2.resize()modify image resolution using methods. Here is a sample code:

for filename in os.listdir(input_folder):
    if filename.endswith('.jpg') or filename.endswith('.png'):
        image = cv2.imread(os.path.join(input_folder, filename))
        
        # 修改图像分辨率
        resized_image = cv2.resize(image, (800, 600))

        # 修改图像质量
        jpeg_file = os.path.join(output_folder, filename)
        cv2.imwrite(jpeg_file, resized_image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])

The above code traverses all image files in the input directory to determine whether they end with '.jpg' or '.png'. Then, use cv2.resize()the method to modify the resolution of the image, and use cv2.imwrite()the method to modify the quality of the image by adjusting parameters when saving the image cv2.IMWRITE_JPEG_QUALITY.

5. Conclusion

By using Python's OpenCV library, we can easily modify the quality and resolution of images in batches. Just set the input and output directories and call the corresponding functions to quickly process image data in batches. I hope this article helps you understand and use Python's OpenCV library.

以下是一个小示例

import cv2
import os
address = r"C:\Users\lenovo\Desktop\paperNeed" # 存放原图片文件夹路径
list = os.listdir(address)
for i, file in enumerate(list):
    firstname = os.path.splitext(file)[0]
    typename = os.path.splitext(file)[1]
    os.rename("{}/{}".format(address, file), "{}/{}.jpg".format(address, i))
    newpath = address + "/"+"{}.jpg".format(i)

    cv2.namedWindow("Image")  # 创建窗口
    img = cv2.imread(newpath)
    cv2.imshow("Image", img)
    changepic = r"C:\Users\lenovo\Desktop\paperNeed2\{}.jpg".format(i) # 修改完质量的图片存放路径
    cv2.imwrite(changepic, img, [int(cv2.IMWRITE_JPEG_QUALITY), 10])

cv2.waitKey(0)
cv2.destroyWindow("Image")  # 关闭窗口

The main quality-changing code is:

 cv2.imwrite(changepic, img, [int(cv2.IMWRITE_JPEG_QUALITY), 10])
Different image formats apply different parameters

If the image format is .jpeg or .jpg format, select the cv2.CV_IMWRITE_JPEG_QUALITY parameter, and its value is 0-100 (the larger the value, the higher the image quality).
If the image format is .webp format, use the cv2.CV_IMWRITE_WEBP_QUALITY parameter, the value 0-100
If the image format is .png format cv2.CV_IMWRITE_PNG_COMPRESSION parameter, the value is 0-9

Guess you like

Origin blog.csdn.net/m0_46114594/article/details/116069109
Recommended