python-imaging-library - 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)
解决方案
从第 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")
如果您愿意说出您实际尝试做的事情,我可能会建议一种解决方法。
推荐阅读
- angular - 表格的角度指令
- php - 从用户字段中删除用户?(WordPress)
- sql - 根据列中重复值的天数计算
- linux - 在单独的线程上并发 msync
- swift - 请求正文中的 HTTP POST“/”字符问题-Swift
- bash - 如何从文件中 grep 并将结果另存为单独的文件
- python - 将 Panda-Dataframe 从横截面重新排列为面板数据
- c# - 从数据库中检索数据到多选下拉列表和选择下拉列表(Asp.Net Core MVC)
- javascript - 语义ui反应从复选框中获取多个选中值
- javascript - 单击在 js 中创建的图像以显示信息