Skip to content

Commit 97268ab

Browse files
committed
Merge pull request BhargavGamit#1 from vennela1/master
initial merge
2 parents edc2ff0 + 966305f commit 97268ab

12 files changed

+431
-0
lines changed

BitonalAlgo.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Author Sri Vennela Vaishnapu
2+
3+
# bi toning algorithm
4+
#if (pixelBuffer[k] + pixelBuffer[k + 1] +
5+
# pixelBuffer[k + 2] <= threshold)
6+
# {
7+
# pixelBuffer[k] = darkColor.B;
8+
# pixelBuffer[k + 1] = darkColor.G;
9+
# pixelBuffer[k + 2] = darkColor.R;
10+
# }
11+
# else
12+
# {
13+
# pixelBuffer[k] = lightColor.B;
14+
# pixelBuffer[k + 1] = lightColor.G;
15+
# pixelBuffer[k + 2] = lightColor.R;
16+
# }
17+
from PIL import Image
18+
import random
19+
lightcolor_red=0
20+
lightcolor_blue=139
21+
lightcolor_green=0
22+
darkcolor_red=179
23+
darkcolor_blue=222
24+
darkcolor_green=196
25+
i = Image.open("input.png")
26+
#pixel data is stored in pixels in form of two dimensional array
27+
pixels = i.load()
28+
width, height = i.size
29+
j=Image.new(i.mode,i.size)
30+
threshold = 374
31+
for x in range(width):
32+
for y in range(height):
33+
cpixel = pixels[x, y]
34+
#cpixel[0] contains red value cpixel[1] contains green value
35+
#cpixel[2] contains blue value cpixel[3] contains alpha value
36+
if(int(cpixel[0] + cpixel[1] +cpixel[2]) > int(threshold)):
37+
red = lightcolor_red
38+
green = lightcolor_green
39+
blue = lightcolor_blue
40+
j.putpixel((x,y),(red,green,blue))
41+
else:
42+
red = darkcolor_red
43+
green = darkcolor_green
44+
blue = darkcolor_blue
45+
j.putpixel((x,y),(red,green,blue))
46+
j.save('output.png')

ColorTintAlgo.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Author Sri Vennela Vaishnapu
2+
3+
# ColourTint algorithm
4+
#blue = pixelBuffer[k] + (255 - pixelBuffer[k]) * blueTint;
5+
# green = pixelBuffer[k + 1] + (255 - pixelBuffer[k + 1]) * greenTint;
6+
# red = pixelBuffer[k + 2] + (255 - pixelBuffer[k + 2]) * redTint;
7+
# if (blue > 255)
8+
# { blue = 255; }
9+
# if (green > 255)
10+
# { green = 255; }
11+
# if (red > 255)
12+
# { red = 255; }
13+
14+
from PIL import Image
15+
i = Image.open("input.png")
16+
print(i.format,i.size,i.mode)
17+
pixels = i.load()
18+
width, height = i.size
19+
j=Image.new(i.mode,i.size)
20+
blueTint=input("enter the percentage of blue tint in fraction")
21+
greenTint=input("enter the percentage of green tint in fraction")
22+
redTint=input("enter the percentage of red tint in fraction")
23+
for x in range(width):
24+
for y in range(height):
25+
cpixel = pixels[x, y]
26+
#cpixel[0] contains red value cpixel[1] contains green value
27+
#cpixel[2] contains blue value cpixel[3] contains alpha value
28+
outputBlue = int(cpixel[0] + (255 - cpixel[0]) * blueTint);
29+
outputGreen = int(cpixel[1] + (255 - cpixel[1]) * greenTint);
30+
outputRed = int(cpixel[2] + (255 - cpixel[2]) * redTint);
31+
if(outputRed>255):
32+
outputRed=255
33+
if(outputGreen>255):
34+
outputGreen=255
35+
if(outputBlue>255):
36+
outputBlue=255
37+
j.putpixel((x,y),(outputRed,outputGreen,outputBlue))
38+
j.save('output.png')
39+

