Home  >  Article  >  Backend Development  >  With four lines of code, Python can create beautiful pictures!

With four lines of code, Python can create beautiful pictures!

WBOY
WBOYforward
2023-04-12 21:52:061628browse

With four lines of code, Python can create beautiful pictures!

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.

Which company is best at image enhancement

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 enhancement processing

Read image

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

Our original image is an innocent girl holding a tomato:

With four lines of code, Python can create beautiful pictures!

Brightness enhancement

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:

With four lines of code, Python can create beautiful pictures!

The enhanced image is overexposed. Is it a little dazzling?

Chroma enhancement

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:

With four lines of code, Python can create beautiful pictures!

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!

Contrast enhancement

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:

With four lines of code, Python can create beautiful pictures!

This image The details are highlighted very clearly, a bit like an 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 to see the effect:

With four lines of code, Python can create beautiful pictures!

Sharp After the degree enhancement, it looks pretty good, and the change is not that obvious compared to the original image.

Summary

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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete