FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
softwarestudies/scripts/panelops.py at master · jaychow/softwarestudies · GitHub
jaychow
softwarestudies
Repository navigation
Code
Issues
4
(4)
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
softwarestudies
/
scripts
/
panelops.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
300 lines (231 loc) · 8.78 KB
Breadcrumbs
softwarestudies
/
scripts
/
panelops.py
Copy path
File metadata and controls
executable file
·
300 lines (231 loc) · 8.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
import
sys
import
os
from
PIL
import
Image
from
PIL
import
ImageDraw
import
math
def
cutOffEdges
(
image
):
print
"Cropping Edges..."
,
imageCopy
=
image
.
copy
()
#size
xSize
=
image
.
size
[
0
]
ySize
=
image
.
size
[
1
]
#find left most frame edge
image
=
image
.
convert
(
"L"
)
#make into matrix
pix
=
image
.
load
()
whiteThresh
=
100
#find upper edge
upperEdge
=
0
thisRowAllWhite
=
True
for
y
in
range
(
0
,
ySize
):
if
not
thisRowAllWhite
:
upperEdge
=
y
break
for
x
in
range
(
0
,
xSize
):
if
not
(
pix
[
x
,
y
]
>
whiteThresh
):
thisRowAllWhite
=
False
#find left edge
leftEdge
=
0
thisColumnAllWhite
=
True
for
x
in
range
(
0
,
xSize
):
if
not
thisColumnAllWhite
:
leftEdge
=
x
break
for
y
in
range
(
0
,
ySize
):
if
not
(
pix
[
x
,
y
]
>
whiteThresh
):
thisColumnAllWhite
=
False
#find bottom edge
bottomEdge
=
ySize
thisRowAllWhite
=
True
ry
=
range
(
0
,
ySize
-
1
)
ry
.
reverse
()
for
y
in
ry
:
if
not
thisRowAllWhite
:
bottomEdge
=
y
break
for
x
in
range
(
0
,
xSize
):
if
not
(
pix
[
x
,
y
]
>
whiteThresh
):
thisRowAllWhite
=
False
#find right edge
rightEdge
=
xSize
thisColumnAllWhite
=
True
rx
=
range
(
0
,
xSize
-
1
)
rx
.
reverse
()
for
x
in
rx
:
if
not
thisColumnAllWhite
:
rightEdge
=
x
break
for
y
in
range
(
0
,
ySize
):
if
not
(
pix
[
x
,
y
]
>
whiteThresh
):
thisColumnAllWhite
=
False
print
"L:"
,
leftEdge
,
"U:"
,
upperEdge
,
"R:"
,
rightEdge
,
"B:"
,
bottomEdge
image
=
imageCopy
image
=
image
.
crop
((
leftEdge
,
upperEdge
,
rightEdge
,
bottomEdge
))
return
image
def
getNextPanel
(
image
,
imageName
,
outDir
,
panelNum
,
threshdir
,
thresh
):
print
"Finding Panel"
,
panelNum
imageCopy
=
image
.
copy
()
#size
xSize
=
image
.
size
[
0
]
ySize
=
image
.
size
[
1
]
image
=
image
.
convert
(
"L"
)
#make into matrix
pix
=
image
.
load
()
minPanelWidth
=
xSize
/
4
minPanelHeight
=
ySize
/
4
whiteThresh
=
int
(
thresh
)
fudgeFactor
=
max
([
ySize
,
xSize
])
/
500
d
=
int
(
threshdir
)
print
"w:"
,
d
if
d
<
0
:
bgc
=
'black'
else
:
bgc
=
'white'
upperEdge
=
0
#assumes the upper edge is at 0
leftEdge
=
0
#ditto left
#code to find the upper left corner so that you don't have to crop it first
#find upper edge
upperEdge
=
0
notWhiteCount
=
0
#print "Finding upper edge..."
for
y
in
range
(
0
,
ySize
-
1
):
#print "Trying row", y
for
x
in
range
(
0
,
xSize
-
1
):
if
(
x
>=
xSize
)
and
(
y
>=
ySize
):
#print "Ran out of image at", (x,y)
return
imageCopy
if
d
*
pix
[
x
,
y
]
<
d
*
whiteThresh
:
notWhiteCount
+=
1
#print "notWhiteCount:", notWhiteCount
if
notWhiteCount
>
minPanelWidth
-
fudgeFactor
:
upperEdge
=
y
break
#find left edge
leftEdge
=
0
notWhiteCount
=
0
#print "Finding left edge..."
for
x
in
range
(
0
,
xSize
-
1
):
#print "Trying column", x
for
y
in
range
(
upperEdge
,
upperEdge
+
minPanelHeight
):
if
(
x
>=
xSize
)
or
(
y
>=
ySize
):
#print "Ran out of image at", (x,y)
return
imageCopy
if
d
*
pix
[
x
,
y
]
<
d
*
whiteThresh
:
notWhiteCount
+=
1
#print "notWhiteCount:", notWhiteCount
if
notWhiteCount
>
minPanelHeight
-
fudgeFactor
:
leftEdge
=
x
break
print
"upperEdge:"
,
upperEdge
print
"leftEdge:"
,
leftEdge
x
=
leftEdge
####### GUTTERS #############
#go down left edge until there is a white line
for
y
in
range
(
upperEdge
+
(
fudgeFactor
),
ySize
-
1
):
#could this be the start of a gutter?
if
d
*
pix
[
x
,
y
]
>
d
*
whiteThresh
:
#found a white pixel
couldBeAGutter
=
True
#assumes that its true
#print "Possible horz. gutter at", y
#checks assumtion
notWhiteCount
=
0
for
x
in
range
(
leftEdge
,
leftEdge
+
minPanelWidth
):
#see if the whiteness goes in enough that it could be a gutter
if
x
>=
xSize
-
1
or
y
>=
ySize
-
1
:
break
if
(
x
>=
xSize
-
fudgeFactor
)
and
(
y
>=
ySize
-
fudgeFactor
):
#print "Ran out of image at", (x,y)
return
imageCopy
if
d
*
pix
[
x
,
y
]
<
d
*
whiteThresh
:
notWhiteCount
+=
1
#########################################################
### I would like to bring attention to this next line ###
#########################################################
if
notWhiteCount
>
fudgeFactor
:
### The right side of this inequality is the gutter fudge factor
couldBeAGutter
=
False
#if assumtion holds
if
couldBeAGutter
or
(
y
>=
ySize
-
fudgeFactor
):
#found one on the left edge or at bottom of image
if
(
y
>=
ySize
-
fudgeFactor
):
print
"Full height panel"
#print "Got horz. gutter at", y #well sort of... ;)
yGutterLoc
=
y
#save this y so we can resue the variable
y
=
upperEdge
#go along top until there is a white line down
for
x
in
range
(
leftEdge
,
xSize
-
1
):
#could this be the start of another gutter?
if
(
d
*
pix
[
x
,
y
]
>
d
*
whiteThresh
)
or
(
x
>=
xSize
-
fudgeFactor
):
#found a white pixel along top or at end of picture
couldBeAGutter
=
True
#print "Possible vert. gutter at",(x,y)
#checks if it really could be
notWhiteCount
=
0
for
y
in
range
(
upperEdge
,
upperEdge
+
minPanelHeight
):
#see if the whiteness goes down
if
d
*
pix
[
x
,
y
]
<
d
*
whiteThresh
:
#it doesnt
#print "Possible vert. gutter at", (x,y), "fails"
notWhiteCount
+=
1
#########################################################
### I would like to bring attention to this next line ###
#########################################################
if
notWhiteCount
>
fudgeFactor
:
### The right side of this inequality is the gutter fudge factor
couldBeAGutter
=
False
#else:
#print "Checks out", pix[x,y], "at", (x,y)
#yep
if
couldBeAGutter
or
(
x
>=
xSize
-
fudgeFactor
):
#found another or at right
if
(
x
>=
xSize
-
fudgeFactor
):
print
"Full width panel"
#OK so we've found another possible gutter
#we should check if they meet but I wont ;)
print
"Got panel"
,
panelNum
image
=
imageCopy
print
"Cropping"
, (
leftEdge
,
upperEdge
),(
x
,
yGutterLoc
)
if
(
x
-
leftEdge
)
<=
fudgeFactor
or
(
yGutterLoc
-
upperEdge
)
<=
fudgeFactor
:
#this isn't a panel it's must be some kind of artifact
print
"Found artifact at"
, (
leftEdge
,
upperEdge
),(
x
,
yGutterLoc
)
draw
=
ImageDraw
.
Draw
(
image
)
draw
.
rectangle
([
leftEdge
-
fudgeFactor
,
upperEdge
-
fudgeFactor
,
x
,
yGutterLoc
],
fill
=
bgc
)
draw
=
None
return
image
panel
=
imageCopy
.
crop
((
leftEdge
,
upperEdge
,
x
,
yGutterLoc
))
#adds this panel to the list
#write to output file
path
=
outDir
fo
=
open
(
os
.
path
.
join
(
path
,
'PanelData.txt'
),
"a"
)
fo
.
write
(
str
(
imageName
)
+
', '
+
str
(
panelNum
)
+
', '
+
str
(
leftEdge
)
+
', '
+
str
(
upperEdge
)
+
', '
+
str
(
x
)
+
', '
+
str
(
yGutterLoc
)
+
'
\n
'
)
#save panel
print
"Saving..."
,
if
not
os
.
path
.
isdir
(
outDir
):
os
.
mkdir
(
outDir
)
newFileName
=
imageName
.
rstrip
(
".jpg"
)
#pageNum = newFileName[newFileName.index('-')+1:]
#print "Page #", pageNum
#issue = newFileName[:newFileName.index('-')]
newFileName
=
newFileName
+
"-f"
+
str
(
panelNum
)
+
".jpg"
print
newFileName
path
=
outDir
panel
.
convert
(
'RGB'
).
save
(
os
.
path
.
join
(
path
,
newFileName
),
"JPEG"
)
draw
=
ImageDraw
.
Draw
(
image
)
print
"Fill Color ="
,
max
([
0
,
255
*
d
])
draw
.
rectangle
([
leftEdge
-
fudgeFactor
,
upperEdge
-
fudgeFactor
,
x
,
yGutterLoc
],
fill
=
bgc
)
draw
=
None
return
image
#I guess we didn't find anything
return
image
#image is now greyscale btw. return imageCopy if you want... hope you don't actually end up here
def
checkAllWhite
(
image
,
imageName
):
print
"Checking All White..."
imageCopy
=
image
.
copy
()
#size
xSize
=
image
.
size
[
0
]
ySize
=
image
.
size
[
1
]
image
=
image
.
convert
(
"L"
)
#make into matrix
pix
=
image
.
load
()
maxNumPanels
=
4
minPanelWidth
=
300
minPanelHeight
=
400
whiteThresh
=
220
for
y
in
range
(
0
,
ySize
):
for
x
in
range
(
0
,
xSize
):
if
pix
[
x
,
y
]
<
whiteThresh
:
print
"This one did not check out. You might want to have to look at it..."
print
"Extra info:"
, (
x
,
y
),
"Value:"
,
pix
[
x
,
y
]
return
-
1
print
"All white"
return
0
Back
|
FazBrowse Home
|
New Git URL