Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Enhance image with Python PIL
petercour
petercour

Posted on

     

Enhance image with Python PIL

Python can be used to play around with images. This is a lot more fun than text. In this article we'll use a simple jpg image. That jpg image will be manipulated.

Images can be enhanced with Python PIL. What does enhanced mean?
It means change in:

  • Brightness(image)
  • Color(image)
  • Contrast(image)
  • Sharpness(image)

As input image we'll take the famous Lena image. This is an image that's often used in image processing.

And so you code

Load the PIL module like this:

from PIL import Imagefrom PIL import ImageEnhance
Enter fullscreen modeExit fullscreen mode

To load and show an image:

image = Image.open('lena.jpg')image.show()
Enter fullscreen modeExit fullscreen mode

The image 'lena.jpg' must be in the same directory as program. If it's not, write a path to the image in front of it.

Enhancement

Ok so now you know how to load the PIL module, load an image and show it. What about the magic?

There are different methods to enhance an image:

ImageEnhance.Brightness(image)ImageEnhance.Color(image)ImageEnhance.Contrast(image)ImageEnhance.Sharpness(image)
Enter fullscreen modeExit fullscreen mode

The app below does all the magic. The method enhance() takes a parameter that you can play around with.

#!/usr/bin/python3#-*- coding: UTF-8 -*-   from PIL import Imagefrom PIL import ImageEnhanceimage = Image.open('lena.jpg')image.show()enh_bri = ImageEnhance.Brightness(image)brightness = 1.5image_brightened = enh_bri.enhance(brightness)image_brightened.show()enh_col = ImageEnhance.Color(image)color = 1.5image_colored = enh_col.enhance(color)image_colored.show()enh_con = ImageEnhance.Contrast(image)contrast = 1.5image_contrasted = enh_con.enhance(contrast)image_contrasted.show()enh_sha = ImageEnhance.Sharpness(image)sharpness = 3.0image_sharped = enh_sha.enhance(sharpness)image_sharped.show()
Enter fullscreen modeExit fullscreen mode

Related links:

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
lizard profile image
Lizard
Mathematician
  • Location
    Amsterdam
  • Education
    Amsterdam
  • Joined
• Edited on• Edited

Maybe you could show the output from one of the enhancement methods for comparison. Nice post 👍🏽

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More frompetercour

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp