-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
36 lines (35 loc) · 1.36 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from PIL import Image, ImageFilter, ImageChops, ImageOps, ImageDraw, ImageFont
import numpy as np
import math
import os
from medianDeNoise import medianDeNoise
from kernelConvolve import kernelConvolve
impath="Lenna.png"
im=Image.open(impath)
w, h = im.size
#imarray = np.asarray(im.copy()).astype(np.uint8).reshape(h,w,3)
#below is for black and white images
imarray = np.asarray(im.convert("L")).copy().astype(np.uint8).reshape(h,w)
def saturationIncrease(imarray, amt, w, h):
#python implementation of http://alienryderflex.com/saturation.html
r =.299
g =.587
b =.114
for i in range(1,h-1):
for n in range(1,w-1):
rr,gg,bb=imarray[i,n]
p=math.sqrt((rr**2*r)+(gg**2*g)+(gg**2*g))
imarray[i,n]=np.clip([p+(rr-p)*amt,p+(gg-p)*amt,p+(bb-p)*amt],0,255)
return imarray
#image = Image.fromarray(medianDeNoise(imarray,w,h), 'RGB')
#image = Image.fromarray(saturationIncrease(imarray,3,w,h), 'RGB')
#sharpen
kernel=np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
#outline kernel=np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
#emboss kernel=np.array([[-2,-1,0],[-1,1,1],[0,1,2]])
#blur kernel=np.array([[.0625,.125,.0625],[.125,.25,.125],[.0625,.125,.0625]])
image = Image.fromarray(kernelConvolve(imarray, kernel), 'L')
#image.show()
#image.format="PNG"
#file, ext = os.path.splitext("Lenna.jpg")
#image.save(file+"filtered.png","PNG")