Python 程序卡住或退出,没有警告或错误(有时会引发段错误)

问题描述 投票:0回答:1

代码:

import subprocess
from PIL import Image
import colorsys
import numpy as np

for t in range(1,49):
    in_file_name=f"Pf{t:02d}.bin"
    subprocess.run(f"sz3 -f -i /home/jzz/compress/SDRBENCH-Hurricane-ISABEL-100x500x500/P/{in_file_name} -z /home/jzz/simulation/dataset/sz3/temp.sz3 -o /home/jzz/simulation/dataset/sz3/temp.sz3.bin -M REL -R 0.1 -3 500 500 100",shell=True,check=True)
    in_data=np.fromfile(f"/home/jzz/compress/SDRBENCH-Hurricane-ISABEL-100x500x500/P/{in_file_name}",dtype=np.float32).reshape(100,500,500)
    out_data=np.fromfile(f"/home/jzz/simulation/dataset/sz3/temp.sz3.bin",dtype=np.float32).reshape(100,500,500)
    for h in range(100):
        print(h)
        mx=max(in_data[h,:,:].max(),out_data[h,:,:].max())
        mn=min(in_data[h,:,:].min(),out_data[h,:,:].min())
        data01=(in_data[h,:,:]-mn)/(mx-mn)
        image=(np.array([list(colorsys.hsv_to_rgb(data01[i,j],1,1)) for i in range(500) for j in range(500)]).reshape(500,500,3)*255).astype(np.uint8)
        image=Image.fromarray(image)
        image.save(f"/home/jzz/simulation/dataset/sz3/ISABEL_P_t={t}_h={h}_x.png")
        data01=(out_data[h,:,:]-mn)/(mx-mn)
        image=(np.array([list(colorsys.hsv_to_rgb(data01[i,j],1,1)) for i in range(500) for j in range(500)]).reshape(500,500,3)*255).astype(np.uint8)
        image=Image.fromarray(image)
        image.save(f"/home/jzz/simulation/dataset/sz3/ISABEL_P_t={t}_h={h}_y.png")
    subprocess.run(f"rm /home/jzz/simulation/dataset/sz3/temp.sz3",shell=True)
    subprocess.run(f"rm /home/jzz/simulation/dataset/sz3/temp.sz3.bin",shell=True)

当我在终端中使用“python my_program.py”时,它开始生成一些图像(这是正确的),然后程序退出或整个终端卡住,没有警告或错误。有时会出现段错误。

有足够的内存和硬盘空间。

注释掉 image.save 没有帮助。

注释掉 image.save 和 image=Image.fromarray 没有帮助。

注释掉 image.save、image=Image.fromarray 和 image=(np.array... 使程序运行直到完成。

python warnings exit segment fault
1个回答
0
投票

in_data[h,:,:].max()
改为
np.max(in_data[h,:,:])

即可解决问题

注意:我不太确定为什么这可以成为一个解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.