Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Latitude and longitude from GPSInfo tag from PIL image._getexif() crashes in V3.4
-
Latitude and longitude from GPSInfo tag from PIL image._getexif() crashes in V3.4 With error
Problem does not come from Pythonista it-self but is due to upgrade of PIL, good to know.
lat,lon, lat_lon_txt = get_gps(img) File "/private/var/mobile/Containers/Shared/AppGroup/94C45A26-1F47-4486-9539-896F8F44AA91/Pythonista3/Documents/MesApps/Localisation photo.py", line 96, in get_gps lat = convert_to_degrees(gps_latitude) File "/private/var/mobile/Containers/Shared/AppGroup/94C45A26-1F47-4486-9539-896F8F44AA91/Pythonista3/Documents/MesApps/Localisation photo.py", line 109, in convert_to_degrees d0 = value[0][0] TypeError: 'IFDRational' object is not subscriptable
def get_gps(image): gps_latitude = None gps_latitude_ref = None gps_longitude = None gps_longitude_ref = None exif_info = image._getexif() # Extract exifs from photo #print(exif_info) if exif_info: for tag, value in exif_info.items(): # Loop on exifs of photo decoded = TAGS.get(tag, tag) # Exif code print(decoded,value) if decoded == "GPSInfo": # Exif is GPSInfo for t in value: # Loop on sub-exifs sub_decoded = GPSTAGS.get(t, t) # Sub-exif code print(sub_decoded,value[t]) if sub_decoded == 'GPSLatitude': gps_latitude = value[t] elif sub_decoded == 'GPSLatitudeRef': gps_latitude_ref = value[t] elif sub_decoded == 'GPSLongitude': gps_longitude = value[t] elif sub_decoded == 'GPSLongitudeRef': gps_longitude_ref = value[t] if gps_latitude and gps_latitude_ref and gps_longitude and gps_longitude_ref: lat = convert_to_degrees(gps_latitude) lat_lon_txt = str(lat)+gps_latitude_ref if gps_latitude_ref != "N": lat = 0 - lat lon = convert_to_degrees(gps_longitude) lat_lon_txt = lat_lon_txt+' - '+str(lon)+gps_longitude_ref if gps_longitude_ref != "E": lon = 0 - lon return lat, lon, lat_lon_txt return False,False,None def convert_to_degrees(value): # convert the GPS coordinates d m s to degrees in float d0 = value[0][0] d1 = value[0][1] d = float(d0) / float(d1) m0 = value[1][0] m1 = value[1][1] m = float(m0) / float(m1) s0 = value[2][0] s1 = value[2][1] s = float(s0) / float(s1) return d + (m / 60.0) + (s / 3600.0)
It seems that with Pillow version 7 (and most likely below) the degree, minute and second values were tuples that were to be interpreted as fractions (the first value being the numerator and the second the denominator). Pillow version 8 provides just a number that can be used directly
Thus, if you use this GPSInfo, you have to change your code...