Created
April 10, 2019 10:59
-
-
Save toshihiroryuu/6eb7176d17f066d17b871d05c2c47bab to your computer and use it in GitHub Desktop.
Addtion, Substration and Multiplication of values to pixels in each channel seperately
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
import matplotlib.pyplot as plt | |
class pixel_transformation: | |
def __init__(self): | |
self.red_added=0 | |
self.green_added=0 | |
self.blue_added=0 | |
self.red_sub=0 | |
self.green_sub=0 | |
self.blue_sub=0 | |
self.red_factor=1 | |
self.green_factor=1 | |
self.blue_factor=1 | |
def pixel_add(self,img,x=0,y=0,z=0): | |
self.img=img | |
self.red_added=x | |
self.green_added=y | |
self.blue_added=z | |
red, green, blue = self.img.split() | |
for y in range(self.img.height): | |
for x in range(self.img.width): | |
value = self.img.getpixel((x, y)) | |
# print(value[0]) | |
new_color = (value[0] + int(red_added), value[1] + int(green_added), value[2] + int(blue_added)) | |
img.putpixel((x, y), new_color) | |
return img | |
def pixel_sub(self,img,x=0,y=0,z=0): | |
self.img=img | |
self.red_sub=x | |
self.green_sub=y | |
self.blue_sub=z | |
red, green, blue = self.img.split() | |
for y in range(self.img.height): | |
for x in range(self.img.width): | |
value = self.img.getpixel((x, y)) | |
# print(value[0]) | |
new_color = (value[0] - int(red_sub), value[1] - int(green_sub), value[2] - int(blue_sub)) | |
img.putpixel((x, y), new_color) | |
return img | |
def pixel_mul(self,img,x=1,y=1,z=1): | |
self.img=img | |
self.red_factor=x | |
self.green_factor=y | |
self.blue_factor=z | |
red, green, blue = self.img.split() | |
for y in range(self.img.height): | |
for x in range(self.img.width): | |
value = self.img.getpixel((x, y)) | |
# print(value[0]) | |
new_color = (value[0]*red_factor), (value[1]*green_factor), (value[2]*blue_factor) | |
img.putpixel((x, y), new_color) | |
return img | |
px=pixel_transformation() | |
file_path = "/home/quest/cat.jpg" | |
red_added = int(input("Red to be added: ")) | |
green_added = int(input("Green to be added: ")) | |
blue_added = int(input("Blue to be added: ")) | |
red_sub = int(input("Red to be substrated: ")) | |
green_sub = int(input("Green to be substracted: ")) | |
blue_sub = int(input("Blue to be substracted: ")) | |
red_factor = int(input("Red Factor to be multiplied: ")) | |
green_factor = int(input("Green Factor to be multiplied: ")) | |
blue_factor = int(input("Blue Factorto be multiplied: ")) | |
img = Image.open(file_path) | |
img=px.pixel_add(img,red_added,green_added,blue_added) | |
img.save('pixel_add.png') | |
plt.imshow(img) | |
plt.show() | |
img = Image.open(file_path) | |
img=px.pixel_sub(img,red_sub,green_sub,blue_sub) | |
img.save('pixel_sub.png') | |
plt.imshow(img) | |
plt.show() | |
img = Image.open(file_path) | |
img=px.pixel_mul(img,red_factor,green_factor,blue_factor) | |
img.save('pixel_mul.png') | |
plt.imshow(img) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment