Skip to content

Commit a1211d6

Browse files
committed
Added And Updated
1 parent 8904df6 commit a1211d6

File tree

3 files changed

+220
-2
lines changed

3 files changed

+220
-2
lines changed

AverageColoursFilter.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Author Bhargav K
2+
3+
# AverageColoursFilter
4+
5+
6+
from PIL import Image
7+
i = Image.open("input.png")
8+
9+
#store pixels of input image
10+
pixels = i.load()
11+
width, height = i.size
12+
k=Image.new(i.mode,i.size)
13+
14+
print "Filter size should be an odd positive number"
15+
filtersize=input("Choose the Size of Filter: ")
16+
print "Type (True) or (False) without braces"
17+
applyred=input ("Choose to apply red : ")
18+
applygreen=input("Choose to apply green : ")
19+
applyblue=input ("Choose to apply blue : ")
20+
print "Choose Shift Type"
21+
print "1 None "
22+
print "2 Shift Left"
23+
print "3 Shift Right"
24+
shifttype=input()
25+
filterwidth=filtersize
26+
filterheight=filtersize
27+
28+
filterOffset = (filterwidth-1)/2;
29+
30+
offsety=filterOffset
31+
offsetx=filterOffset
32+
33+
34+
j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety)))
35+
36+
for x in range(width+(2*offsetx)):
37+
for y in range(height+(2*offsety)):
38+
if(x<offsetx):
39+
j.putpixel((x,y),(0,0,0))
40+
if(y<offsety):
41+
j.putpixel((x,y),(0,0,0))
42+
if(x>=width):
43+
j.putpixel((x,y),(0,0,0))
44+
if(y>=height):
45+
j.putpixel((x,y),(0,0,0))
46+
if(x>=offsetx and y>=offsety and x < width and y <height):
47+
cpixel=pixels[x-offsetx,y-offsety]
48+
j.putpixel((x,y),cpixel)
49+
50+
51+
for y in range(height+(2*offsety)):
52+
for x in range(width+(2*offsetx)):
53+
if(x>=offsetx and y>=offsety and x < width and y <height):
54+
blue=0.0
55+
green=0.0
56+
red=0.0
57+
filterx=x-offsetx
58+
filtery=y-offsety
59+
for fy in range(filterheight):
60+
for fx in range(filterwidth):
61+
cpixel=j.getpixel((x-offsety+fx,y-offsety+fy))
62+
red+=float(cpixel[0])
63+
green+=float(cpixel[1])
64+
blue+=float(cpixel[2])
65+
red=red/filtersize
66+
green=green/filtersize
67+
blue=blue/filtersize
68+
cpixel=j.getpixel((x,y))
69+
if(applyred==False):
70+
red=cpixel[0]
71+
if(applygreen==False):
72+
green=cpixel[1]
73+
if(applyblue==False):
74+
blue=cpixel[2]
75+
if(red<0):
76+
red=0
77+
if(red>255):
78+
red=255
79+
if(green<0):
80+
green=0
81+
if(green>255):
82+
green=255
83+
if(blue<0):
84+
blue=0
85+
if(blue>255):
86+
blue=255
87+
if(shifttype==1):
88+
k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue)))
89+
if(shifttype==2):
90+
k.putpixel((x-offsetx,y-offsety),(int(blue),int(red),int(green)))
91+
if(shifttype==3):
92+
k.putpixel((x-offsetx,y-offsety),(int(green),int(blue),int(red)))
93+
k.save('output.png')

