如何将子流程的输出保存到数据帧?

发布于 2025-01-11 10:20:52 字数 1162 浏览 3 评论 0原文

我正在编写一个脚本,用于从 RTK 无人机图像中提取 exif 数据(纬度、经度和高度)。我或多或少地从 YouTube 视频(Franchyze923)复制了下面的代码 - 做了一些修改。 [我编码的时间很短]。如何获取子流程的结果以保存到表/数据框(最终我想将信息保存到 .csv)。

该脚本的不同版本为每个图像生成了一个 .csv - 然后我将所有 csv 文件导入并将 pd.concat() 它们放入一个数据帧中。这可行但看起来很笨重。

import os
import subprocess

#Extracting exif data for images in Agisoft folder

exiftool_location = #path to exiftool.exe
images_to_extract_exif = #path to images

for path, directories, files in os.walk(images_to_extract_exif):
    for images_to_extract_exif in files:
        if images_to_extract_exif.endswith("JPG"):
            full_jpg_path = os.path.join(path, images_to_extract_exif)
            exiftool_command = [exiftool_location, "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-T", "-n", full_jpg_path]
            subprocess.run(exiftool_command)

代码的输出看起来很棒 - 我只是不知道如何将其保存到表/数据帧中。

DJI_0001.JPG    45.2405341666667    -95.3808298055556   354.427
DJI_0002.JPG    45.2405253333333    -95.3808253055556   354.434
DJI_0003.JPG    45.2404568888889    -95.3808200277778   354.447
DJI_0004.JPG    45.2403695277778    -95.3808205555556   354.431

I'm working on a script to extract exif data (Latitude, Longitude, and Altitude) from RTK drone images. I have more or less copied the code below from a youtube video (Franchyze923)- with a few modifications. [I've been coding for a very short time]. How can I get the results of the subprocess to save to a table/dataframe (eventually I want to save the information to a .csv).

A different version of this script generated a .csv for every image - which I then imported all the csv files and pd.concat() them into one dataframe. That works but seems clunky.

import os
import subprocess

#Extracting exif data for images in Agisoft folder

exiftool_location = #path to exiftool.exe
images_to_extract_exif = #path to images

for path, directories, files in os.walk(images_to_extract_exif):
    for images_to_extract_exif in files:
        if images_to_extract_exif.endswith("JPG"):
            full_jpg_path = os.path.join(path, images_to_extract_exif)
            exiftool_command = [exiftool_location, "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-T", "-n", full_jpg_path]
            subprocess.run(exiftool_command)

The output from the code looks great - I just have no clue how to save it to a table/dataframe.

DJI_0001.JPG    45.2405341666667    -95.3808298055556   354.427
DJI_0002.JPG    45.2405253333333    -95.3808253055556   354.434
DJI_0003.JPG    45.2404568888889    -95.3808200277778   354.447
DJI_0004.JPG    45.2403695277778    -95.3808205555556   354.431

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦纸 2025-01-18 10:20:52

这是收集 ExifTool 输出并放入 DataFrame 的方法。我没有使用 -T 来输出文本,而是使用 -j 来获取 json。 Json 可以很容易地在 Python 中读取,因此我们不必解释文本。默认情况下,ExifTool 的 json 输出包含一个 SourceFile 标记,其中文件路径作为值。我将提供一种从输出中删除此标记的方法,但它可以作为 -filename 的替代方案。

import json
import subprocess
import pandas as pd
# from pathlib import Path

# You can pass the dir containing all the files to ExifTool (as StarGeek suggested)
dir_path = "C:/Path/To/Dir"

# If you need to use the file paths in Python:
# fp_list = [fp for fp in Path(dir_path).iterdir()]
# cmd_list = ["exiftool", "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-j", "-n", *fp_list]

cmd_list = ["exiftool", "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-j", "-n", dir_path]
proc = subprocess.run(cmd_list, capture_output=True)
# Setting capture_output=True captures ExifTool's output in stdout and errors in stderr
exif_list = json.loads(proc.stdout)

# If you want to delete SourceFile tags:
for d in exif_list:
    del d["SourceFile"]

# exif_list = [{FileName:val, gpslatitude:val, gpslongitude:val, gpsaltitude:val}, ..., {FileName:val, gpslatitude:val, gpslongitude:val, gpsaltitude:val}]

exif_df = pd.DataFrame(exif_list)

exif_df:

FileNamegpslatitudegpslongitudegpsaltitude
0文件名valvalval
1文件名valvalval

Here is a way to collect the ExifTool output and put in a DataFrame. Instead of using -T to output text I used -j to get json. Json can be easily read in Python so we don't have to interpret the text. By default ExifTool's json output includes a SourceFile tag with the file path as the value. I'll include a way to delete this tag from the output but it could be an alternative to -filename.

import json
import subprocess
import pandas as pd
# from pathlib import Path

# You can pass the dir containing all the files to ExifTool (as StarGeek suggested)
dir_path = "C:/Path/To/Dir"

# If you need to use the file paths in Python:
# fp_list = [fp for fp in Path(dir_path).iterdir()]
# cmd_list = ["exiftool", "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-j", "-n", *fp_list]

cmd_list = ["exiftool", "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-j", "-n", dir_path]
proc = subprocess.run(cmd_list, capture_output=True)
# Setting capture_output=True captures ExifTool's output in stdout and errors in stderr
exif_list = json.loads(proc.stdout)

# If you want to delete SourceFile tags:
for d in exif_list:
    del d["SourceFile"]

# exif_list = [{FileName:val, gpslatitude:val, gpslongitude:val, gpsaltitude:val}, ..., {FileName:val, gpslatitude:val, gpslongitude:val, gpsaltitude:val}]

exif_df = pd.DataFrame(exif_list)

exif_df:

FileNamegpslatitudegpslongitudegpsaltitude
0file namevalvalval
1file namevalvalval
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文