Python PIL (pillow) library’s ImageEnhance module has lot of convenient methods that can be used to make basic image adjustments such as brightness, contrast, sharpness and saturation.
In this Python tutorial, we’re going to show you how to adjust brightness, contrast, sharpness and saturation of an image using PIL (pillow) library in Python.
Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.
from PIL import Image, ImageEnhance
file = "stairs.jpg"
img = Image.open()
img.show()
To change brightness of the image we can apply ImageEnhance.Brightness()
method.
To do this we need to:
filter = ImageEnhance.Brightness(img)
img.filter(float)
filter = ImageEnhance.Brightness(img)
new_image = img.filter(1.2)
new_image.show()
If you encounter an error with the path to your image because there can be operating system conflicts especially regarding directory representation.
In most cases it helps to type the file path in raw string format or use backslashes instead of regular slashes.
At this point your image will be assigned to img variable but nothing will happen until you use the .show()
method as below.
After opening an image and assigning it to a variable named img, you can increase its brightness with a factor of 1.2 as below:
filter = ImageEnhance.Brightness(img)
new_image = img.filter(1.2)
new_image.show()
To change contrast of the image we can apply ImageEnhance.Contrast() method.
To do this we need to:
filter = ImageEnhance.Contrast(img)
img.filter(float)
.Contrast
method an then a new image is created with a contrast factor of 2.0.
filter = ImageEnhance.Contrast(img)
new_image = img.filter(2)
new_image.show()
To change brightness of the image we can apply ImageEnhance.Brightness()
method.
To do this we need to:
filter = ImageEnhance.Sharpness(img)
img.filter(float)
And here is a Python example that applies a sharpness of 4 to an image contained in variable named img.
filter = ImageEnhance.Sharpness(img)
new_image = img.filter(4)
new_image.show()
To change brightness of the image we can apply ImageEnhance.Brightness() method.
To do this we need to:
filter = ImageEnhance.Color(img)
img.filter(float)
.Color
method of ImageEnhance
module.
filter = ImageEnhance.Color(img)
new_image = img.filter(3)
new_image.show()
This PIL tutorial builds on fundamental image editing concepts such as opening an image and showing an image. You can navigate to our basic Python lessons as well as more advanced Python Lessons or our other PIL Tutorials.
Basic:
You can also check out PIL (pillow) library’s official documentation.
In this Python Tutorial, we’ve seen how to adjust brightness, contrast, sharpness and color saturation of an image using ImageEnhance method.
We have also demonstrated each image processing operation with a Python example.
Also, we have build on the imaging basics we have learned in previous PIL (pillow) library tutorials such as open() and show() methods.