-
Notifications
You must be signed in to change notification settings - Fork 0
/
chiseler.py
138 lines (110 loc) · 4.47 KB
/
chiseler.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from basic import *
import numpy as np
import math
import sys
def chiseler(image_name):
# changing the image pixle for git hub format
'''
this will convert the image in the format such that required
and return block dimention amd image
'''
#first reading height if it is divisible by week if not
#if remender of the the height by week is odd then just leave down pixle of image and
#devide it into 7 parts
img = read_image(image_name)
# convert image into list format for chisel
img = img.tolist()
height, width = image_size(img)
Initial = f'\t-Initial dimentions : Height -{height} Width -{width} '
##########################################################################################
# height chiseling
# check if there is any extra pixle line there
extra_height_pixle = height % 7
if extra_height_pixle != 0:
print('✅ chisel some pixles here')
# print(extra_height_pixle)
# check if the extra pixle is odd or even
if extra_height_pixle % 2 == 0:
# divide them equaly to top and bottom
removeable_lines = int(extra_height_pixle / 2)
extra_top = removeable_lines
extra_bottom = removeable_lines
print('\t-even height chiseling ->','extra_top',extra_top,' extra_bottom',extra_bottom)
for _ in range(extra_bottom): # or extra_top or removeable_lines
# always remove the first and last element of code
img.remove(img[0])
img.remove(img[-1])
else:
# divide them by giving one more to bottom line therefore it remove one extra line from the bottom
# the use of math.ceil is to always get proceding number
removeable_lines = math.ceil(extra_height_pixle/2)
if removeable_lines != 1:
extra_top = removeable_lines - 1
extra_bottom = removeable_lines
else:
extra_top = 0
extra_bottom = removeable_lines
print('\t-odd height chiseling ->','extra_top',extra_top,' extra_bottom',extra_bottom)
# real chiseling here
for i in range(extra_bottom): # here it should be extra_bottom because extra_top can be 0 here
# extra_bottom consist of equaly to "extra_top" + 1
# so one time of extra we wanna be delete only one ok last line and all the other cases we wanna delete both
if i == 0:
img.remove(img[-1]) # wanna remove down most element here
if i != 0:
img.remove(img[0])
img.remove(img[-1])
else:
pass
#############################################################################################
# updating height and width variables
height, width = image_size(img)
# print(height,width)
# dimentions of that block
dimnention = height/7
############################################################################################
extra_width_pixle = width%dimnention
if extra_width_pixle != 0:
# check if there is any extra pixle then require
if extra_width_pixle%2 == 0 :
# check if it is even then divide them equally amd remove them from the list
removeable_element = int(extra_width_pixle/2)
extra_right = removeable_element
extra_left = removeable_element
print('\t-even width chiseling ->','extra_right',extra_right,' extra_left',extra_left)
for i in img :
for _ in range(extra_left) : # can be removeable_element or extra_right because it even condition
# removeing element from the the list
i.remove(i[-1])
i.remove(i[0])
else:
# check if it is odd then divide them equally and give remaing one to left amd remove them from the list
removeable_element = math.ceil(extra_width_pixle/2)
if removeable_element != 1:
extra_right = removeable_element - 1
extra_left = removeable_element
else:
extra_right = 0
extra_left = removeable_element
print('\t-odd width chiseling ->','extra_right',extra_right,' extra_left',extra_left)
# print((img))
for i in img :
for j in range(extra_left) : # odd so it has to be extra_left
# removeing element from the the list
if j == 0:
i.remove(i[0]) # wanna remove left most element here
if j != 0 :
i.remove(i[-1])
i.remove(i[0])
# updating after width chiseler
height, width = image_size(img)
print(Initial)
print('\t-Ending dimentions : Height -',height,'Width -',width)
img = np.array(img,dtype ='uint8')
return (int(dimnention) ,img )
# chiseler('1.jpeg')
if __name__ == '__main__':
# example to run
'''python confirm.py <image name>'''
'''python confirm.py 3.jpeg'''
chiseler(sys.argv[1])