-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarve.py
115 lines (97 loc) · 2.95 KB
/
carve.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from PIL import Image
import numpy as np
def eng(i, j, img):
if i>=len(img[0])-2 or i == 0 \
or j>=len(img)-2 or j == 0:
return 1444
sobel_hor = img[j-1][i-1] + 2*img[j][i-1] + img[j+1][i-1] + \
-img[j-1][i+1] + -2*img[j][i+1] + -img[j+1][i+1]
sobel_ver = img[j-1][i-1] + 2*img[j-1][i] + img[j-1][i+1] + \
-img[j+1][i-1] + -2*img[j+1][i] + -img[j+1][i+1]
return np.sqrt(sobel_hor**2 + sobel_ver**2)
# img[h][w]
def carve_vertical(img):
h = len(img)
w = len(img[0])
e = np.zeros((h,w))
c = np.zeros((h,w))
p = np.zeros((h,w,2))
for j in range(h):
for i in range(w):
e[j][i] = eng(i,j,img)
e = e / 1444
for i in range(w):
c[0,i] = e[0,i]
p[0,i] = (-1,-1)
for j in range(1,h):
for i in range(0,w):
c[j,i] = c[j-1,i]
p[j,i] = (j-1,i)
if i > 0 and c[j-1,i-1] < c[j,i] and c[j-1,i-1] > 0:
c[j,i] = c[j-1,i-1]
p[j,i] = (j-1,i-1)
if i < w-1 and c[j-1,i+1] < c[j,i] and c[j-1,i+1] > 0:
c[j,i] = c[j-1,i+1]
p[j,i] = (j-1,i+1)
c[j,i] += e[j,i]
pos = (5,5)
for i in range(w):
if c[pos] < c[h-1,i] and c[h-1,i] > 0:
pos = (j,i)
seam = [(pos[0], pos[1])]
for j in range(h-2, -1, -1):
seam.append((int(p[pos][0]//1), int(p[pos][1]//1)))
pos = (int(p[pos][0]//1), int(p[pos][1]//1))
return seam
def delete_seam(seam, img):
h = len(img)
w = len(img[0])
_img = np.zeros((h,w-1))
i_adj = 0
for j in range(h):
i_adj = 0
for i in range(w-1):
if (j,i) in seam:
i_adj=1
_img[j,i] = img[j,i+i_adj]
return _img
def dupe_seam(seam, img):
h = len(img)
w = len(img[0])
_img = np.zeros((h,w+1))
for j in range(h):
i_adj = 0
for i in range(w):
if (j,i) in seam:
_img[j,i] = img[j,i]
i_adj=1
_img[j,i+i_adj] = img[j,i]
return _img
def seam_carve_ver(new_width, img):
_img = np.array(img)
if new_width > img.width:
n = new_width - img.width
for i in range(n):
print(i)
seam = carve_vertical(_img)
_img = dupe_seam(seam , _img)
else:
n = img.width - new_width
for i in range(n):
print(i)
seam = carve_vertical(_img)
_img = delete_seam(seam , _img)
return Image.fromarray(_img)
def seam_carve_hor(new_height, img):
img = img.rotate(90, expand=True)
print('hor')
img = seam_carve_ver(new_height, img)
img = img.rotate(270, expand=True)
return img
def seam_carve(new_size, img):
img = seam_carve_ver(new_size[0], img)
return seam_carve_hor(new_size[1], img)
img = Image.open('tower.jpg').convert("L")
img.show()
img = seam_carve((img.width-20, img.height), img)
img.show()