When you adjust color of an image, you are adjusting the color balance which has the following components:
1. White balance: Adjusting color to make it look natural under different lighting conditions. Incandescent lamp, tungsten lamp, etc.
2. Color temperature: Adjusting the warmth or coolness of the image. Higher the temperature (in Kelvin) cooler the image (blue) and cooler the temperature warmer the image (more yellow). It may appear counterintuitive as to how we feel the effects of temperature. In reference to how a black body appears at different Kelvin values, higher Kelvin around 6500K and lower Kelvin, between 2000 to 3000. It represents the hue of a light source.
3. Tint:: adjusting green-magenta balance to correct for any color casts that were not addressed by the above. Note that color casts is an artifact of camera, environment etc. and is unwanted.
Well, in Python using PIL you need to use the ImageEnhance.color() method.
This method just takes a single argument (enhance factor), a floating-point number. The color enhancement for three enhancement factors are as follows:
1.0 : returns a copy of the image
<1.0: decreases the enhancement (e.g., less color)
>1.0: increases the enhancement)e.g., more color)
The PIL library can only do the above corrections or changes but for a more granular control you may need additional libraries or custom software.
Here is an example code and some screenshots. I am again using the "TheKiss" image used earlier. The original image is very large and so I have used a smaller image, about 10% of the original using size adjusting code in the Python code ().
=========================
AdjustColorPIL.py
from PIL import Image, ImageEnhance
from PIL.ImageFile import ImageFile
# Open an image file
image: ImageFile = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images\TheKiss.jpg')
new_width = image.width // 10
new_height = image.height // 10
image = image.resize((new_width, new_height))
# Create an enhancer object for color
color_enhancer = ImageEnhance.Color(image)
# Enhance the image color with a factor (e.g., 1.5 for 50% more color)
enhanced_image = color_enhancer.enhance(0.5)
# Save the enhanced image
enhanced_image.save("enhanced_0point5.jpg")
# display the image
enhanced_image.show()
===============================
The above code was run for three values of enhance factor 0.5. 1.00 and 1.5. The results are as shown here:
enhance factor= 0.5The enhancement or otherwise using the enhance factor has no upper or lower limits. But beyond certain values, the result may look weird or uninteresting.
Here are some practical guidelines:
0.0 ≤ factor < 1.0: This range will reduce color saturation. For example:
0.0: Converts the image to grayscale.
0.5: Significantly reduces color intensity.
factor > 1.0: This range will increase color saturation. For example:
1.5: Moderately increases color intensity.
2.0 and above: Can lead to highly saturated, unnatural-looking colors.
No comments:
Post a Comment