When we usually use some image processing software, we often see it adjusting the brightness, contrast, chroma or sharpness of the image. Do you think the underlying implementation of this technology is very advanced?
In fact, the most basic implementation principle requires only a few lines of code to be implemented in Python. After learning it, you can also perform simple image enhancement processing.
There is a class called ImageEnhance in the PIL module in Python. This class is specially used for image enhancement processing. It can not only enhance (or weaken) the brightness of the image, Contrast, chroma, and can also be used to enhance the sharpness of an image.
To use this module, you must first install the PIL library:
pip install pillow
image = Image.open('girl.jpeg') image.show()
Our original image is an innocent girl holding a tomato:
enh_bri = ImageEnhance.Brightness(image) brightness = 4 image_brightened = enh_bri.enhance(brightness) image_brightened.show()
In order to make the contrast obvious, we enhance the brightness of the original image 4 times, look at the effect:
The enhanced image is overexposed. Is it a little dazzling?
enh_col = ImageEnhance.Color(image) color = 4 image_colored = enh_col.enhance(color) image_colored.show()
Similarly, we enhance the chroma of the original image by 4 times to see the effect:
This The color of the picture is relatively strong, and I suddenly feel like I have changed from a young girl to a promiscuous woman!
enh_con = ImageEnhance.Contrast(image) contrast = 4 image_contrasted = enh_con.enhance(contrast) image_contrasted.show()
Similarly, we enhance the contrast of the original image by 4 times to see the effect:
This image The details are highlighted very clearly, a bit like an early movie scene.
enh_sha = ImageEnhance.Sharpness(image) sharpness = 4 image_sharped = enh_sha.enhance(sharpness) image_sharped.show()
Similarly, we enhance the sharpness of the original image by 4 times to see the effect:
Sharp After the degree enhancement, it looks pretty good, and the change is not that obvious compared to the original image.
After reading this, do you feel it is very simple? The four most basic image enhancement skills can all be implemented with just one line of code. I am enhancing the image here. You can also do the reverse operation. You only need to adjust the coefficient to less than 1 to weaken the image.
Of course, in actual applications, we will definitely comprehensively optimize these dimensions to achieve the effect of beautiful pictures.
The above is the detailed content of With four lines of code, Python can create beautiful pictures!. For more information, please follow other related articles on the PHP Chinese website!