forked from alsrnv/google_earth_pro_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_cleaner.py
43 lines (32 loc) · 1.42 KB
/
image_cleaner.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
import pandas as pd
import os
import shutil
import config
import logging
CURRENT_PATH = os.getcwd()
IMAGE_PATH = os.path.join(CURRENT_PATH, config.IMAGES_FOLDER)
class ImageCleaner():
def __init__(self, path_images = IMAGE_PATH):
self.path_images = path_images
def group_image(self):
logging.info('starting grouping images')
all_files = [file for file in os.listdir(self.path_images) if file.endswith('.jpg')]
df = pd.DataFrame({'filename': all_files})
df['file_number'] = df['filename'].apply(lambda x: x.split('_')[1].split('.')[0]).astype(int)
df['coords'] = df['filename'].apply(lambda x: x.split('_')[0])
for coord, group in df.groupby('coords'):
os.mkdir(os.path.join(self.path_images, coord))
#moving files to the new directory
for _, row in group.iterrows():
filename = row['filename']
file_number = row['file_number']
shutil.move(os.path.join(self.path_images, filename),
os.path.join(self.path_images, coord, filename))
os.rename(os.path.join(self.path_images, coord, filename),
os.path.join(self.path_images, coord, str(file_number) + '.jpg'))
def delete_duplicates(self):
logging.info('starting deleting duplicates for each folder')
folders = list(filter(lambda x: os.path.isdir(os.path.join(self.path_images, x)),
os.listdir(self.path_images)))
for folder in folders:
os.system('image-cleaner {}'.format(os.path.join(self.path_images, folder)))