ColourBalanceAlgo.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Author Sri Vennela Vaishnapu
2+
3+
# bi toning algorithm
4+
# blue = 255.0f / blueLevelFloat * (float )pixelBuffer[k];
5+
# green = 255.0f / greenLevelFloat * (float)pixelBuffer[k + 1];
6+
# red = 255.0f / redLevelFloat * (float)pixelBuffer[k + 2];
7+
# if (blue > 255) {blue = 255;}
8+
# else if (blue < 0) {blue = 0;}
9+
# if (green > 255) {green = 255;}
10+
# else if (green < 0) {green = 0;}
11+
# if (red > 255) {red = 255;}
12+
# else if (red < 0) {red = 0;}
13+
from PIL import Image
14+
i = Image.open("lena.jpg")
15+
print(i.format,i.size,i.mode)
16+
pixels = i.load() # this is not a list, nor is it list()'able
17+
width, height = i.size
18+
j=Image.new(i.mode,i.size)
19+
blueLevelFloat=input("enter the color level for blue which is ranging from 0 to 255")
20+
greenLevelFloat=input("enter the color level for green which is ranging from 0 to 255")
21+
redLevelFloat=input("enter the color level for red which is ranging from 0 to 255")
22+
for x in range(width):
23+
for y in range(height):
24+
cpixel = pixels[x, y]
25+
#cpixel[0] contains red value cpixel[1] contains green value
26+
#cpixel[2] contains blue value cpixel[3] contains alpha value
27+
outputBlue = int(255.0 / blueLevelFloat * cpixel[0]);
28+
outputGreen = int(255.0 / greenLevelFloat * cpixel[1]);
29+
outputRed = int(255.0 / redLevelFloat * cpixel[2]);
30+
if(outputRed>255):
31+
outputRed=255
32+
elif(outputRed<0):
33+
outputRed=0
34+
if(outputGreen>255):
35+
outputGreen=255
36+
elif(outputGreen<0):
37+
outputGreen=0
38+
if(outputBlue>255):
39+
outputBlue=255
40+
elif(outputBlue<0):
41+
outputBlue=0
42+
j.putpixel((x,y),(outputRed,outputGreen,outputBlue))
43+
j.save('colourbal.png')
44+

ColourInversionAlgo.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Author Sri Vennela Vaishnapu
2+
3+
# Inversion algorithm
4+
# colour = GetPixelColour(x, y)
5+
# invertedRed = 255 - Red(colour)
6+
# invertedGreen = 255 - Green(colour)
7+
# invertedBlue = 255 - Blue(colour)
8+
# PutPixelColour(x, y) = RGB(invertedRed, invertedGreen,invertedBlue)
9+
10+
from PIL import Image
11+
i = Image.open("input.png")
12+
13+
#pixel data is stored in pixels in form of two dimensional array
14+
pixels = i.load()
15+
width, height = i.size
16+
j=Image.new(i.mode,i.size)
17+
18+
for x in range(width):
19+
for y in range(height):
20+
cpixel = pixels[x, y]
21+
#cpixel[0] contains red value cpixel[1] contains green value
22+
#cpixel[2] contains blue value cpixel[3] contains alpha value
23+
invertedRed = 255 - cpixel[0]
24+
invertedGreen = 255 - cpixel[1]
25+
invertedBlue = 255 - cpixel[2]
26+
j.putpixel((x, y),(invertedRed, invertedGreen, invertedBlue))
27+
j.save('output.png')

ColourShadingAlgo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Author Sri Vennela Vaishnapu
2+
3+
# Colour Shading algorithm
4+
from PIL import Image
5+
import random
6+
i = Image.open("lena.jpg")
7+
blueShade = input("enter the ahdingfactor for blue range:0 to 1")
8+
redShade = input("enter the ahdingfactor for red range:0 to 1")
9+
greenShade = input("enter the ahdingfactor for green range:0 to 1")
10+
#pixel data is stored in pixels in form of two dimensional array
11+
pixels = i.load()
12+
width, height = i.size
13+
j=Image.new(i.mode,i.size)
14+
15+
for x in range(width):
16+
for y in range(height):
17+
cpixel = pixels[x, y]
18+
#cpixel[0] contains red value cpixel[1] contains green value
19+
#cpixel[2] contains blue value cpixel[3] contains alpha value
20+
blue = int(cpixel[0] * blueShade)
21+
green = int(cpixel[1] * greenShade)
22+
red = int(cpixel[2] * redShade)
23+
if(blue < 0):
24+
blue = 0
25+
if(green < 0):
26+
green = 0
27+
if(red < 0):
28+
red = 0
29+
j.putpixel((x,y),(red,green,blue))
30+
j.save('shading.png')

