Sure, here’s the translation for “Need help in Image.save()”: 需要帮助 Image.save()

huangapple go评论87阅读模式
英文:

Need help in Image.save()

问题

  1. 我正在努力保存一个 .tiff 文件,代码如下:

image_array = np.vstack(band1, band2, band3)
image = Image.fromarray(image_array, mode='RGB')
file_name = 'img2.tiff'
image.save(file_name, format='TIFF')

  1. 但是它给我报错:

***错误:参数超出范围 ***

  1. 由于我正在处理一个非常大的文件和大型数据集,我在第二个图像上遇到了问题,但第一个图像也很大,只是形状稍小。我该怎么办?
  2. 我期望代码能够运行并使用 PIL 库中的此函数保存文件,以便我可以预处理数据。
英文:

I am working on save a .tiff file from an array the code is :

  1. image_array = np.vstack(band1,band2,band3)
  2. image=Image.fromarray(image_array,mode='RGB')
  3. file_name='img2.tiff'
  4. image.save(file_name,format='TIFF')

But it is giving me error:

  1. ***error: argument out of range ***

As I am working on a very large file and large dataset. I am facing problem for the 2nd image but the 1st image is also very big but a little bit small in shape. What should I do

I was expecting the code to run and save the file using this fnx. from PIL library, so that I can pre-process the data

答案1

得分: 0

正如 @cgohlke 在评论中指出的,如果文件未经压缩,则超过4GB。因此,这将需要使用 PIL 无法写入的 BigTIFF。您可以通过以下方式查看:

  1. import numpy as np
  2. from PIL import Image
  3. # 合成与您的大小相同的图像数据
  4. d = np.zeros((32797, 44161, 3), np.uint8)
  5. # 检查大小
  6. print(d.size) # 打印 4,345,044,951 - 对于普通的 TIFF 来说太大了
  7. # 转换为 PIL 图像
  8. im = Image.fromarray(d)
  9. # 尝试保存
  10. im.save("result.tif") # 错误 'L' 格式要求 0 <= number <= 4294967295
  11. # 尝试保存压缩 - 可行
  12. im.save("result.tif", compression='tiff_lzw')

因此,令人不满的是,您依赖于压缩来保存图像,尽管这可能有效。我认为您最好采纳 Christoph 的建议并使用 tifffile 或另一个库,比如 wand

如果您的数据是卫星图像,通常会有大面积的黑色区域,这是由于地球旋转导致的区域偏移,这些区域应该很容易压缩,因此可能对您有利。


我注意到 OpenCV 也可以保存您的文件,它也使用无损的 LZW 压缩。

  1. import cv2
  2. # 使用上述的 Numpy 数组 'd' 以 TIFF 格式写入
  3. cv2.imwrite('result.tif', d)

然而,如果您提供的数据不够可压缩,它也会失败:

  1. # 生成不够可压缩的数据
  2. d = np.random.randint(0, 256, (32797, 44161, 3), np.uint8)
  3. cv2.imwrite('result.tif', d) # 失败
英文:

As @cgohlke points out in the comments, your file exceeds 4GB if uncompressed. As such, that would require a BigTIFF which PIL cannot write. You can see this with:

  1. import numpy as np
  2. from PIL import Image
  3. # Synthesize image data same size as yours
  4. d = np.zeros((32797,44161,3), np.uint8)
  5. #&#160;Check size
  6. print(d.size) # prints 4,345,044,951 - too big for regular TIFF
  7. # Make into PIL Image
  8. im = Image.fromarray(d)
  9. # Try to save
  10. im.save(&quot;result.tif&quot;) #&#160;ERROR &#39;L&#39; format requires 0 &lt;= number &lt;= 4294967295
  11. # Try to save compressed - WORKS
  12. im.save(&quot;result.tif&quot;, compression=&#39;tiff_lzw&#39;)

So, it is rather unsatisfactory that you are reliant on compression to be able to save your image - though it may work. I think you would be well advised to take Christoph's advice and use tifffile or another library - such as wand.

If your data is satellite imagery, there are often large areas of black where the swathes are offset as a result of Earth's rotation, these should be readily compressible so that may work in your favour.


I note that OpenCV can save your file and it too uses lossless LZW compression.

  1. import cv2
  2. #&#160;Write Numpy array &#39;d&#39; from above as TIFF
  3. cv2.imwrite(&#39;result.tif&#39;, d)

However, it too fails if you give it less compressible data:

  1. #&#160;Make less compressible data
  2. d = np.random.randint(0, 256, (32797,44161,3), np.uint8)
  3. cv2.imwrite(&#39;result.tif&#39;, d) #&#160;!!! FAILS !!!

huangapple
  • 本文由 发表于 2023年3月21日 03:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794354-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定