Skip to content

Commit 01213b4

Browse files
committed
Update
Added new scripts
1 parent c646cca commit 01213b4

8 files changed

+346
-1
lines changed

License.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
2+
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
55
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,

Mean Filter.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Author Kavya S, Bhargav K
2+
3+
# Mean filter
4+
5+
from PIL import Image
6+
i = Image.open("input.jpg")
7+
8+
#pixel data is stored in pixels in form of two dimensional array
9+
pixels = i.load()
10+
width, height = i.size
11+
k=Image.new(i.mode,i.size)
12+
13+
filtersize=input('Enter the filtersize: ')
14+
filterOffset=(filtersize-1)/2
15+
filterheight=filtersize
16+
filterwidth=filtersize
17+
offsety=filterOffset
18+
offsetx=filterOffset
19+
20+
j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety)))
21+
22+
for x in range(width+(2*offsetx)):
23+
for y in range(height+(2*offsety)):
24+
if(x<offsetx):
25+
j.putpixel((x,y),(0,0,0))
26+
if(y<offsety):
27+
j.putpixel((x,y),(0,0,0))
28+
if(x>width):
29+
j.putpixel((x,y),(0,0,0))
30+
if(y>height):
31+
j.putpixel((x,y),(0,0,0))
32+
if(x>=offsetx and y>=offsety and x <=width and y <=height):
33+
cpixel=pixels[x-offsetx,y-offsety]
34+
j.putpixel((x,y),cpixel)
35+
36+
37+
for y in range(height+(2*offsety)):
38+
for x in range(width+(2*offsetx)):
39+
if(x>=offsetx and y>=offsety and x <=width and y <=height):
40+
blue=0.0
41+
green=0.0
42+
red=0.0
43+
blues=list()
44+
reds=list()
45+
greens=list()
46+
filterx=x-offsetx #y?
47+
filtery=y-offsety
48+
for fy in range(filterheight):
49+
for fx in range(filterwidth):
50+
cpixel=j.getpixel((x-offsetx+fx,y-offsety+fy))
51+
reds.append(float(cpixel[0]))
52+
greens.append(float(cpixel[1]))
53+
blues.append(float(cpixel[2]))
54+
red=sum(reds)/(filtersize*filtersize)
55+
blue=sum(blues)/(filtersize*filtersize)
56+
green=sum(greens)/(filtersize*filtersize)
57+
k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue)))
58+
del reds[:]
59+
del blues[:]
60+
del greens[:]
61+
k.save('output.png')

Median Filter.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Author Bhargav K
2+
3+
# Median filter
4+
5+
from PIL import Image
6+
i = Image.open("input.jpg")
7+
8+
#pixel data is stored in pixels in form of two dimensional array
9+
pixels = i.load()
10+
width, height = i.size
11+
k=Image.new(i.mode,i.size)
12+
13+
filtersize=input('Enter the size of the filter: ')
14+
filterOffset=(filtersize-1)/2
15+
filterheight=filtersize
16+
filterwidth=filtersize
17+
offsety=filterOffset
18+
offsetx=filterOffset
19+
20+
j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety)))
21+
22+
for x in range(width+(2*offsetx)):
23+
for y in range(height+(2*offsety)):
24+
if(x<offsetx):
25+
j.putpixel((x,y),(0,0,0))
26+
if(y<offsety):
27+
j.putpixel((x,y),(0,0,0))
28+
if(x>=width):
29+
j.putpixel((x,y),(0,0,0))
30+
if(y>=height):
31+
j.putpixel((x,y),(0,0,0))
32+
if(x>=offsetx and y>=offsety and x < width and y <height):
33+
cpixel=pixels[x-offsetx,y-offsety]
34+
j.putpixel((x,y),cpixel)
35+
36+
for y in range(height+(2*offsety)):
37+
for x in range(width+(2*offsetx)):
38+
if(x>=offsetx and y>=offsety and x < width and y <height):
39+
blue=0.0
40+
green=0.0
41+
red=0.0
42+
blues=list()
43+
reds=list()
44+
greens=list()
45+
filterx=x-offsetx
46+
filtery=y-offsety
47+
for fy in range(filterheight):
48+
for fx in range(filterwidth):
49+
cpixel=j.getpixel((x-offsety+fx,y-offsety+fy))
50+
reds.append(float(cpixel[0]))
51+
greens.append(float(cpixel[1]))
52+
blues.append(float(cpixel[2]))
53+
reds.sort()
54+
blues.sort()
55+
greens.sort()
56+
index=len(reds)
57+
medianindex=index/2
58+
if(index%2!=0):
59+
red=reds[medianindex]
60+
blue=blues[medianindex]
61+
green=greens[medianindex]
62+
else:
63+
red=(reds[medianindex]+reds[medianindex+1])/2
64+
blue=(blues[medianindex]+blues[medianindex+1])/2
65+
green=(greens[medianindex]+greens[medianindex+1])/2
66+
k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue)))
67+
del reds[:]
68+
del blues[:]
69+
del greens[:]
70+
k.save('output.png')

