首页 > 解决方案 > PIL - 将图像类型从 L 转换为 F 后无法更改像素值 - AttributeError: 'int' object has no attribute 'data'

问题描述

我有一个"L"转换为 float 的类型图像"F"。转换后尝试更改其像素值时出现错误。下面是一个片段。

a = mnist_images[i].convert(mode="F")
a = a.point(lambda j: 0)

和错误信息

<ipython-input-45-f89cda3cf30e> in generate_moving_mnist(shape, seq_len, seqs, num_sz, nums_per_image)
     86                 b=mnist_images[i].point(lambda j: 0)
     87                 a=mnist_images[i].convert(mode="F")#.point(lambda j: 0)
---> 88                 a = a.point(lambda j: 0)
     89 
     90                 # paste the mnist_images[i] at position[i] on the current canvas

~\anaconda3\envs\tf\lib\site-packages\PIL\Image.py in point(self, lut, mode)
   1572                 # UNDONE wiredfool -- I think this prevents us from ever doing
   1573                 # a gamma function point transform on > 8bit images.
-> 1574                 scale, offset = _getscaleoffset(lut)
   1575                 return self._new(self.im.point_transform(scale, offset))
   1576             # for other modes, convert the function to a table

~\anaconda3\envs\tf\lib\site-packages\PIL\Image.py in _getscaleoffset(expr)
    483 def _getscaleoffset(expr):
    484     stub = ["stub"]
--> 485     data = expr(_E(stub)).data
    486     try:
    487         (a, b, c) = data  # simplified syntax

AttributeError: 'int' object has no attribute 'data'

如果我不转换图像类型,我可以更改像素值,即下面的行本身可以正常工作。

b = mnist_images[i].point(lambda j: 0)

我目前正在使用一种解决方法,但我认为它很慢

a=mnist_images[i].copy()
a=a.convert(mode="F")
  for row in range(a.size[0]):
    for col in range(a.size[1]):
      a.putpixel((row, col), new_pixel_value)

标签: python-imaging-library

解决方案


从第 1581 行看来,浮动图像类型不支持PIL/Image.py该函数:point()"F"

if self.mode == "F":
    # FIXME: _imaging returns a confusing error message for this case
    raise ValueError("point operation not supported for this mode")

如果您愿意说出您实际尝试做的事情,我可能会建议一种解决方法。


推荐阅读