Python+opencv is the correct way to open retouching get

Force two sentences:

Images are the most common media format other than text in web applications.

Popular Web still images are JPEG, PNG, ICO, BMP, etc. Motion pictures are mainly in GIF format.
Please add image description
In order to save image transmission traffic, large Internet companies also customize images in special formats, and the WEBP format is a representative.

In addition to data analysis, Python is also very useful for image processing.

Using Python for image processing, the most famous library is PIL (Python Imaging Library). It supports the latest Python3 and has many new features. Pillow has also become one of the essential tools for Python image processing.

However, the library we are going to use today is OpenCV Python, and we will study PIL in the next issue.

This article mainly introduces Python+OpenCV to realize image binarization, to help you better use python to process pictures, and interested friends can learn about it.

Image processing library preparation

OpenCV Python to process images, the installation process is as follows:

pip install opencv-python

Please add image description
image loading

Let's open an image test first. The image can be successfully loaded as follows to indicate that it has been

Successfully installed OpenCV-Python

Please add image description

Image zoom

On the premise of keeping the details of the picture unchanged, the picture can be enlarged or reduced.

Where img is the image object, and img.shape represents the shape and size of the image, which are height, width, and number of channels.

# 获取图片尺寸
img = cv2.imread("./pic1.jpg")
h, w, ch = img.shape
print(h, w, ch)
  
'''
1240 960 3
'''

Our thinking is as follows:

The program reads in the image file, and uses the cv2.resize method to scale the image to half the size.

Python学习交流Q群:660193417###
# 获取图片尺寸
img = cv2.imread("./pic1.jpg")
h, w, ch = img.shape
print('原图尺寸:', h, w, ch)

new_h = int(h / 2)
new_w = int(w / 2)
res = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
cv2.imwrite('./half_pic1.jpg', res)

# 获取图片尺寸
img = cv2.imread("./half_pic1.jpg")
h, w, ch = img.shape
print('缩半原图尺寸:', h, w, ch)
    
'''
原图尺寸:1240 960 3
缩半原图尺寸:620 480 3
'''

The actual effect is as follows:
Please add image description
Please add image description
picture cropping

Take out the partial shape of the picture. Here we take the image of the lady and remove the extra borders around the front, back, left and right according to your needs.

Python学习交流Q群:660193417###
img = cv2.imread("./pic1.jpg")
h, w, ch = img.shape
print(h, w, ch)
# (x0,y0) (x1,y1) 矩阵
x0, y0 = 200, 80
x1, y1 = 880, 960
# img 是一个按行扫描的矩阵
res = img[y0:y1, x0:x1]
print('截取后 H,W=', res.shape[:2])
cv2.imwrite('./pic.jpg', res)

'''
1240 960 3
截取后 H,W= (880, 680)
'''

The actual effect is as follows:
Please add image description
Please add image description
Image composition

Stack and stitch two or more images.

Prepare an original image pic1.jpg and a watermark image img.png. The goal is to paste the watermark on the bottom right of the portrait image.

# 读取原始图片
image = cv2.imread('./pic1.jpg')
(h, w) = image.shape[:2]
print("SOURCE", image.shape)

# 读取水印
imgsy = cv2.imread('./img.png')
(h_sy, w_sy) = imgsy.shape[:2]
print("SHUIYIN", imgsy.shape)

# 定义原图片选区
roi = image[h - h_sy:h, w - w_sy:w]

# 原图片选区和水印区融合,让水印透明
for y in range(h_sy):
    for x in range(w_sy):

        p = imgsy[y, x]
        if (p[0], p[1], p[2]) == (0, 0, 0):
            imgsy[y, x] = roi[y, x]

cv2.imwrite('./shuiyin+roi.png', imgsy)

# 选区范围设定为融合后的水印
image[h - h_sy: h, w - w_sy: w] = imgsy

cv2.imwrite('./pic_sy.jpg', image)

Please add image description
Please add image description

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/124366484