Min-Max Filter.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Author Bhargav K
2+
3+
# Min-Max Filter
4+
5+
from PIL import Image
6+
i = Image.open("input.jpg")
7+
8+
#pixel data is stored in pixels in form of two dimensional array
9+
pixels = i.load()
10+
width, height = i.size
11+
k=Image.new(i.mode,i.size)
12+
sol=Image.new(i.mode,i.size)
13+
14+
filtersize=input('Enter the size of the median filter: ')
15+
filterOffset=(filtersize-1)/2
16+
filterheight=filtersize
17+
filterwidth=filtersize
18+
offsety=filterOffset
19+
offsetx=filterOffset
20+
21+
j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety)))
22+
23+
for x in range(width+(2*offsetx)):
24+
for y in range(height+(2*offsety)):
25+
if(x<offsetx):
26+
j.putpixel((x,y),(0,0,0))
27+
if(y<offsety):
28+
j.putpixel((x,y),(0,0,0))
29+
if(x>=width):
30+
j.putpixel((x,y),(0,0,0))
31+
if(y>=height):
32+
j.putpixel((x,y),(0,0,0))
33+
if(x>=offsetx and y>=offsety and x < width and y <height):
34+
cpixel=pixels[x-offsetx,y-offsety]
35+
j.putpixel((x,y),cpixel)
36+
37+
for y in range(height+(2*offsety)):
38+
for x in range(width+(2*offsetx)):
39+
if(x>=offsetx and y>=offsety and x < width and y <height):
40+
blue=0.0
41+
green=0.0
42+
red=0.0
43+
blues=list()
44+
reds=list()
45+
greens=list()
46+
filterx=x-offsetx
47+
filtery=y-offsety
48+
for fy in range(filterheight):
49+
for fx in range(filterwidth):
50+
cpixel=j.getpixel((x-offsety+fx,y-offsety+fy))
51+
reds.append(float(cpixel[0]))
52+
greens.append(float(cpixel[1]))
53+
blues.append(float(cpixel[2]))
54+
reds.sort()
55+
blues.sort()
56+
greens.sort()
57+
index=len(reds)
58+
medianindex=index/2
59+
if(index%2!=0):
60+
red=reds[medianindex]
61+
blue=blues[medianindex]
62+
green=greens[medianindex]
63+
else:
64+
red=(reds[medianindex]+reds[medianindex+1])/2
65+
blue=(blues[medianindex]+blues[medianindex+1])/2
66+
green=(greens[medianindex]+greens[medianindex+1])/2
67+
k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue)))
68+
del reds[:]
69+
del blues[:]
70+
del greens[:]
71+
72+
minmaxfiltersize=input('Enter the size of min-max filter: ')
73+
minmaxfilteroffset=(minmaxfiltersize-1)/2
74+
filterheight=minmaxfiltersize
75+
filterwidth=minmaxfiltersize
76+
minmaxoffsety=minmaxfilteroffset
77+
minmaxoffsetx=minmaxfilteroffset
78+
79+
z = Image.new(i.mode,(width+(2*minmaxoffsetx),height+(2*minmaxoffsety)))
80+
81+
for x in range(width+(2*minmaxoffsetx)):
82+
for y in range(height+(2*minmaxoffsety)):
83+
if(x<minmaxoffsetx):
84+
z.putpixel((x,y),(0,0,0))
85+
if(y<minmaxoffsety):
86+
z.putpixel((x,y),(0,0,0))
87+
if(x>=width):
88+
z.putpixel((x,y),(0,0,0))
89+
if(y>=height):
90+
z.putpixel((x,y),(0,0,0))
91+
if(x>=minmaxoffsetx and y>=minmaxoffsety and x < width and y <height):
92+
cpixel=k.getpixel((x-minmaxoffsetx,y-minmaxoffsety))
93+
z.putpixel((x,y),cpixel)
94+
95+
for y in range(height+(2*minmaxoffsety)):
96+
for x in range(width+(2*minmaxoffsetx)):
97+
if(x>=minmaxoffsetx and y>=minmaxoffsety and x < width and y <height):
98+
minblue=255
99+
mingreen=255
100+
minred=255
101+
maxblue=0
102+
maxgreen=0
103+
maxred=0
104+
filterx=x-minmaxoffsetx
105+
filtery=y-minmaxoffsety
106+
for fy in range(filterheight):
107+
for fx in range(filterwidth):
108+
cpixel=z.getpixel((x-minmaxoffsety+fx,y-minmaxoffsety+fy))
109+
minblue=min(minblue,cpixel[2])
110+
mingreen=min(mingreen,cpixel[1])
111+
minred=min(minred,cpixel[0])
112+
maxblue=max(maxblue,cpixel[2])
113+
maxgreen=max(maxgreen,cpixel[1])
114+
maxred=max(maxred,cpixel[0])
115+
red=maxred-minred
116+
green=maxgreen-mingreen
117+
blue=maxblue-minblue
118+
sol.putpixel((x-minmaxoffsetx,y-minmaxoffsety),(int(red),int(green),int(blue)))
119+
sol.save('minmax.png')

