-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblur.py
58 lines (43 loc) · 1.71 KB
/
blur.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from grid import *
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
def calculateBlur(filename, sigma, tau, v, z0, s0 = 0.017, A = 0.0046):
img = Image.open(filename)
width, height = img.size
pc = [width / 2, height / 2]
slantAxisCoeff = [np.arctan(np.pi / 2 + tau), pc[1] - np.arctan(np.pi / 2 + tau) * pc[0]]
blurMat = np.ndarray(shape = (width, height), dtype = int)
for i in range(width):
for j in range(height):
p = [i, j]
dist = pointToLineDist(p, slantAxisCoeff[0], slantAxisCoeff[1])
eps = np.arctan(dist / v)
relDist = np.cos(sigma) * np.cos(eps) / np.cos(sigma + eps)
blurDiamRad = 2 * np.arctan(np.abs(A * s0 / z0 * (1 - 1/relDist)) / 2 / s0)
blurDiamPix = int(round(2 * v * np.tan(blurDiamRad / 2)))
blurMat[i][j] = blurDiamPix
return blurMat
def applyBlur(filename, blurMat):
img = Image.open(filename)
px = img.load()
finalImage = Image.new(img.mode, img.size, color = (0, 0, 0))
maxBlur = np.amax(blurMat)
currBlurDiam = np.amin(blurMat)
print('start: ' + str(currBlurDiam))
print('end: ' + str(maxBlur))
while currBlurDiam <= maxBlur:
print('curr: ' + str(currBlurDiam))
tempImage = Image.new(img.mode, img.size, color = (0, 0, 0))
tempImagePx = tempImage.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if blurMat[i][j] == currBlurDiam:
blurMat[i][j] = maxBlur + 1
tempImagePx[i, j] = px[i, j]
tempImage = tempImage.filter(ImageFilter.GaussianBlur(radius = currBlurDiam / 2))
finalImage = Image.fromarray(np.asarray(finalImage) + np.asarray(tempImage))
currBlurDiam = np.amin(blurMat)
finalImage.filter(ImageFilter.EDGE_ENHANCE_MORE)
finalImage.save('result.jpg')
plt.imshow(finalImage)