How to adjust image contrast in Python

It is a learning memo.

What i did

I wrote a Python script to adjust the contrast of an image.

code

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('image.jpg')

#contrast
contrast = 128

#Contrast adjustment factor
factor = (259 *(contrast + 255)) / (255 *(259 - contrast))

#Convert to float type
newImage = np.array(img, dtype = 'float64')

#Contrast adjustment. (0 or less or 255 or more) is clipping
newImage = np.clip((newImage[:,:,:] - 128) * factor + 128, 0, 255)

#Return to int type
newImage = np.array(newImage, dtype = 'uint8')

#output
cv2.imwrite('out.png', newImage)

result

I will try it with Lena's image.

lena.png Original image

out.png After adjusting with contrast +128

Reference URL

IMAGE PROCESSING ALGORITHMS PART 5: CONTRAST ADJUSTMENT Algorithms for Adjusting Brightness and Contrast of an Image

Recommended Posts

How to adjust image contrast in Python
How to use Python Image Library in python3 series
How to develop in Python
How to create an image uploader in Bottle (Python)
[Python] How to do PCA in Python
How to collect images in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to install Python
How to install python
Image format in Python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to create a JSON file in Python
How to crop an image with Python + OpenCV
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
Tool to make mask image for ETC in Python
How to retrieve the nth largest value in Python
[2020.8 latest] How to install Python
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to get the variable name itself in python
How to install Python [Windows]
To flush stdout in Python
How to get the number of digits in Python
python3: How to use bottle (2)
How to write string concatenation in multiple lines in Python