Coup! Python quickly handles image processing

If you are a developer in the field of computer vision, image recognition, image processing or machine learning, then Python is undoubtedly your best friend. The Python ecosystem provides a rich set of machine learning, data analysis, and computer vision libraries such as TensorFlow, Keras, PyTorch, Numpy, Pillow, and OpenCV, which are well suited for image processing and computer vision.

In this article, I'll show you how to use Python and the Pillow library, and how to handle various images properly with OpenCV.

Install the Pillow library

As always, if you want to use the Pillow library, you first need to install it. The following commands can be run on the command line:

pip install Pillow

Import and display images

Opening image files in Python is very simple. Just use  Image the classes and  show() methods of the Pillow library, for example:

from PIL import Image

# 导入图像文件
img = Image.open("img.jpg")

# 显示图像
img.show()

Scale and resample images

Scaling and resampling images is one of the most fundamental operations in image processing. The two most common scaling and resampling techniques are shrinking the image and removing pixels and linearly interpolating the image to enlarge the image.

The Pillow library provides  resize() methods to implement this process:

image = Image.open("img.jpg")

# 将图像大小变为原来大小的一半
image = image.resize((int(image.width/2), int(image.height/2)))

# 改变图像大小,宽变为100,高按比例缩放
new_image = image.resize((100, int(image.height / image.width * 100)))

crop image

Cropping an image means removing something from the original image. This operation is very common, such as removing irrelevant regions in garbage images. crop() This can be done using functions in the Pillow library  .

The following example will demonstrate how to crop the center of a set with size (200, 200) into a square.

img = Image.open("img.jpg")

width, height = img.size
new_width = new_height = 200

x = (width - new_width) / 2
y = (height - new_height) / 2
img = img.crop((x, y, x + new_width, y + new_height))

Adjust image brightness, contrast, and saturation

Adjusting image brightness, contrast, and saturation is a common operation method in image processing. The OpenCV library provides some functions that can accomplish this task, the most common ones are  cv2.cvtColor() and  cv2.addWeighted(). cv2.cvtColor() The function is used to set the color channel, and  cv2.addWeighted() the function is used to add two images and blend them with the given weight.

Image.open('img.jpg')
	img = img.convert('HSV')
    brightness_multiplier = 1.2
    saturation_multiplier = 1.1

    # 调整亮度和饱和度
    img = ImageEnhance.Brightness(img).enhance(brightness_multiplier)
    img = ImageEnhance.Color(img).enhance(saturation_multiplier)
    img.show()

equalize image

Image histogram equalization is a technique that can improve image contrast. Compared to other image processing techniques, it is a very simple process that walks you through it in seconds. The OpenCV library provides  cv2.equalizeHist() functions to implement this operation, while using the Pillow library requires converting the image to grayscale first and then processing it accordingly.

# 使用Pillow进行灰度处理
img = Image.open('img.jpg')
gray = img.convert('L')

# 调用Pillow直方图均衡函数
eq = ImageOps.equalize(gray)

Summarize

The Pillow library provides many useful functions that can be used to perform various manipulations on images. For example, you can use Pillow to scale images, adjust brightness and saturation, equalize histograms, and even draw various shapes, fonts, colors, lines, etc. on images. Compared with the Pillow library, the OpenCV library is more powerful in image processing. functions support a large number of image processing and computer vision functions, and these functions are relatively stable and reliable. Hope this tutorial gave you a fresh perspective on image processing and computer vision in Python.

Guess you like

Origin blog.csdn.net/weixin_62757215/article/details/130552101