Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7ee7f31

Browse files
authored
Create exif_csv.py
1 parentdb1587c commit7ee7f31

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

‎exif_csv.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Disclaimer: This script is for educational purposes only.
2+
# Do not use against any photos that you don't own or have authorization to test.
3+
4+
#!/usr/bin/env python3
5+
6+
# Please note:
7+
# This program is for .JPG and .TIFF format files. The program could be extended to support .HEIC, .PNG and other formats.
8+
# Installation and usage instructions:
9+
# 1. Install Pillow (Pillow will not work if you have PIL installed):
10+
# python3 -m pip install --upgrade pip
11+
# python3 -m pip install --upgrade Pillow
12+
# 2. Add .jpg images downloaded from Flickr to subfolder ./images from where the script is stored.
13+
# Try the following Flickr account: https://www.flickr.com/photos/194419969@N07/? (Please don't use other Flickr accounts).
14+
# Note most social media sites strip exif data from uploaded photos.
15+
16+
importos
17+
importcsv
18+
fromPILimportImage
19+
fromPIL.ExifTagsimportGPSTAGS,TAGS
20+
21+
22+
# Helper function
23+
defcreate_google_maps_url(gps_coords):
24+
# Exif data stores coordinates in degree/minutes/seconds format. To convert to decimal degrees.
25+
# We extract the data from the dictionary we sent to this function for latitudinal data.
26+
dec_deg_lat=convert_decimal_degrees(float(gps_coords["lat"][0]),float(gps_coords["lat"][1]),float(gps_coords["lat"][2]),gps_coords["lat_ref"])
27+
# We extract the data from the dictionary we sent to this function for longitudinal data.
28+
dec_deg_lon=convert_decimal_degrees(float(gps_coords["lon"][0]),float(gps_coords["lon"][1]),float(gps_coords["lon"][2]),gps_coords["lon_ref"])
29+
# We return a search string which can be used in Google Maps
30+
returnf"https://maps.google.com/?q={dec_deg_lat},{dec_deg_lon}"
31+
32+
33+
# Converting to decimal degrees for latitude and longitude is from degree/minutes/seconds format is the same for latitude and longitude. So we use DRY principles, and create a seperate function.
34+
defconvert_decimal_degrees(degree,minutes,seconds,direction):
35+
decimal_degrees=degree+minutes/60+seconds/3600
36+
# A value of "S" for South or West will be multiplied by -1
37+
ifdirection=="S"ordirection=="W":
38+
decimal_degrees*=-1
39+
returndecimal_degrees
40+
41+
42+
# Print Logo
43+
print("""
44+
______ _ _ ______ _ _
45+
| _ \ (_) | | | ___ \ | | | |
46+
| | | |__ ___ ___ __| | | |_/ / ___ _ __ ___ | |__ __ _| |
47+
| | | / _` \ \ / / |/ _` | | ___ \/ _ \| '_ ` _ \| '_ \ / _` | |
48+
| |/ / (_| |\ V /| | (_| | | |_/ / (_) | | | | | | |_) | (_| | |
49+
|___/ \__,_| \_/ |_|\__,_| \____/ \___/|_| |_| |_|_.__/ \__,_|_|
50+
51+
52+
53+
_______ _____________ _____ _____ _____ _
54+
| ___\ \ / /_ _| ___| |_ _| _ || _ | |
55+
| |__ \ V / | | | |_ | | | | | || | | | |
56+
| __| / \ | | | _| | | | | | || | | | |
57+
| |___/ /^\ \_| |_| | | | \ \_/ /\ \_/ / |____
58+
\____/\/ \/\___/\_| \_/ \___/ \___/\_____/
59+
60+
61+
""")
62+
63+
# Add files to the folder ./images
64+
# We assign the cwd to a variable. We will refer to it to get the path to images.
65+
cwd=os.getcwd()
66+
# Change the current working directory to the one where you keep your images.
67+
os.chdir(os.path.join(cwd,"images"))
68+
# Get a list of all the files in the images directory.
69+
files=os.listdir()
70+
71+
# Check if you have any files in the ./images folder.
72+
iflen(files)==0:
73+
print("You don't have have files in the ./images folder.")
74+
exit()
75+
# Loop through the files in the images directory.
76+
77+
# We open a csv file to write the data to it.
78+
withopen("../exif_data.csv","a",newline="")ascsv_file:
79+
# We create a writer for the csv format.
80+
writer=csv.writer(csv_file)
81+
82+
forfileinfiles:
83+
# We add try except black to handle when there are wrong file formats in the ./images folder.
84+
try:
85+
# Open the image file. We open the file in binary format for reading.
86+
image=Image.open(file)
87+
print(f"_______________________________________________________________{file}_______________________________________________________________")
88+
# The ._getexif() method returns a dictionary. .items() method returns a list of all dictionary keys and values.
89+
gps_coords= {}
90+
writer.writerow(("Filename",file))
91+
# We check if exif data are defined for the image.
92+
ifimage._getexif()==None:
93+
writer.writerow((file,"Contains no exif data."))
94+
# If exif data are defined we can cycle through the tag, and value for the file.
95+
else:
96+
fortag,valueinimage._getexif().items():
97+
# If you print the tag without running it through the TAGS.get() method you'll get numerical values for every tag. We want the tags in human-readable form.
98+
# You can see the tags and the associated decimal number in the exif standard here: https://exiv2.org/tags.html
99+
tag_name=TAGS.get(tag)
100+
iftag_name=="GPSInfo":
101+
forkey,valinvalue.items():
102+
# Write the GPS Data value for every key to the csv file.
103+
writer.writerow((GPSTAGS.get(key), {val}))
104+
# We add Latitude data to the gps_coord dictionary which we initialized in line 110.
105+
ifGPSTAGS.get(key)=="GPSLatitude":
106+
gps_coords["lat"]=val
107+
# We add Longitude data to the gps_coord dictionary which we initialized in line 110.
108+
elifGPSTAGS.get(key)=="GPSLongitude":
109+
gps_coords["lon"]=val
110+
# We add Latitude reference data to the gps_coord dictionary which we initialized in line 110.
111+
elifGPSTAGS.get(key)=="GPSLatitudeRef":
112+
gps_coords["lat_ref"]=val
113+
# We add Longitude reference data to the gps_coord dictionary which we initialized in line 110.
114+
elifGPSTAGS.get(key)=="GPSLongitudeRef":
115+
gps_coords["lon_ref"]=val
116+
else:
117+
# We write data not related to the GPSInfo to the csv file.
118+
writer.writerow((tag_name,value))
119+
# We print the longitudinal and latitudinal data which has been formatted for Google Maps. We only do so if the GPS Coordinates exists.
120+
ifgps_coords:
121+
# We write the Google Maps Link to the csv file.
122+
writer.writerow(("Google Maps Link",create_google_maps_url(gps_coords)))
123+
# Change back to the original working directory.
124+
exceptIOError:
125+
print("File format not supported!")
126+
127+
os.chdir(cwd)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp