环境

Windows 10 LTSC 1809 17763.1457
Python 3.7.6
exif 1.0.1
plum-py 0.3.1

更新 2020-10-19

exif 1.0.2 已修复此 Bug。
This BUG has been fixed in exif 1.0.2.

问题 Bug

Python exif plum-py Bug:当 getattr 或 hasattr 的参数 name = lens_specification 时,一定报错ZeroDivisionError:division by zero
Python exif plum-py Bug: When the parameter name = 'lens_specification' of getattr or hasattr, an error must be reported ZeroDivisionError: division by zero

解决

方法一(建议)

exif 1.0.2 已修复此 Bug。
This BUG has been fixed in exif 1.0.2.

方法二(临时)

由于不清楚每个数值是怎么来的,所以目前采用的临时解决方案。如果 rational_view.denominator = 0,就直接增加 0。
只需修改 exififd_tag_rational.py 中 read 中部分代码即可,如下。
Since it is not clear how each value came from, the temporary solution currently adopted. If rational_view.denominator's value = 0, add 0 directly.
Just modify the exif\ifd_tag\_rational.py intermediate 'read' part of the code, as follows.

......
    def read(self):
        """Read tag value.

        :returns: tag value
        :rtype: corresponding Python type

        """
        retvals = []

        for rational_index in range(self.tag_view.value_count.get()):
            current_offset = self.tag_view.value_offset.get() + rational_index * self.rational_dtype_cls.nbytes
            rational_view = self.rational_dtype_cls.view(self._app1_ref.body_bytes, current_offset)
            # retvals.append(rational_view.numerator / rational_view.denominator) # 原 old
            # 新 new
            if rational_view.denominator == 0: 
                retvals.append(0)
            else:
                retvals.append(rational_view.numerator / rational_view.denominator)
......

方法三:

遇到此参数时,跳过获取值。

方法四:

使用 exif 软件对 lens_specification 进行编辑时,只需要删除 1 个数字并再次添加它,就不会报告错误。

原因

2020-09-14

默认值=显示值=[50, 50, 0, 0],而实际值为 [50, 50, 0/0, 0/0],即当程序执行时,获取到的数据为 [50, 50, 0/0, 0\0].
当执行 getattr 时,代码执行置获取"0/0时",导致 Rational_view.numerator 和 Rational_view.denominator 的值均为 0。
在下一步计算'rational_view.numerator / rational_view.denominator'时,则为'0/0',计算机中"0/0"这个运算当然是不可能执行的,则报错"ZeroDivisionError: division by zero"。
当使用其他 exif 软件对值进行修改后,实际存储值才为 [50, 50, 0, 0]。

Default value = display value = [50, 50, 0, 0], and the actual variable = [50, 50, 0/0, 0/0].
When the program is executed, the acquired data is [50, 50, 0/0, 0/0].
When getattr is executed, when the code is executed to obtain "0/0", this causes the values ​​of Rational_view.numerator and Rational_view.denominator to become 0.
And in the following calculation, " rational_view.Numerator / Rational_view.denominator", which is "0/0", of course the computer cannot perform the calculation of "0/0", and the error "ZeroDivisionError: Division by zero" is reported.
When the value is modified by other exif software, the actual stored value will be [50, 50, 0, 0].

2020-09-14

进行了一些尝试,发现可能的原因是 lens_specification 的默认值。
例如:默认值为[50, 50, 0, 0],但是当使用 exif 软件对其进行编辑时,只需要删除 1 个数字并再次添加它,就不会报告错误。

  • 疑问:不清楚为什么,编码问题?

I made some attempts and found that the possible reason is the default value of lens_specification.
For example: The default value is [50, 50, 0, 0], but when I edit it with exif software, I only need to delete 1 number and add it again, and no error will be reported.

  • I was surprised, what exactly was sent in the process?

2020-09-13

已经向作者提出 Issue。

错误代码 Error Code

lens_specification <class 'str'>
Traceback (most recent call last):
  File "exif_demo1.py", line 74, in <module>
    main()
  File "exif_demo1.py", line 18, in main
    d = exif_get_attrs_value(image, image_attrs)
  File "exif_demo1.py", line 68, in exif_get_attrs_value
    value = getattr(image, str(attr), None)
  File "D:\Python\xuexi\venv\lib\site-packages\exif\_image.py", line 86, in __getattr__
    return getattr(self._segments['APP1'], item)
  File "D:\Python\xuexi\venv\lib\site-packages\exif\_app1_metadata.py", line 405, in __getattr__
    return ifd_tag.read()
  File "D:\Python\xuexi\venv\lib\site-packages\exif\ifd_tag\_rational.py", line 75, in read
    retvals.append(rational_view.numerator / rational_view.denominator)
  File "D:\Python\xuexi\venv\lib\site-packages\plum\_plumview.py", line 275, in __truediv__
    return self.get() / other
  File "D:\Python\xuexi\venv\lib\site-packages\plum\_plumview.py", line 269, in __rtruediv__
    return other / self.get()
ZeroDivisionError: division by zero
最后修改:2020 年 10 月 19 日 09 : 20 AM
如果觉得文章帮助了您,您可以随意赞赏。