[英]Image saving with PIL throws corruption error for large file size
我正在使用 PIL 將大型 numpy 數組轉換為 tif (2.28G),但出現文件損壞錯誤。 我之前用較小的數組運行了完全相同的代碼,它工作了(1.7G)。 我該如何解決這個問題?
編輯:
編碼:
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")
[圖片錯誤][1][1]:https://i.stack.imgur.com/SW8JS.png
您似乎正在將所需的所有部分加載到一個巨大的列表中,然后將其連接到一個數組中,然后將其另存為圖像,這意味着您正在嘗試將所有內容同時保存在列表、數組和圖像中因此需要 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.
如果您安裝了libtiff
,您也可以在保存 output 文件時嘗試添加compression='lzw'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.