Python 获取图像文件夹经纬度到 df
Python getting image folder lat and lon to df
我试图在 python 中构建一个代码来遍历文件夹中的所有照片并获取 exif 信息(只有日期和 GPS 信息是相关的)。
我在网上找到了一个代码,但它给了我一个错误。
代码:
def find_images(image_dir, extensions=None):
default_extensions = ('jpg', 'jpeg')
if extensions is None:
extensions = default_extensions
elif isinstance(extensions, str):
extensions = (extensions,)
for root, dirs, filenames in os.walk(image_dir):
for filename in filenames:
# print(filename, filename.split('.', 1))
if filename.split('.', 1)[-1].lower() in extensions:
yield os.path.join(root, filename)
def process_exif_data(image):
decoded_exif = {}
with Image.open(image) as image_file:
exif_data = image_file._getexif()
if exif_data is None:
return None
for tag, value in exif_data.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
decoded_exif.update(decode_gps_data(value))
else:
decoded_exif[decoded] = value
# This could be done with a dict comprehension and a ternary expression too
return decoded_exif
def decode_gps_data(info):
gps_tags = {GPSTAGS.get(k, k): v for k, v in value.items}
lat, long = get_coordinates(gps_tags)
gps_tags['lat'] = lat
gps_tags['lon'] = lon
return gps_tags
def get_coordinates(gps_tags):
coords = {'Latitude': 'N', 'Longitude': 'E'}
for coord, nominal_ref in coords.items():
c = gps_tags.get("GPS%s" % coord, None)
c_ref = gps_tags.get("GPS%sRef" % coord, None)
if c and c_ref:
yield _convert_to_degrees(c, c_ref, nominal_ref)
else:
yield None
def _convert_to_degrees(value, ref, nominal_ref=None):
if nominal_ref is None:
nominal_ref = ('N', 'E',)
elif isinstance(nom, str):
nominal_ref = (nominal_ref, )
ref = 1 if ref in nominal_ref else -1
return ref * sum(float(v[0]) / float(v[1]) / 60 ** i for i, v in enumerate(value))
def extract_important_data(image_data, important_datalabels=('lat', 'lon', 'DateTimeOriginal')):
if image_data is None:
return None
return {key: image_data.get(key, None) for key in important_datalabels}
def extract_info(images, important_datalabels=('lat', 'lon', 'DateTimeOriginal')):
for image_path in images:
print(image_path)
exif_data = process_exif_data(image_path)
important_data = extract_important_data(exif_data)
if important_data:
yield image_path, important_data
def get_photos_info(image_dir, filename=None, important_datalabels=('lat', 'lon', 'DateTimeOriginal')):
if image_dir is None:
image_dir='.'
images = find_images(image_dir)
info = extract_info(imgs, important_datalabels=important_datalabels)
result_df = pd.DataFrame(columns = important_datalabels)
for image_path, item in info:
result_df.loc[image_path] = item
if 'DateTimeOriginal' in important_datalabels:
date_format = '%Y:%m:%d %H:%M:%S'
result_df['DateTimeOriginal'] = pd.to_datetime(result_df['DateTimeOriginal'], format=date_format)
if filename:
result_df.to_excel(filename)
return result_df
empty_df = pd.DataFrame
empty_df = get_photos_info(r"C:\Users\dor h\OneDrive\Desktop\study\Final project")
错误:
AttributeError Traceback (most recent call last)
<ipython-input-32-0aaf3d855413> in <module>
123
124 empty_df = pd.DataFrame
--> 125 empty_df = get_photos_info(r"C:\Users\dor h\OneDrive\Desktop\study\Final project")
126 # print(empty_df)
<ipython-input-32-0aaf3d855413> in get_photos_info(image_dir, filename, important_datalabels)
111 info = extract_info(imgs, important_datalabels=important_datalabels)
112 result_df = pd.DataFrame(columns = important_datalabels)
--> 113 for image_path, item in info:
114 result_df.loc[image_path] = item
115 if 'DateTimeOriginal' in important_datalabels:
<ipython-input-32-0aaf3d855413> in extract_info(images, important_datalabels)
99 for image_path in images:
100 print(image_path)
--> 101 exif_data = process_exif_data(image_path)
102 important_data = extract_important_data(exif_data)
103 if important_data:
<ipython-input-32-0aaf3d855413> in process_exif_data(image)
48 def process_exif_data(image):
49 decoded_exif = {}
---> 50 with Image.open(image) as image_file:
51 exif_data = image_file._getexif()
52 if exif_data is None:
D:\paython\lib\site-packages\PIL\Image.py in open(fp, mode)
2816 exclusive_fp = True
2817
-> 2818 prefix = fp.read(16)
2819
2820 preinit()
AttributeError: 'JpegImageFile' object has no attribute 'read'
我在某处读到问题是我正在尝试打开一个已经打开的 jpg 文件,但我看不到我在做什么,所以我也不知道如何解决这个问题。
提前tnx
看来 info = extract_info(imgs, important_datalabels=important_datalabels)
行中的 imgs
是错字,应该是 images
.
为什么这会为您提供您所看到的特定错误模式取决于 imgs
是什么。变量 imgs
必须在笔记本中的某处定义,否则您的代码将引发 NameError。 imgs
可能是一个列表或迭代器,其中包含 Image.open()
不期望的某种对象;这会导致它在尝试使用对象的 read
方法时失败。
我试图在 python 中构建一个代码来遍历文件夹中的所有照片并获取 exif 信息(只有日期和 GPS 信息是相关的)。 我在网上找到了一个代码,但它给了我一个错误。 代码:
def find_images(image_dir, extensions=None):
default_extensions = ('jpg', 'jpeg')
if extensions is None:
extensions = default_extensions
elif isinstance(extensions, str):
extensions = (extensions,)
for root, dirs, filenames in os.walk(image_dir):
for filename in filenames:
# print(filename, filename.split('.', 1))
if filename.split('.', 1)[-1].lower() in extensions:
yield os.path.join(root, filename)
def process_exif_data(image):
decoded_exif = {}
with Image.open(image) as image_file:
exif_data = image_file._getexif()
if exif_data is None:
return None
for tag, value in exif_data.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
decoded_exif.update(decode_gps_data(value))
else:
decoded_exif[decoded] = value
# This could be done with a dict comprehension and a ternary expression too
return decoded_exif
def decode_gps_data(info):
gps_tags = {GPSTAGS.get(k, k): v for k, v in value.items}
lat, long = get_coordinates(gps_tags)
gps_tags['lat'] = lat
gps_tags['lon'] = lon
return gps_tags
def get_coordinates(gps_tags):
coords = {'Latitude': 'N', 'Longitude': 'E'}
for coord, nominal_ref in coords.items():
c = gps_tags.get("GPS%s" % coord, None)
c_ref = gps_tags.get("GPS%sRef" % coord, None)
if c and c_ref:
yield _convert_to_degrees(c, c_ref, nominal_ref)
else:
yield None
def _convert_to_degrees(value, ref, nominal_ref=None):
if nominal_ref is None:
nominal_ref = ('N', 'E',)
elif isinstance(nom, str):
nominal_ref = (nominal_ref, )
ref = 1 if ref in nominal_ref else -1
return ref * sum(float(v[0]) / float(v[1]) / 60 ** i for i, v in enumerate(value))
def extract_important_data(image_data, important_datalabels=('lat', 'lon', 'DateTimeOriginal')):
if image_data is None:
return None
return {key: image_data.get(key, None) for key in important_datalabels}
def extract_info(images, important_datalabels=('lat', 'lon', 'DateTimeOriginal')):
for image_path in images:
print(image_path)
exif_data = process_exif_data(image_path)
important_data = extract_important_data(exif_data)
if important_data:
yield image_path, important_data
def get_photos_info(image_dir, filename=None, important_datalabels=('lat', 'lon', 'DateTimeOriginal')):
if image_dir is None:
image_dir='.'
images = find_images(image_dir)
info = extract_info(imgs, important_datalabels=important_datalabels)
result_df = pd.DataFrame(columns = important_datalabels)
for image_path, item in info:
result_df.loc[image_path] = item
if 'DateTimeOriginal' in important_datalabels:
date_format = '%Y:%m:%d %H:%M:%S'
result_df['DateTimeOriginal'] = pd.to_datetime(result_df['DateTimeOriginal'], format=date_format)
if filename:
result_df.to_excel(filename)
return result_df
empty_df = pd.DataFrame
empty_df = get_photos_info(r"C:\Users\dor h\OneDrive\Desktop\study\Final project")
错误:
AttributeError Traceback (most recent call last)
<ipython-input-32-0aaf3d855413> in <module>
123
124 empty_df = pd.DataFrame
--> 125 empty_df = get_photos_info(r"C:\Users\dor h\OneDrive\Desktop\study\Final project")
126 # print(empty_df)
<ipython-input-32-0aaf3d855413> in get_photos_info(image_dir, filename, important_datalabels)
111 info = extract_info(imgs, important_datalabels=important_datalabels)
112 result_df = pd.DataFrame(columns = important_datalabels)
--> 113 for image_path, item in info:
114 result_df.loc[image_path] = item
115 if 'DateTimeOriginal' in important_datalabels:
<ipython-input-32-0aaf3d855413> in extract_info(images, important_datalabels)
99 for image_path in images:
100 print(image_path)
--> 101 exif_data = process_exif_data(image_path)
102 important_data = extract_important_data(exif_data)
103 if important_data:
<ipython-input-32-0aaf3d855413> in process_exif_data(image)
48 def process_exif_data(image):
49 decoded_exif = {}
---> 50 with Image.open(image) as image_file:
51 exif_data = image_file._getexif()
52 if exif_data is None:
D:\paython\lib\site-packages\PIL\Image.py in open(fp, mode)
2816 exclusive_fp = True
2817
-> 2818 prefix = fp.read(16)
2819
2820 preinit()
AttributeError: 'JpegImageFile' object has no attribute 'read'
我在某处读到问题是我正在尝试打开一个已经打开的 jpg 文件,但我看不到我在做什么,所以我也不知道如何解决这个问题。
提前tnx
看来 info = extract_info(imgs, important_datalabels=important_datalabels)
行中的 imgs
是错字,应该是 images
.
为什么这会为您提供您所看到的特定错误模式取决于 imgs
是什么。变量 imgs
必须在笔记本中的某处定义,否则您的代码将引发 NameError。 imgs
可能是一个列表或迭代器,其中包含 Image.open()
不期望的某种对象;这会导致它在尝试使用对象的 read
方法时失败。