Last active
April 25, 2020 22:44
-
-
Save medic-code/367b11117a1910a838d3800cc850bbd1 to your computer and use it in GitHub Desktop.
Image sorting out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from GPSPhoto import gpsphoto | |
import requests | |
import os | |
import shutil | |
def address(data): | |
long = data['Longitude'] | |
lat = data['Latitude'] | |
response = requests.get(f'https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{long}&key=INSERT API KEY') | |
json_response = response.json() | |
city = json_response['results'][0]['address_components'][2]['long_name'] | |
return city | |
def year_folder(data): | |
year_path = os.getcwd() + '\\' + data['Date'].split('/')[-1] | |
if data['Date'].split('/')[-1] == year_path.split('\')[-1]: | |
return year_path | |
else: | |
os.mkdir(data['Date'].split('/')[-1]) | |
return year_path | |
def location_folder(year_path,city): | |
location_path = year_path + '\\' + city | |
if location_path.split()[-1] == city: | |
print('City Folder already created') | |
else: | |
os.mkdir(location_path) | |
print(f'City: {city} folder created ') | |
def move_images(year_path, city,a): | |
location_folder = year_path + f'\\{city}' | |
shutil.move(os.getcwd() + f'\\{a}',location_folder + f'\\{a}') | |
if ___name___ = ___main___: | |
image_list = os.listdir('F:\\images') | |
for a in image_list: | |
data = gpsphoto.getGPSData(os.getcwd() + f'\\{a}') | |
if data['Latitude']: | |
city = address(data) | |
else: | |
print('Image has no Address') | |
continue | |
if data['Date']: | |
year_path = year_folder(data) | |
else: | |
print('Image has no Date Attached') | |
continue | |
year_path, city = year_folder(data) | |
location_folder(year_path,city) | |
move_images(year_path,city,a) | |
print('Image moved succesfully') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment