I think who said that programmers can't make P pictures? Please ~4 lines of python code is enough~

When we usually use some image processing software, we often see it adjust the brightness, contrast, chroma or sharpness of the image. Do you think the underlying implementation of this technology is very high?

In fact, the most basic implementation principle requires only a few lines of code to implement in Python. After learning it, you can also perform simple image enhancement processing.

Which one is better for image enhancement

There is a class called ImageEnhance in the PIL module in Python, which is specially used for image enhancement processing. It can not only enhance (or reduce) the brightness, contrast, and chromaticity of the image, but also can be used to enhance the sharpness of the image.

To use this module, you must first install the PIL library:

pip install pillow

image enhancement

read image

image = Image.open('girl.jpeg')
image.show()

Our original image is of a pure girl holding a tomato:
Please add image description

Brightness enhancement

Python学习交流Q群:660193417###
enh_bri = ImageEnhance.Brightness(image)
brightness = 4
image_brightened = enh_bri.enhance(brightness)
image_brightened.show()

For obvious contrast, we enhance the brightness of the original image by 4 times and see the effect:
Please add image description
the enhanced image is overexposed, is it a little dazzling?

Chroma enhancement

Python学习交流Q群:660193417###
enh_col = ImageEnhance.Color(image)
color = 4
image_colored = enh_col.enhance(color)
image_colored.show()

Similarly, we increase the chromaticity of the original image by 4 times and see the effect:
Please add image description
the color of this image is relatively strong, and it suddenly becomes a thermal image...

contrast enhancement

enh_con = ImageEnhance.Contrast(image)
contrast = 4
image_contrasted = enh_con.enhance(contrast)
image_contrasted.show()

Again, we increased the contrast of the original image by a factor of 4 and see the effect:
Please add image description
this image brings out the details, a bit like a very early movie scene.

sharpness enhancement

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, and see the effect:
Please add image description
after the sharpness is enhanced, it looks good, and the change is not so obvious compared with the original image.

Summarize

After reading it, does it feel very simple? Miss is so pretty. The four most basic image enhancement skills can all be achieved with just one line of code. I am here to
enhance the image, you can also do the reverse operation, you only need to adjust the coefficient to less than 1 to achieve image reduction.

Of course, in practical applications, we will definitely optimize these dimensions comprehensively to achieve the effect of Meitu.

Well, programmers can also P pictures, okay?

Please add image description

Guess you like

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