Home > Article > Backend Development > I was traveling with my girlfriend for three days, and Python cured my mental insufficiency...
I took annual leave a while ago and went on a long trip with my girlfriend. On the way, I felt happy and the scenery was pleasant, but I was also really tired, especially after not having a good rest for several days and having to bump all the way home.
Who would have thought that as soon as I stepped home and was about to take a rest, I would receive this message:
Save the pictures one by one, unexpectedly She actually took two hundred photos in four days.
This is a bit difficult for me: First of all, my current work does not require cutting pictures, and PS has been uninstalled a long time ago. Secondly, even if I have PS, it is enough to just apply presets to hundreds of pictures. It will take a while to get it done. It’s torture just thinking about being tired and sleepy and having to do things.
I was a little at a loss, so I looked at these photos carefully while thinking about it:
Since the quality of the original pictures is okay, it doesn’t actually require much editing. Operation, because there are many clouds that day and the light is not very good, the color of the photo is a bit dull. You can increase the saturation appropriately and reduce the brightness at the same time to get a good photo.
At this time, I suddenly remembered an API I used when looking at OpenCV a while ago. With the help of python, we might be able to quickly process these hundreds of pictures.
First of all, let’s introduce HSV. HSV is a color space. Unlike RGB, which describes colors through a combination of red, green and blue, HSV splits colors into hue (H), saturation (S) and lightness ( V) three dimensions, which can more directly express the lightness, darkness and vividness of colors, so it is widely used in the field of image recognition.
With the help of opencv's split() function, we can separate the HSV variables of the picture, and then modify it and then use the merge() function to synthesize a new picture to achieve Batch modify the effects of saturation and brightness. At the same time, split() can also separate the three RGB color channels of the image, and then modify a channel independently.
Without further ado, let’s start the operation:
import cv2 import numpy as np import os def modify_image(img_path, target_dir): # 读取全部I was traveling with my girlfriend for three days, and Python cured my mental insufficiency... pic = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) # 将I was traveling with my girlfriend for three days, and Python cured my mental insufficiency...修改为HSV pichsv = cv2.cvtColor(pic, cv2.COLOR_BGR2HSV) # 提取饱和度和明度 H,S,V = cv2.split(pichsv) # S为饱和度,V为明度 new_pic = cv2.merge([np.uint8(H), np.uint8(S*1.4), np.uint8(V*0.9)]) # 将合并后的I was traveling with my girlfriend for three days, and Python cured my mental insufficiency...重置为RGB pictar = cv2.cvtColor(new_pic, cv2.COLOR_HSV2BGR) # 获取原文件名 file_name = img_path.split("/")[-1] # 将I was traveling with my girlfriend for three days, and Python cured my mental insufficiency...写入目录 cv2.imwrite(os.path.join(target_dir, file_name), pictar) root, dirs, files = next(os.walk("./test/")) for item in files: img_path = os.path.join(root,item) process_image(img_path, "./target/")
After three clicks, five and two additions, I got the code. Just by looking at the phone, hundreds of pictures were processed. The left side is before modification, and the right side is after modification. You can see that the effect is still very obvious, and the color is much fuller.
All the pictures were done in a few minutes. My girlfriend looked in disbelief, but she was very satisfied with the result. Of course I won't tell her how I did it.
The above is the detailed content of I was traveling with my girlfriend for three days, and Python cured my mental insufficiency.... For more information, please follow other related articles on the PHP Chinese website!