微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决保存文本文件时 Python 程序崩溃

我有一个包含 853 个图像的大图像堆栈,每个图像的尺寸为 (11779,15394) 像素和 3 个通道。图像具有 8 位深度并以 BigTiff 格式保存。我的数据存储在我连接到计算机的外部 SSD 上。 我想要做的是根据初始值更改某些像素的值。由于我无法将完整堆栈加载到内存中,因此我使用 tifffile.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,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,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,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 的问题,但不知何故将列表保存为文本文件

我已经可以使用其他(较小的)数据运行代码而没有任何错误消息,但它会因这些数据而崩溃。有没有人知道什么可能导致崩溃或我可以尝试什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。