Python PIL错误“L”格式需要0 < = number < = 4294967295

1yjd4xko  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(173)

我创建了一个int的numpy数组,然后将其转换为uint32类型的PIL Image,然后尝试保存它。请参见下面的简单模拟代码:

import numpy
resLabels = numpy.ndarray(shape=(38000, 38000), dtype=numpy.int32)
resLabels.fill(0)
resLabels[0,0] = 10000000 # Just a dumb value to ensure that the int32 is required.
arr = resLabels.astype("uint32")
im = Image.fromarray(arr)
im.save("Test.tif")

不幸的是,当我试图保存图像时,在最后一行出现以下错误:

Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/Image.py", line 2431, in save
save_handler(self, fp, filename)
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 1860, in _save
offset = ifd.save(fp)
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 945, in save
result = self.tobytes(offset)
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 889, in tobytes
data = self._write_dispatch[typ](self, *values)
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 699, in <lambda>
b"".join(self._pack(fmt, value) for value in values)
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 699, in <genexpr>
b"".join(self._pack(fmt, value) for value in values)
      File "/Users//miniconda3/envs/Py39dev/lib/python3.9/site-packages/PIL/TiffImagePlugin.py", line 666, in _pack
return struct.pack(self._endian + fmt, *values)
struct.error: 'L' format requires 0 <= number <= 4294967295

经过快速测试,它似乎来自图像维度。对于30Kx30K的图像,一切正常,但对于40Kx40K的图像,一切都失败。
如何解决这个问题?

bf1o4zei

bf1o4zei1#

我测试我的图像的整体大小:如果width x height x channels x encoding大于4294967295,则使用tifffile库。

相关问题