-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolor_channel_conv.py
197 lines (138 loc) · 6.91 KB
/
color_channel_conv.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
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
from PIL import Image
import os
from os import listdir
from os.path import isfile, join
import pandas as pd
import numpy as np
import tensorflow as tf
output_set = './output/'
dirname = ['train_set', 'valid_set', 'test_set']
subdirname = ['0', '1']
# Creating a list of path (6 elements) that are going to be used for reading images. It contains the train-valid-test set with their subfolders (aka their binary labels).
fullpath_sets = [str(join(output_set, dirn, subdirn + '/')) for dirn in dirname for subdirn in subdirname]
train_df = pd.read_csv(output_set + 'train_set.csv')
valid_df = pd.read_csv(output_set + 'valid_set.csv')
test_df = pd.read_csv(output_set + 'test_set.csv')
def remove_pic_from_df(df, file_lst):
'''
Removing pics from the corresponding dataframe
'''
indexNames = df[ df['fname'].isin(file_lst) ].index
df = df.drop(indexNames).reset_index(drop=True)
return df
### Deleting pixel limit exceeding images and pics with .gif extension, and converting images with color channels other than RGB
for fps in fullpath_sets:
onlyfiles = [f for f in listdir(fps) if isfile(join(fps, f))]
A_channel = list() # A list that are going to conatin images with A(alpha) channel
NonA_channel = list() # A list that are NOT going to conatin images with A(alpha) channel
explode_lst = list() # A list that are going to conatin images bigger than the PIL's limit pixel size, 89478485
gif_to_delete = list() # A list containing pics with .gif extension
for pic in onlyfiles:
### Deleting .gif files from the folder
if pic[-3:] == 'gif':
gif_to_delete.append(pic)
os.remove(fps + pic)
continue
else:
try:
img = Image.open(fps + pic)
### Throwing out images bigger than the PIL's limit pixel size, 89478485 and collect those pictures into the 'explode_lst'
if img.size[0] * img.size[1] > 89478485:
explode_lst.append(pic)
os.remove(fps + pic)
continue
else:
### Collecting images with color channels other than RGB (such as CMYK, RGBA, P, L, LA) to their respective A_channel or NonA_channel list
image_mode = img.mode
if (image_mode != 'RGB') and ('A' not in image_mode):
NonA_channel.append(pic)
elif (image_mode != 'RGB') and ('A' in image_mode):
A_channel.append(pic)
else: #elif img.mode == 'RGB'
continue
except:
continue
### Converting images with color channels other than RGB (such as CMYK, RGBA, P, L, LA) to RGB from A_channel or NonA_channel list
for fn in A_channel:
try:
img = Image.open(fps + fn)
img.load()
new_img = Image.new("RGB", img.size, (255, 255, 255))
new_img.paste(img, mask=img.getchannel('A'))
file_format = fn.rsplit('.', maxsplit=1)[1].upper()
if file_format == 'JPG':
file_format = 'JPEG'
new_img.save(fps + fn, file_format, quality=100)
except:
continue
for fn in NonA_channel:
try:
img = Image.open(fps + fn)
img = img.convert('RGB')
file_format = fn.rsplit('.', maxsplit=1)[1].upper()
if file_format == 'JPG':
file_format = 'JPEG'
img.save(fps + fn, file_format, quality = 100)
except:
continue
### Removing pixel limit exceeding images and images with .gif extension from the appropriate dataframe
if fps.split('/')[2] == 'train_set':
train_df = remove_pic_from_df(train_df, explode_lst)
train_df = remove_pic_from_df(train_df, gif_to_delete)
elif fps.split('/')[2] == 'valid_set':
valid_df = remove_pic_from_df(valid_df, explode_lst)
valid_df = remove_pic_from_df(valid_df, gif_to_delete)
elif fps.split('/')[2] == 'test_set':
test_df = remove_pic_from_df(test_df, explode_lst)
test_df = remove_pic_from_df(test_df, gif_to_delete)
### Sanity check whether all pictures are RGB and if an image cannot be decoded by tensorflow.
for fps in fullpath_sets:
onlyfiles = [f for f in listdir(fps) if isfile(join(fps, f))]
rouge_lst = list() ### A list of images that cannot be decoded by tensorflow or have any other flaws other than that.
notRGB_lst = list() ### A list of nonRGB pictures (hopefully this list will be empty in each directory after the conversion made previously)
for pic in onlyfiles:
img_raw = tf.io.read_file(fps + pic)
try:
img_dec = tf.io.decode_image(img_raw)
except:
print('-------Rouge Picture according to TF:----------')
print('filepath:',fps)
print('image:', pic)
print('-----------------\n')
rouge_lst.append(pic)
os.remove(fps + pic)
continue
try:
img = Image.open(fps + pic)
if img.mode == 'RGB':
continue
else:
print('######Non-RGB picture########')
print('The non-RGB picture was found in this path:', fps + pic)
notRGB_lst.append(pic)
print('##############\n')
except:
print('-------Rouge Picture according to any other errors:----------')
print('filepath:',fps)
print('image:', pic)
print('-----------------\n')
rouge_lst.append(pic)
os.remove(fps + pic)
continue
print("--------Before removing elements from the dataframe, let's summaraize------------")
print('filepath:',fps)
print('length of rouge_lst:', rouge_lst)
print('length of notRGB_lst:', notRGB_lst)
### Removing the above mentioned images from dataframe.
if fps.split('/')[2] == 'train_set':
train_df = remove_pic_from_df(train_df, rouge_lst)
train_df = remove_pic_from_df(train_df, notRGB_lst)
elif fps.split('/')[2] == 'valid_set':
valid_df = remove_pic_from_df(valid_df, rouge_lst)
valid_df = remove_pic_from_df(valid_df, notRGB_lst)
elif fps.split('/')[2] == 'test_set':
test_df = remove_pic_from_df(test_df, rouge_lst)
test_df = remove_pic_from_df(test_df, notRGB_lst)
train_df.to_csv(output_set + 'train_set.csv', index = False)
valid_df.to_csv(output_set + 'valid_set.csv', index = False)
test_df.to_csv(output_set + 'test_set.csv', index = False)