Example of contrast adjustment for PS image adjustment using Python, pythonps

Source: Internet
Author: User

Example of contrast adjustment for PS image adjustment using Python, pythonps

This article describes how to adjust the contrast of PS images in Python. We will share this with you for your reference. The details are as follows:

Use Python to adjust the image in PS-contrast. The specific algorithm principle is as follows:

(1), nRGB = RGB + (RGB-Threshold) * Contrast/255

In the formula, nRGB indicates the new R, G, and B components of the image pixel, RGB indicates the R, G, and B components of the image pixel, and Threshold is the given Threshold value, contrast is the Contrast increment processed.

Photoshop processes contrast increments based on the positive and negative values of the given values:

When the increment is-255, It is the lower limit of the image contrast. At this time, all the RGB components of the image are equal to the threshold. The image is completely gray, and there is only one line on the grayscale image, that is, the threshold gray level;

When the increment is greater than-255 and less than 0, the following formula is used to calculate the pixel components of the image;

When the increment is equal to 255, It is the upper limit of the image contrast. It is actually equal to the set image threshold. The image consists of up to eight colors and consists of up to eight lines on the grayscale image, red, yellow, green, blue, purple, black, and white;

When the increment is greater than 0 and less than 255, the increment is processed according to the following formula (2), and then the contrast is calculated based on the formula (1) above:

(2) nContrast = 255*255/(255-Contrast)-255
In the formula, nContrast is the Contrast increment after processing, and Contrast is the given Contrast increment.

#-*-Coding: UTF-8 -*-#! Python3import matplotlib. pyplot as pltfrom skimage import iofile_name = 'd:/Visual Effects/PS Algorithm/4.jpg '; img = io. imread (file_name) img = img * 1.0 thre = img. mean () #-100-100 contrast =-55.0img _ out = img * 1.0if contrast <=-255.0: img_out = (img_out> = 0) + thre-1 elif contrast>-255.0 and contrast <0: img_out = img + (img-thre) * contrast/255.0 elif contrast <255.0 and contrast> 0: new_con = 255.0*255.0/(256.0-contrast)-255.0 img_out = img + (img-thre) * new_con/255.0 else: mask_1 = img> thre img_out = mask_1 * 255.0img _ out = img_out/255.0 # mask_1 = img_out for saturation processing <0mask_2 = img_out> 1img_out = img_out * (1-mask_1) img_out = img_out * (1-mask_2) + mask_2plt.figure () plt. title ('www .jb51.net') plt. imshow (img/255.0) plt. axis ('off') plt. figure (2) plt. title ('www .jb51.net') plt. imshow (img_out) plt. axis ('off') plt. show ()

Run

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.