- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
代码:
#!/usr/bin/python3
from PIL import Image, ExifTags
img = Image.open("/root/Bilder/sysPics/schwarz-weiß-Karte.jpg")
for i, j in img._getexif().items():
if i in ExifTags.TAGS:
print(ExifTags.TAGS[i] + " - " + str(j))
错误:
Traceback (most recent call last):
File "python-tool.py", line 7, in <module>
for i, j in img._getexif().items():
AttributeError: 'NoneType' object has no attribute 'items'
谁能帮帮我?我从未使用过 PIL 库,但我看到了一个教程,此 .items()
方法在其中起作用:
https://www.youtube.com/watch?v=-PiR5SX4Mxo&list=PLNmsVeXQZj7onbtIXvxZTzeKGnzI6NFp_&index=8
他的代码和我的没有区别,我不敢相信他们在最后的补丁中切断了 .items()
方法。
最佳答案
第一,您不应该依赖内部函数,例如 _get_exif()
因为它们的实现可以随时更改,而且它们通常不供公众使用。 (参见 PEP8 命名约定中的 _single_leading_underscore
)。
第二,您应该考虑到并非所有图像都有 EXIF 数据。尝试获取 EXIF 数据可能会 None
.所以它不是 .items()
这就是问题的方法,而是你的 _get_exif()
返回 None
.您的代码没有针对这种情况的处理,您总是假设 _get_exif()
返回 dict
.
现在,为了解决您的问题,对于 Python3.x(我有 Python3.6.8)和 PIL(安装为 Pillow,Pillow==6.0.0
),Image
对象现在提供公共(public) getexif()
您可以使用的方法。返回类型是 None
如果图像没有 EXIF 数据或 <class 'PIL.Image.Exif'>
.
from PIL import Image, ExifTags
img = Image.open("sample.jpg")
print(dir(img))
# [... 'getexif' ...]
img_exif = img.getexif()
if img_exif:
print(type(img_exif))
# <class 'PIL.Image.Exif'>
print(dict(img_exif))
# { .. 271: 'FUJIFILM', 305: 'Adobe Photoshop Lightroom 6.14 (Macintosh)', }
img_exif_dict = dict(img_exif)
for key, val in img_exif_dict.items():
if key in ExifTags.TAGS:
print(ExifTags.TAGS[key] + " - " + str(val))
else:
print("Sorry, image has no exif data.")
关于python - 获取 "' NoneType' 对象没有属性 'items'“PIL 库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56560211/
我目前正在尝试定义函数,但遇到了这个错误。我只是想做一个简单的函数,用户输入 2 个数字,然后将它们相乘。也请尽可能简单地解释我做错了什么。 (我是菜鸟) def userinput(): w
使用IPtools python 包我试图查看 IP 地址是否在特定范围内。这是我的代码: for line in g: org= line.split("|")[0] ranges
输入 [['1','2','3'],['a','b','c'],['6','7','8'],['e','f','g']] 输出应该是: 1, 2, 3a, b, c6, 7, 8e, f, g Cod
我目前正在使用 lambda 使 tkinter 按钮依次执行两件事: def classManip(): cManip = tk.Toplevel() cManip.title
我正在学习Python,作为练习,我编写了一些代码来查找用户定义函数的导数。代码如下。 def fx(value, function): x = value return eval(f
使用 Django。我有以下模型: class Postagem(models.Model): id = models.AutoField(primary_key=True, editable=Fal
我正在尝试为给定的数据集选择重要的特征(或者至少了解哪些特征解释更多的变异性)。为此,我使用 ExtraTreesClassifier 和 GradientBoostingRegressor - 然后
刚刚获得了SDXL模型的访问权限,希望为即将发布的版本进行测试...不幸的是,我们当前用于我们服务的代码似乎不能与稳定ai/稳定-扩散-xl-base-0.9一起工作,我不完全确定SDXL有什么不同,
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我想遍历可迭代列表,但要求某些元素的类型可以是 None。 这看起来像这样: none_list = [None, [0, 1]] for x, y in none_list: print("
我得到object is not subscriptable在非空查询结果上。当我打印时 c.fetchone()它打印了正确的结果,但是当我检查类型时它显示 import sqlite3 conn
我在第 15 行收到此错误,但我不明白为什么。有任何想法吗?看来属性已经明确定义了,所以我完全不知所措。任何帮助将非常感激。AttributeError:“NoneType”对象没有属性“Sheets
我尝试对 Chrome WebDriver 进行子类化以包含一些初始化和清理代码,但随后 Python 提示创建的对象设置为 None: import glob import selenium imp
这个问题已经有答案了: Why do I get AttributeError: 'NoneType' object has no attribute 'something'? (10 个回答) 已关
这个问题已经有答案了: Why does the print function return None? (1 个回答) 已关闭 6 年前。 我对 Python 还很陌生。我正在制作一个生成器,可以为
我正在尝试比较两个表( table_a 和 table_b )并减去 table_a 的最后一列从table_b的最后一列开始。但是,table_a 包含一个额外的行,导致我得到 NoneType错误
当“文件名”是一个存在的文件时,这段代码运行良好……但是当它不存在时……我不断收到同样的错误:TypeError: 'NoneType' 对象不可迭代 (Errno 2) 尽管我从不迭代任何东西,除非
我在下面的代码中收到“NoneType”对象不可迭代的 TypeError。下面的代码用于使用 pyautogui 滚动 digits 文件夹中的 10 张图像(命名为 0 到 9,以图像中的 # 命
我有一段代码表现得很奇怪。 一开始,我导入了一个模块,它是 C 库的 python 绑定(bind)。 try: import pyccn except: print "ERROR:
我是一名优秀的程序员,十分优秀!