-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_image.py
66 lines (52 loc) · 1.87 KB
/
clean_image.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 11 01:23:01 2018
@author: deepmind
"""
#to convert color jpeg to grey scale
import cv2
import glob
import numpy as np
import pandas as pd
import ntpath
df_pics_raw = []
lfiles = glob.glob('pics-raw/p2/**/**/*.tif')+glob.glob('pics-raw/**/**/**/*.tif')
lfiles = glob.glob('pics-raw/pA/**/**/*.tif')
lfiles += glob.glob('pics-raw/pB1/**/**/*.tif')
lfiles += glob.glob('pics-raw/pB2a/**/**/*.tif')
lfiles
for iPath in lfiles:
print(iPath)
try:
image = cv2.imread(iPath)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
abssum = np.sum(np.abs(gray_image-np.roll(gray_image,1)))
dFname = {'fpath':iPath,'dir':ntpath.dirname(iPath),'fname':ntpath.basename(iPath),'abssum':abssum}
df_pics_raw.append(dFname)
except:
print('error',iPath)
df_pics = pd.DataFrame(df_pics_raw)
df_pics = df_pics.sort_values(['dir','abssum'])
#df_pics.groupby('dir').head(1)
#df_pics.groupby('dir').tail(1)
for ig, dfg in df_pics.groupby('dir'):
for idf in dfg.head(1).to_dict('records'):
try:
ipath_tgt = 'pics-best/'+idf['fpath'].replace('/','-').replace('tif','jpg')
image = cv2.imread(idf['fpath'])
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# cv2.imwrite(ipath_tgt,gray_image)
cv2.imwrite(ipath_tgt,image)
except:
print('error',iPath)
for ig, dfg in df_pics.groupby('dir'):
for idf in dfg.tail(1).to_dict('records'):
try:
ipath_tgt = 'pics-worst/'+idf['fpath'].replace('/','-').replace('tif','jpg')
image = cv2.imread(idf['fpath'])
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# cv2.imwrite(ipath_tgt,gray_image)
cv2.imwrite(ipath_tgt,image)
except:
print('error',iPath)