DilateAndErodeFilter.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Author Bhargav K
2+
3+
# Morphological Filters
4+
# DilateAndErodeFilter
5+
6+
7+
from PIL import Image
8+
i = Image.open("input.png")
9+
10+
#store pixels of input image
11+
pixels = i.load()
12+
width, height = i.size
13+
k=Image.new(i.mode,i.size)
14+
15+
print "Filter size should be an odd positive number"
16+
filtersize=input("Choose the Size of Filter: ")
17+
print "Type (True) or (False) without braces"
18+
applyred=input("Choose to apply red : ")
19+
applygreen=input("Choose to apply green : ")
20+
applyblue=input("Choose to apply blue : ")
21+
print "Choose Morphology Type"
22+
print "1 Dilation"
23+
print "2 Erosion"
24+
25+
morphtype=input()
26+
27+
morphresetvalue=0.0
28+
if(morphtype==2):
29+
morphresetvalue=255.0
30+
31+
print "Choose Morphology Edge Type"
32+
print "1 EdgeDetection"
33+
print "2 SharpenEdgeDetection"
34+
35+
edgetype=input()
36+
37+
filterwidth=filtersize
38+
filterheight=filtersize
39+
40+
filterOffset = (filterwidth-1)/2;
41+
42+
offsety=filterOffset
43+
offsetx=filterOffset
44+
45+
46+
j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety)))
47+
48+
for x in range(width+(2*offsetx)):
49+
for y in range(height+(2*offsety)):
50+
if(x<offsetx):
51+
j.putpixel((x,y),(0,0,0))
52+
if(y<offsety):
53+
j.putpixel((x,y),(0,0,0))
54+
if(x>=width):
55+
j.putpixel((x,y),(0,0,0))
56+
if(y>=height):
57+
j.putpixel((x,y),(0,0,0))
58+
if(x>=offsetx and y>=offsety and x < width and y <height):
59+
cpixel=pixels[x-offsetx,y-offsety]
60+
j.putpixel((x,y),cpixel)
61+
62+
63+
for y in range(height+(2*offsety)):
64+
for x in range(width+(2*offsetx)):
65+
if(x>=offsetx and y>=offsety and x < width and y <height):
66+
blue=morphresetvalue
67+
green=morphresetvalue
68+
red=morphresetvalue
69+
filterx=x-offsetx
70+
filtery=y-offsety
71+
if(morphtype==1):
72+
for fy in range(filterheight):
73+
for fx in range(filterwidth):
74+
cpixel=j.getpixel((x-offsety+fx,y-offsety+fy))
75+
if(float(cpixel[0]) > red):
76+
red=float(cpixel[0])
77+
if(float(cpixel[1]) > green):
78+
green=float(cpixel[1])
79+
if(float(cpixel[2]) > blue):
80+
blue=float(cpixel[2])
81+
if(morphtype==2):
82+
for fy in range(filterheight):
83+
for fx in range(filterwidth):
84+
cpixel=j.getpixel((x-offsety+fx,y-offsety+fy))
85+
if(float(cpixel[0]) < red):
86+
red=float(cpixel[0])
87+
if(float(cpixel[1]) < green):
88+
green=float(cpixel[1])
89+
if(float(cpixel[2]) < blue):
90+
blue=float(cpixel[2])
91+
cpixel=j.getpixel((x,y))
92+
if(applyred==False):
93+
red=cpixel[0]
94+
if(applygreen==False):
95+
green=cpixel[1]
96+
if(applyblue==False):
97+
blue=cpixel[2]
98+
if(edgetype==1 or edgetype==2):
99+
if(morphtype==1):
100+
red=red-cpixel[0]
101+
green=green-cpixel[1]
102+
blue=blue-cpixel[2]
103+
if(morphtype==2):
104+
red=cpixel[0]-red
105+
green=cpixel[1]-green
106+
blue=cpixel[2]-blue
107+
if(edgetype==2):
108+
red=red+cpixel[0]
109+
green=green+cpixel[1]
110+
blue=blue+cpixel[2]
111+
if(red<0):
112+
red=0
113+
if(red>255):
114+
red=255
115+
if(green<0):
116+
green=0
117+
if(green>255):
118+
green=255
119+
if(blue<0):
120+
blue=0
121+
if(blue>255):
122+
blue=255
123+
k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue)))
124+
125+
k.save('output.png')

GammaCorrectionAlgo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import random
1212
i = Image.open("input.png")
1313
#pixel data is stored in pixels in form of two dimensional array
14-
pixels = i.load() # this is not a list, nor is it list()'able
14+
pixels = i.load()
1515
width, height = i.size
1616
j=Image.new(i.mode,i.size)
17-
gamma=input("enter the value of gamma range:0.25 to 2.0")
17+
gamma=input("Enter the value of gamma range:0.25 to 2.0: ")
1818
for x in range(width):
1919
for y in range(height):
2020
cpixel = pixels[x, y]

0 commit comments

Comments
 (0)