本来希望不用求助,但是解决不了。简而言之,我有一个带有标题、RGB 值和光谱值的二进制文件。我只需要压缩光谱值(使用 imagecodecs 中的 Jpegxl)。在大多数情况下,此算法有效并为我提供 1.5 的压缩比,但在某些情况下,尤其是当输入文件很大时,我会遇到以下异常:
文件“imagecodecs_jpegxl.pyx”,第 669 行,在 imagecodecs._jpegxl.jpegxl_decode 中 RuntimeError:无法确定帧数
我附上了代码的简化版本和算法运行良好的小文件。不幸的是,我无法上传生成错误的文件,因为它们有几 GB 的重量,但文件保留了相同的结构。
import struct
import numpy as np
import imagecodecs
import os
import io
import time
import mmap
import filecmp
from imagecodecs import jpegxl_encode, jpegxl_decode
file_ips="Corallo"
#Indirizzo da dove iniziano i dati (dopo header)
IND_START = 285046 # Corallo
input_file = f"{file_ips}.ips"
output_file = f"{file_ips}_new.ips"
print("Apro file")
start_process = time.time()
with open(input_file, "rb") as file:
# otttengo la dimensione del file
file_size = os.path.getsize(input_file)
# mappo il file in memoria
with mmap.mmap(file.fileno(), length=file_size, access=mmap.ACCESS_READ) as mm:
#ottengo altezza e larghezza dall'header
width, height = struct.unpack_from("<2h", mm, 4)
n_values = height * width * 31 * 2
rgb_values = height * width * 3
#i valori spettrali da comprimere iniziano dopo i valori rgb
IND_START = IND_START + rgb_values
print(n_values)
#creo la matrice dai valori spettrali
spectr_matrix = np.frombuffer(mm[IND_START:IND_START + n_values], dtype=np.uint16).reshape(-1,31)
#comprimo
compress_data = jpegxl_encode(spectr_matrix)
#decomprimo
decompress_data = jpegxl_decode(compress_data)
uno = spectr_matrix
due = np.array(decompress_data)
if np.array_equal(uno, due):
print("Gli array sono uguali")
else:
print("Gli array sono diversi")
我该如何解决这个问题?或者可能是什么问题?我注意到,如果我在某些情况下更改矩阵的形状或“n_values”,则不会发生异常。
rgb_values、n_values 和 IND_START 都是已经检查过的正确值。
提前致谢