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

Commitdb1587c

Browse files
authored
Create exif.py
1 parent0275cf5 commitdb1587c

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

‎exif.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
importsys
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+
64+
# Choice whether to keep output in the Terminal or redirect to a file.
65+
whileTrue:
66+
output_choice=input("How do you want to receive the output:\n\n1 - File\n2 - Terminal\nEnter choice here: ")
67+
try:
68+
conv_val=int(output_choice)
69+
ifconv_val==1:
70+
# We redirect the standard output stream to a file instead of the screen.
71+
sys.stdout=open("exif_data.txt","w")
72+
break
73+
elifconv_val==2:
74+
# The standard output stream is the screen so we don't need to redirect and just need to break the while loop.
75+
break
76+
else:
77+
print("You entered an incorrect option, please try again.")
78+
except:
79+
print("You entered an invalid option, please try again.")
80+
81+
82+
# Add files to the folder ./images
83+
# We assign the cwd to a variable. We will refer to it to get the path to images.
84+
cwd=os.getcwd()
85+
# Change the current working directory to the one where you keep your images.
86+
os.chdir(os.path.join(cwd,"images"))
87+
# Get a list of all the files in the images directory.
88+
files=os.listdir()
89+
90+
# Check if you have any files in the ./images folder.
91+
iflen(files)==0:
92+
print("You don't have have files in the ./images folder.")
93+
exit()
94+
# Loop through the files in the images directory.
95+
forfileinfiles:
96+
# We add try except black to handle when there are wrong file formats in the ./images folder.
97+
try:
98+
# Open the image file. We open the file in binary format for reading.
99+
image=Image.open(file)
100+
print(f"_______________________________________________________________{file}_______________________________________________________________")
101+
# The ._getexif() method returns a dictionary. .items() method returns a list of all dictionary keys and values.
102+
gps_coords= {}
103+
# We check if exif data are defined for the image.
104+
ifimage._getexif()==None:
105+
print(f"{file} contains no exif data.")
106+
# If exif data are defined we can cycle through the tag, and value for the file.
107+
else:
108+
fortag,valueinimage._getexif().items():
109+
# 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.
110+
# You can see the tags and the associated decimal number in the exif standard here: https://exiv2.org/tags.html
111+
tag_name=TAGS.get(tag)
112+
iftag_name=="GPSInfo":
113+
forkey,valinvalue.items():
114+
# Print the GPS Data value for every key to the screen.
115+
print(f"{GPSTAGS.get(key)} -{val}")
116+
# We add Latitude data to the gps_coord dictionary which we initialized in line 110.
117+
ifGPSTAGS.get(key)=="GPSLatitude":
118+
gps_coords["lat"]=val
119+
# We add Longitude data to the gps_coord dictionary which we initialized in line 110.
120+
elifGPSTAGS.get(key)=="GPSLongitude":
121+
gps_coords["lon"]=val
122+
# We add Latitude reference data to the gps_coord dictionary which we initialized in line 110.
123+
elifGPSTAGS.get(key)=="GPSLatitudeRef":
124+
gps_coords["lat_ref"]=val
125+
# We add Longitude reference data to the gps_coord dictionary which we initialized in line 110.
126+
elifGPSTAGS.get(key)=="GPSLongitudeRef":
127+
gps_coords["lon_ref"]=val
128+
else:
129+
# We print data not related to the GPSInfo.
130+
print(f"{tag_name} -{value}")
131+
# We print the longitudinal and latitudinal data which has been formatted for Google Maps. We only do so if the GPS Coordinates exists.
132+
ifgps_coords:
133+
print(create_google_maps_url(gps_coords))
134+
# Change back to the original working directory.
135+
exceptIOError:
136+
print("File format not supported!")
137+
138+
ifoutput_choice=="1":
139+
sys.stdout.close()
140+
os.chdir(cwd)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp