简体   繁体   English

使用 PIL 保存图像会引发大文件的损坏错误

[英]Image saving with PIL throws corruption error for large file size

I'm converting a large numpy array to tif (2.28G) with PIL, but I am getting a file corrupted error.我正在使用 PIL 将大型 numpy 数组转换为 tif (2.28G),但出现文件损坏错误。 I ran the exact same code with a smaller array before and it worked (1.7G).我之前用较小的数组运行了完全相同的代码,它工作了(1.7G)。 How can I go about and solve this problem?我该如何解决这个问题?

edits:编辑:

  • The numpy array I cannot convert has dtype ('uint8') and dimension (854715, 890, 3)我无法转换的 numpy 数组具有 dtype ('uint8') 和维度 (854715, 890, 3)
  • The numpy array I can save is 'uint8' with dimensions (674775, 890, 3)我可以保存的 numpy 数组是“uint8”,尺寸为(674775、890、3)
  • I have 1960MB inactive memory and 40MB free memory我有 1960MB 非活动 memory 和 40MB 空闲 memory

The code:编码:

import sys
from PIL import Image
import numpy as np
import os

folder =  'Core4_4712_4803'

# read all file names from defined folder core_images into a 
Python list (collection of data)
images = os.listdir(folder) 
# list to collect UV stacked numpy array
combine_core_UV = []

print(images)
for img_name in images:
    im = Image.open(folder+"/"+img_name, 'r')
    img = np.array(im)

    x = 330
    x2 = 1663
    x3 = 3240
    x4 = 4457
    x5 = 5830
    y = 1675
    w = 890 # x2 - x1
    h = 8997 # y2 - y1
    crop_img1 = img[y:y+h, x:x+w]
    crop_img2 = img[y:y+h, x2:x2+w]
    crop_img3 = img[y:y+h, x3:x3+w]
    crop_img4 = img[y:y+h, x4:x4+w]
    crop_img5 = img[y:y+h, x5:x5+w]
    combined_image =np.concatenate((crop_img1,crop_img2,crop_img3,crop_img4,crop_img5),axis=0)
    combine_core_UV.append(combined_image)
    
   "concatenate pieces vertically"
combined_image_UV = np.concatenate(combine_core_UV,axis=0)
# save image
im = Image.fromarray(combined_image_UV)
im.save("uv_stacked_image_"+folder+".tif")

[picture error][1] [1]: https://i.stack.imgur.com/SW8JS.png [图片错误][1][1]:https://i.stack.imgur.com/SW8JS.png

You appear to be loading all the parts you want into an enormous list which you then concatenate into an array which you then save as an image, which means you are trying to hold everything in a list and an array and an image at the same time thereby requiring 3x the RAM you might otherwise need.您似乎正在将所需的所有部分加载到一个巨大的列表中,然后将其连接到一个数组中,然后将其另存为图像,这意味着您正在尝试将所有内容同时保存在列表、数组和图像中因此需要 3 倍您可能需要的 RAM。

Try creating your output image at the start, outside the loop then grab one piece at a time and paste it into the right place in the output image then move on to the next ROI without accumulating everything in memory except the output image. Try creating your output image at the start, outside the loop then grab one piece at a time and paste it into the right place in the output image then move on to the next ROI without accumulating everything in memory except the output image.

You might also try adding compression='lzw' when saving your output file if you have libtiff installed.如果您安装了libtiff ,您也可以在保存 output 文件时尝试添加compression='lzw'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM