我编写了一个脚本来更新 WhatsApp 图像的 exif 数据。我想使用 Pydroid 应用程序在我的 Android 手机上运行它,但最终出现 PermissionError: [Errno 13] Permission returned 错误。
import os
import piexif
import datetime
dir_path = '/storage/emulated/0/Android/media/com.whatsapp/WhatsApp/Media/WhatsApp Images'
img_count = 1
for root, dirs, files in os.walk(dir_path):
for fname in files:
if fname.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
img_path = os.path.join(root, fname)
exif_dict = piexif.load(img_path)
if not exif_dict['Exif'].get(piexif.ExifIFD.DateTimeOriginal, ""):
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(img_path)).strftime("%Y:%m:%d %H:%M:%S")
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = mtime
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, img_path)
print(f"[{img_count}] DateTimeOriginal of {img_path} updated successfully to {mtime}")
img_count += 1
我发现有些人建议将这些行添加到代码中以请求写入权限:
from android.permissions import request_permissions, Permission
request_permissions([Permission.WRITE_EXTERNAL_STORAGE, ...])
但是我最终遇到了另一个问题:
Error importing module "android": this module is a part of Kivy
You cannot use it from elsewhere and you barely need it at all
This is not a bug, please do not report it via email or Google Play reviews, thank you for your understanding
还有其他选项可以在 Android 上授予 WRITE 权限吗?或者还有其他对 Android 文件执行批量 exif 数据更新的解决方案吗? 我也熟悉 Java,所以我可以尝试一下,如果我是 Python,这是不可能的。