Adjust Image Contrast, Brightness, and Gamma in Python

In image processing and editing, brightness and contrast are manipulated to enhance the visual quality of the images. In addition, these parameters allow you to fine-tune images by reducing overexposure. On the other hand, the gamma parameter is also used to control the brightness of the image. While processing images programmatically, you may have to deal with these parameters. Therefore, in this article, we are going to show you how to adjust brightness, contrast, and gamma of an image in Python.

Python Library to Adjust Image Contrast, Brightness, and Gamma

To adjust image contrast, brightness, and gamma, we will use Aspose.Imaging for Python. It is a powerful yet easy to use image processing library that lets a Python developer manipulate images effortlessly. To use the library, you can either download or install it using the following command.

> pip install aspose-imaging-python-net 

Adjust Contrast of an Image in Python

Contrast refers to how different the colors or brightness levels are in an image. Adjusting contrast makes the objects in an image more clear. High contrast means more sharp and clear image, similar to photos taken in bright sunlight. Whereas, low contrast makes it hard to see and distinguish the objects, just like when it’s foggy outside.

Let’s now see how to adjust the contrast of an image using Python.

  • First, load the image using Image.load() method.
  • Then, cast the object to the RasterImage type.
  • After that, cache the image if it isn’t using RasterImage.cache_data() method.
  • Adjust contrast in the range [-100, 100] using RasterImage.adjust_contrast() method.
  • Finally, save the resultant image using RasterImage.save() method.

The following code sample shows how to adjust the contrast of an image in Python.

The screenshot below shows the image before and after adjusting the contrast.

Adjust Contrast of Images in Python

Adjust Image Contrast

Adjust Brightness of an Image in Python

The brightness is used to increase or decrease the darkness in an image so that we can adjust the visibility of the objects. The following are the steps to modify the brightness of an image in Python.

  • First, load the image using Image.load() method.
  • Then, cast the object to the RasterImage type.
  • After that, cache the image if it isn’t using RasterImage.cache_data() method.
  • Adjust the brightness of the image using RasterImage.adjust_brightness() method.
  • Finally, save the modified image using RasterImage.save() method.

The following code sample shows how to adjust the brightness of an image in Python.

Here is the comparison of input and output images after modifying the value of brightness.

Adjust Brightness of Images in Python

Adjust Image Brightness

Modify Gamma of an Image in Python

Gamma refers to the attribute that controls the ratio of the RGB colors in an image. Along with that, it also modifies the brightness of the image. So let’s see how to adjust the gamma parameter of an image using Python.

  • First, load the image using Image.load() method.
  • Then, cast the object to the RasterImage type.
  • After that, cache the image if it isn’t using RasterImage.cache_data() method.
  • Modify gamma value using RasterImage.adjust_gamma() method.
  • Finally, save the updated image using RasterImage.save() method.

The following code sample shows how to adjust the gamma value of an image in Python.

The image below shows the comparison of input and output images after modifying the gamma value.

Adjust Gamma of Images in Python

Adjust Image Gamma

Free License to Adjust Image’s Contrast, Brightness, and Gamma

You can adjust the contrast, brightness, and gamma of your images without evaluation limitations by getting a free temporary license.

Free Online Image Editor

You can use our free web-based image editing tool to modify your images online. This image editor is powered by Aspose.Imaging for Python and doesn’t require you to create an account.

Conclusion

This article demonstrated how to adjust contrast, brightness, and gamma values in an image using Python. With the help of steps and code samples, we have demonstrated how to modify these parameters in an image. In addition, we have depicted the output using the images. We have also provided you with a free image editing tool, which is based on Aspose.Imaging for Python and it is completely free.

In case you want to explore more about Python image processing library, visit documentation. If you would have any questions or queries, reach us at our forum.

See Also