Mirror Images.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Author Kavya S, Bhargav K
2+
3+
# Mirroring Image
4+
5+
from PIL import Image
6+
i = Image.open("input.jpg")
7+
8+
#pixel data is stored in pixels in form of two dimensional array
9+
pixels = i.load()
10+
width, height = i.size
11+
j=Image.new(i.mode,i.size)
12+
13+
print "1 Mirror along X and Y axis"
14+
print "2 Mirror along Y axis"
15+
print "3 Mirror along X axis"
16+
17+
option=input('Enter your choice: ')
18+
if option == 1:
19+
widthmodifier=width-1
20+
heightmodifier=height-1
21+
elif option == 2:
22+
widthmodifier=0
23+
heightmodifier=height-1
24+
else:
25+
widthmodifier=width-1
26+
heightmodifier=0
27+
28+
for image_width_iterator in range(width):
29+
for image_height_iterator in range(height):
30+
cpixel = pixels[image_width_iterator,image_height_iterator]
31+
j.putpixel((abs(widthmodifier-image_width_iterator),abs(heightmodifier-image_height_iterator)),(cpixel))
32+
j.save('output.png')

Rotate by 90 degrees.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Author Kavya S
2+
3+
# Rotate image by 90 degrees
4+
5+
from PIL import Image
6+
i = Image.open("input.jpg")
7+
8+
#pixel data is stored in pixels in form of two dimensional array
9+
pixels = i.load()
10+
width, height = i.size
11+
j=Image.new(i.mode,(height,width))
12+
13+
14+
#cpixel[0] contains red value cpixel[1] contains green value
15+
#cpixel[2] contains blue value cpixel[3] contains alpha value
16+
for image_width_iterator in range(width):
17+
for image_height_iterator in range(height):
18+
cpixel = pixels[image_width_iterator,image_height_iterator]
19+
j.putpixel((abs(height-1-image_height_iterator),abs(image_width_iterator)),(cpixel))
20+
j.save('output.png')

Single Colour Conversion-Inversion.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Author Kavya S
2+
3+
# Single Color Inversion
4+
5+
from PIL import Image
6+
i = Image.open("input.jpg")
7+
8+
#pixel data is stored in pixels in form of two dimensional array
9+
pixels = i.load()
10+
width, height = i.size
11+
j=Image.new(i.mode,i.size)
12+
13+
14+
print '1 Red filter'
15+
print '2 Blue filter'
16+
print '3 Green filter'
17+
print '4 Red Invert filter'
18+
print '5 Blue Invert filter'
19+
print '6 Green Invert filter'
20+
red = 0
21+
blue = 0
22+
green = 0
23+
24+
#cpixel[0] contains red value cpixel[1] contains green value
25+
#cpixel[2] contains blue value cpixel[3] contains alpha value
26+
option=input('Enter your choice: ')
27+
for image_width_iterator in range(width):
28+
for image_height_iterator in range(height):
29+
cpixel = pixels[image_width_iterator, image_height_iterator]
30+
if option == 1:
31+
red = cpixel[0]
32+
elif option == 2:
33+
blue = cpixel[2]
34+
elif option == 3:
35+
green = cpixel[1];
36+
elif option == 4:
37+
red = 255 - cpixel[0]
38+
elif option == 5:
39+
blue = 255 - cpixel[2]
40+
else :
41+
green = 255 - cpixel[1];
42+
j.putpixel((image_width_iterator,image_height_iterator),(red,green,blue))
43+
j.save('output.png')

input.jpg

58.2 KB
Loading

0 commit comments

Comments
 (0)