File size: 1,370 Bytes
96cd238 1946e20 96cd238 |
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 |
# iterate files over subdirectories in `00_store`, store image files in hash table, delete files with same hash
import os
import hashlib
import imghdr
deleted = 0
def is_image(filename, verbose=False):
data = open(filename, "rb").read(10)
# check if file is JPG or JPEG
if data[:3] == b"\xff\xd8\xff":
if verbose == True:
print(filename + " is: JPG/JPEG.")
return True
# check if file is PNG
if data[:8] == b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a":
if verbose == True:
print(filename + " is: PNG.")
return True
# check if file is GIF
if data[:6] in [b"\x47\x49\x46\x38\x37\x61", b"\x47\x49\x46\x38\x39\x61"]:
if verbose == True:
print(filename + " is: GIF.")
return True
return False
def action(in_path):
for root, _, files in os.walk(in_path):
for file in files:
file_path = os.path.join(root, file)
# print(file_path)
with open(file_path, "rb") as f:
if is_image(file_path):
# it's a jpeg
pass
else:
# print(file_path, "is not valid jpeg")
os.remove(file_path)
CURR_DIR = os.path.dirname(os.path.abspath(__file__))
action(CURR_DIR + "/data/train")
action(CURR_DIR + "/data/validate")
|