ContrastAlgo.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#Author SriVennelaVaishnapu
2+
3+
#Contrast Algorithm
4+
#factor = (259 * (contrast + 255)) / (255 * (259 - contrast))
5+
#colour = GetPixelColour(x, y)
6+
#newRed = Truncate(factor * (Red(colour) - 128) + 128)
7+
#newGreen = Truncate(factor * (Green(colour) - 128) + 128)
8+
#newBlue = Truncate(factor * (Blue(colour) - 128) + 128)
9+
#PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)
10+
#Procedure Truncate(value)
11+
# If value < 0 Then value = 0
12+
# If value > 255 Then value = 255
13+
# Return value
14+
#EndProcedure
15+
from PIL import Image
16+
i = Image.open("input.png")
17+
#debugging purpose
18+
#print(i.format,i.size,i.mode)
19+
#pixel data is stored in pixels in form of two dimensional array
20+
pixels = i.load() # this is not a list, nor is it list()'able
21+
width, height = i.size
22+
j=Image.new(i.mode,i.size)
23+
contrast=input("enter contrast value range:-255 to 255")
24+
def Truncate(value):
25+
if(value < 0):
26+
value = 0
27+
if(value > 255):
28+
value = 255
29+
return value
30+
for x in range(width):
31+
for y in range(height):
32+
cpixel = pixels[x, y]
33+
factor = (259 * (contrast + 255)) / (255 * (259 - contrast))
34+
#cpixel[0] contains red value cpixel[1] contains green value
35+
#cpixel[2] contains blue value cpixel[3] contains alpha value
36+
newRed = Truncate((factor * (cpixel[0] - 128) + 128))
37+
newGreen = Truncate((factor * (cpixel[1] - 128) + 128))
38+
newBlue = Truncate((factor * (cpixel[2] - 128) + 128))
39+
j.putpixel((x,y),(newRed, newGreen, newBlue))
40+
j.save('output.png')

CustomAlgo-BW.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Author Sri Vennela Vaishnapu
2+
3+
# Custom algorithm
4+
# ConversionFactor = 255 / (NumberOfShades - 1)
5+
# AverageValue = (Red + Green + Blue) / 3
6+
# Gray = Integer((AverageValue / ConversionFactor) + 0.5) * ConversionFactor
7+
8+
from PIL import Image
9+
import random
10+
i = Image.open("input.png")
11+
12+
#pixel data is stored in pixels in form of two dimensional array
13+
pixels = i.load()
14+
width, height = i.size
15+
j=Image.new(i.mode,i.size)
16+
17+
#selects the random no of shades between 2 to 256 including 2 and 256
18+
NumberOfShades = int(random.random()*255)+2
19+
ConversionFactor = 255 / (NumberOfShades - 1)
20+
for x in range(width):
21+
for y in range(height):
22+
cpixel = pixels[x, y]
23+
#cpixel[0] contains red value cpixel[1] contains green value
24+
#cpixel[2] contains blue value cpixel[3] contains alpha value
25+
AverageValue = (cpixel[0] + cpixel[1] + cpixel[2]) / 3
26+
gray = int(((AverageValue / ConversionFactor) + 0.5) * ConversionFactor)
27+
j.putpixel((x,y),(gray,gray,gray))
28+
j.save('output.png')

DesaturationAlgo-BW.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#Author SriVennela Vaishnapu
2+
3+
#Desaturation Algorithm
4+
#Gray = ( Max(Red, Green, Blue) + Min(Red, Green, Blue) ) / 2
5+
6+
from PIL import Image
7+
import math
8+
import random
9+
i = Image.open("input.png")
10+
11+
#pixel data is stored in pixels in form of two dimensional array
12+
pixels = i.load()
13+
width, height = i.size
14+
j=Image.new(i.mode,i.size)
15+
16+
#loop for storing each pixel of original image in new variable cpixel
17+
for x in range(width):
18+
for y in range(height):
19+
cpixel = pixels[x, y]
20+
#cpixel[0] is for red value cpixel[1] is for green value cpixel[2] is for blue value
21+
Gray = int( max(cpixel[0],cpixel[1],cpixel[2]) +min(cpixel[0],cpixel[1],cpixel[2]) ) / 2
22+
j.putpixel((x,y),(Gray,Gray,Gray))
23+
j.save('output.png')

