Python程序在保存文本文件时崩溃

2024-04-24 23:35:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含853个图像的大型图像堆栈,每个图像都有尺寸(1177915394)像素和3个通道。图像的深度为8位,并以BigTiff格式保存。我的数据存储在连接到计算机的外部SSD上。 我想做的是根据初始值改变一些像素的值。因为我无法将整个堆栈加载到内存中,所以我使用tiffile.memmap来内存映射我要调查和更改的像素

下面是我的代码的一个简单示例:

import numpy as np
from tifffile import memmap

memmap_img_HSV = memmap('img_HSV.tif', mode='r')    #original image stack I want to investigate
memmap_img_result = memmap('img_result.tif', mode='r+')    #another image which I want to overwrite with the results

img_hue = memmap_img_HSV[:,:,:,0]    #first channels of original image

memmap_img_result[:,:,:] = 0    #set all pixels = 0, before I save the results into it

fill_value = 11    #define the variable "fill_value" which will be used later
sigma_list = []    #create a list where I later save coordinates in

array_label = memmap_img_result[z1:z2, y1:y2, x1:x2]    #only memmap indicated pixels
array_hue = img_hue[z1:z2, y1:y2, x1:x2]

count = array_label == fill_value                       #these are the pixels which have already been investigated and replaced by the "fill_value"

orig_value_hue = np.median(array_hue[count])    #determine the median value of the pixel values from "count"

org = memmap_img_result[zv1:zv2, yv1:yv2, xv1:xv2]    #original values of the investigated pixels

if (orig_value_hue+1) < 10:    #condition, which pixels should be replaced with "fill_value": the rest stays how it was before
    res = np.where((memmap_img_HSV[zv1:zv2, yv1:yv2, xv1:xv2] == 0), fill_value, org)

if (orig_value_hue+1) < 10:    #another condition; if this is True, write the coordinates into list 
    res_sigma = np.argwhere(memmap_img_HSV[zv1:zv2, yv1:yv2, xv1:xv2] == 1)
    if len(res_sigma)>0:
        sigma_list.append([zv1,yv1,xv1])
        sigma_list = np.asarray(sigma_list[1])

        if len(sigma_list[sigma+1])>0:
            np.savetxt('list.txt', np.column_stack([sigma_list[1]]))

memmap_img_result.flush()
del memmap_img_HSV
del memmap_img_result

如果代码看起来有点混乱,我道歉;这只是对复杂得多的代码的简化。但我希望,目标会变得清晰

现在我的问题是:当我运行代码时,有时代码会崩溃。在将文件'list.txt'写入磁盘时,我要么收到错误消息“设备上没有剩余空间”,尽管磁盘上有大量可用空间(足以保存列表)。或者我收到错误消息“无效参数:'list.txt'”。此外,SSD在错误消息后显示0 kB大小。我必须先修复磁盘并重新启动,然后才能再次使用磁盘。 我认为这不是memmap的问题,而是将列表保存为文本文件的问题

我已经可以在没有任何错误消息的情况下使用其他(较小的)数据运行代码,但它会在使用这些数据时崩溃。有没有人知道是什么原因导致了这次车祸,或者我可以试试什么


Tags: the代码图像imgifvaluenpresult