Try to reproduce color film with Python

What you want to make

What I am thinking of creating this time is a film that changes the color tone of the entire image, such as that attached to a camera. If you use the Numpy module, you can easily make 3 primary color films, but I wanted to increase the versatility and support all colors.

The original image

sample.jpg

Finished product

from PIL import Image
import numpy as np

def color_filter(img_source, rgb):
    #Convert to a real array
    img_source=np.array(img_source, dtype='float16')
        
    #Film RGB is 0~255
    if max(rgb)>255 or min(rgb)<0:
        return Image.fromarray(np.unit8(img_source))
    
    #Apply RGB film
    img_source[:,:,0]*=rgb[0]/255
    img_source[:,:,1]*=rgb[1]/255
    img_source[:,:,2]*=rgb[2]/255
            
    #Output after conversion to Image class
    img_out=Image.fromarray(np.uint8(img_source))
    return img_out

How it works

The RGB value of each pixel is multiplied by the ratio of the film's RGB to 255 (maximum RGB value). The color range can be changed from the original image to pitch black. The point is that the film when it is pitch black is a wall.

Execution result

Film color: RGB (100,255,100) sample_out2.jpg Film color: RGB (173,216, 230) sample_out2-2.jpg

Failure work

At first I thought that it would be possible to take the average with the filter value instead of the ratio, so I will also post the result. Since the wall could not be reproduced, it will be a failure.

from PIL import Image
import numpy as np

def color_filter2(img_source, rgb):
    img_source=np.array(img_source, dtype="float16")
        
    #Film RGB is 0~255
    if max(rgb)>255 or min(rgb)<0:
        return Image.fromarray(np.unit8(img_source))
    
    #Apply RGB film
    img_source[:,:,0]+=rgb[0]
    img_source[:,:,0]/=2.0
    img_source[:,:,1]+=rgb[1]
    img_source[:,:,1]/=2.0
    img_source[:,:,2]+=rgb[2]
    img_source[:,:,2]/=2.0
    
            
    #Output after conversion to Image class
    return Image.fromarray(np.uint8(img_source))

Execution result

Film color: RGB (100,255,100) sample_out2-1.jpg Film color: RGB (173,216, 230) sample_out2-2.jpg Wall: RGB (0,0,0) sample_out2-3.jpg After all it is whitish overall.

Summary

I think there is demand for both, so please try various things. I hope you can use it to inflate image recognition data. (Addition) For some reason, I pasted the code when I forgot the slice notation ... The one I am currently pasting is lighter.

Recommended Posts

Try to reproduce color film with Python
Try to operate Facebook with Python
Try logging in to qiita with Python
Output color characters to pretty with python
Try scraping with Python.
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to automatically generate Python documents with Sphinx
Try to make a dihedral group with Python
Try to detect fish with python + OpenCV2.4 (unfinished)
Try to factorial with recursion
Connect to BigQuery with Python
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
Reproduce LightGBM Objective with python
Try to make a command standby tool with python
Try to understand Python self
Try to solve the internship assignment problem with Python
Try Python output with Haxe 3.2
Connect to Wikipedia with Python
Post to slack with Python 3
Try to operate DB with Python and visualize with d3
Try running Python with Try Jupyter
Switch python to 2.7 with alternatives
Write to csv with Python
Try to automate pdf format report creation with Python
Try face recognition with Python
Try to display various information useful for debugging with python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Try to bring up a subwindow with PyQt5 and Python
Try to automate the operation of network devices with Python
It's Halloween so I'll try to hide it with Python
Try to decipher the garbled attachment file name with Python
Try scraping with Python + Beautiful Soup
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try singular value decomposition with Python
Output to csv file with Python
Try to profile with ONNX Runtime
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
Try to calculate Trace in Python
How to get started with Python
Try converting cloudmonkey CLI to python3 -1
What to do with PYTHON release?
Try face recognition with python + OpenCV
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
Try frequency control simulation with Python
Easily post to twitter with Python 3
Try to output audio with M5STACK
I want to debug with Python
People who are accustomed to Android programs try multithreading with Python
Try python
Try to create a python environment with Visual Studio Code & WSL