ErrorDiffusionAlgo.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#Authors Bhargav K,SriVennelaVaishnapu
2+
3+
#Error Diffusion Algorithm
4+
#error = actualColour - nearestColour
5+
#PutPixelColour(x+1, y ) = Truncate(GetPixelColour(x+1, y ) + 0.5 * error)
6+
#PutPixelColour(x , y+1) = Truncate(GetPixelColour(x , y+1) + 0.5 * error)
7+
#Procedure Truncate(value)
8+
# If value < 0 Then value = 0
9+
# If value > 255 Then value = 255
10+
# Return value
11+
#EndProcedure
12+
13+
from PIL import Image
14+
i = Image.open("lena.jpg")
15+
#debugging purpose
16+
#print(i.format,i.size,i.mode)
17+
#pixel data is stored in pixels in form of two dimensional array
18+
pixels = i.load()# this is not a list, nor is it list()'able
19+
width, height = i.size
20+
j=Image.new(i.mode,i.size)
21+
def Truncate(value):
22+
if(value < 0):
23+
value = 0
24+
if(value > 255):
25+
value = 255
26+
return value
27+
def Error(value):
28+
if(value >= 0 and value <=127):
29+
return 0
30+
else:
31+
return 128
32+
for x in range(width-1):
33+
for y in range(height-1):
34+
cpixel = pixels[x, y]
35+
#cpixel[0] contains red value cpixel[1] contains green value
36+
#cpixel[2] contains blue value cpixel[3] contains alpha value
37+
error_red = cpixel[0]-Error(cpixel[0])
38+
error_green = cpixel[1]-Error(cpixel[1])
39+
error_blue = cpixel[2]-Error(cpixel[2])
40+
apixel= pixels[x+1,y]
41+
bpixel= pixels[x,y+1]
42+
j.putpixel((x+1, y ),(int(Truncate(apixel[0] + 0.5 * error_red)),int(Truncate(apixel[1]+0.5*error_blue)),int(Truncate(apixel[2]+0.5*error_green))))
43+
j.putpixel((x , y+1),(int(Truncate(bpixel[0] + 0.5 * error_red)),int(Truncate(bpixel[1]+0.5*error_blue)),int(Truncate(bpixel[2]+0.5*error_green))))
44+
45+
j.save('error.png')

GammaCorrectionAlgo.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#Authors SriVennelaVaishnapu
2+
3+
#Gamma correction Algorithm
4+
#gammaCorrection = 1 / gamma
5+
#colour = GetPixelColour(x, y)
6+
#newRed = 255 * (Red(colour) / 255) ^ gammaCorrection
7+
#newGreen = 255 * (Green(colour) / 255) ^ gammaCorrection
8+
#newBlue = 255 * (Blue(colour) / 255) ^ gammaCorrection
9+
#PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)
10+
from PIL import Image
11+
import random
12+
i = Image.open("input.png")
13+
#debugging purpose
14+
#print(i.format,i.size,i.mode)
15+
#pixel data is stored in pixels in form of two dimensional array
16+
pixels = i.load() # this is not a list, nor is it list()'able
17+
width, height = i.size
18+
j=Image.new(i.mode,i.size)
19+
gamma=input("enter the value of gamma range:0.25 to 2.0")
20+
for x in range(width):
21+
for y in range(height):
22+
cpixel = pixels[x, y]
23+
#gamma value can range from 0.25 to 2.0
24+
gammaCorrection = 1 / gamma
25+
#cpixel[0] contains red value cpixel[1] contains green value
26+
#cpixel[2] contains blue value cpixel[3] contains alpha value
27+
newRed = int(255 * ((cpixel[0]/ 255.0)**gammaCorrection))
28+
newGreen = int(255 * ((cpixel[1]/ 255.0)**gammaCorrection))
29+
newBlue = int(255 * ((cpixel[2]/ 255.0)**gammaCorrection))
30+
j.putpixel((x,y),(newRed, newGreen, newBlue))
31+
j.save('output.png')

0 commit comments

Comments
 (0)