-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoCrop.py
More file actions
96 lines (80 loc) · 2.71 KB
/
AutoCrop.py
File metadata and controls
96 lines (80 loc) · 2.71 KB
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
import cv2
import os, os.path
import glob
def get_contours(img):
# First make the image 1-bit and get contours
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 90, 255, 0)
#cv2.imwrite('thresh.jpg', thresh)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# filter contours that are too large or small
size = get_size(img)
contours = [cc for cc in contours if contourOK(cc, size)]
return contours
def get_size(img):
ih, iw = img.shape[:2]
return iw * ih
def contourOK(cc, size=1000000):
x, y, w, h = cv2.boundingRect(cc)
if w < 50 or h < 50: return False # too narrow or wide is bad
area = cv2.contourArea(cc)
return area < (size * 0.5) and area > 200
def find_boundaries(img, contours):
# margin is the minimum distance from the edges of the image, as a fraction
ih, iw = img.shape[:2]
minx = iw
miny = ih
maxx = 0
maxy = 0
for cc in contours:
x, y, w, h = cv2.boundingRect(cc)
if x < minx: minx = x
if y < miny: miny = y
if x + w > maxx: maxx = x + w
if y + h > maxy: maxy = y + h
return (minx+20, miny+20, maxx-20, maxy-20)
def crop(img, boundaries):
minx, miny, maxx, maxy = boundaries
return img[miny:maxy, minx:maxx]
def process_image(fimage, fpath, t):
img = cv2.imread(fimage)
width = int(img.shape[1] * 0.1)
height = int(img.shape[0] * 0.1)
dim = (width, height)
# resize image
img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
contours = get_contours(img)
cv2.drawContours(img, contours, -1, (0,255,0)) # draws contours, good for debugging
bounds = find_boundaries(img, contours)
cropped = crop(img, bounds)
if get_size(cropped) < 400: return # too small
fpath+=('/')
fpath+=(str(t))
fpath+=('.jpg')
print("ended")
#print(fpath)
cv2.imwrite(fpath, cropped)
#process_image('runes/testing/Ansuz/IMG_20200220_211502.jpg')
testing_dir= 'runes/testing/'# do somthing with this need to traverse training file
saving_dir= 'runes/saving/'
files = []
save = []
images = []
j = 0
types = ('*.bmp', '*.BMP', '*.tiff', '*.TIFF', '*.tif', '*.TIF', '*.jpg', '*.JPG', '*.JPEG', '*.jpeg') # all should work but only .jpg was tested
for t in os.listdir(testing_dir):
#if glob.glob(t) != []:
testing_dir = 'runes/testing/'
saving_dir = 'runes/saving/'
testing_dir+=t
saving_dir+=t
files.append(testing_dir)
save.append(saving_dir)
for i in range (0,len(files)):
for t in os.listdir(files[i]):
j+=1
testing_dir = files[i]
testing_dir+='/'
testing_dir+=t
images.append(testing_dir)
process_image(images[j-1], save[i], j)