Skip to content

Instantly share code, notes, and snippets.

@takapiro99
Last active November 5, 2021 06:25
Show Gist options
  • Save takapiro99/1fb8202e8a5b6f76042fdeb7194cf51e to your computer and use it in GitHub Desktop.
Save takapiro99/1fb8202e8a5b6f76042fdeb7194cf51e to your computer and use it in GitHub Desktop.
import os
import struct
# フォルダ内の全ての .SPE ファイルを /out フォルダに .txt として出力します
# 対象フォルダ内(一つの階層だけに対応しています)にこの python ファイルを置き、実行してください
def from_bytes(b, fmt, offset):
return struct.unpack(fmt, b[offset:offset+struct.calcsize(fmt)])[0]
def read_spe(b):
datatype = from_bytes(b, "h", 108)
frame_width = from_bytes(b, "H", 42)
frame_height = from_bytes(b, "H", 656)
num_frames = from_bytes(b, "i", 1446)
to_np_type = [np.float32, np.int32, np.int16, np.uint16,
None, np.float64, np.uint8, None, np.uint32]
np_type = to_np_type[datatype]
itemsize = np.dtype(np_type).itemsize
count = frame_width * frame_height
all_data = []
for i in range(0, num_frames):
data = np.frombuffer(b, dtype=np_type, count=count,
offset=4100 + i*count*itemsize)
image = np.reshape(data, (frame_height, frame_width))
all_data.append(image)
return all_data
def main():
if not os.path.exists('out'):
os.makedirs("out")
files = os.listdir()
print("processing...")
for FILEPATH in files:
data = None
if os.path.splitext(FILEPATH)[1] != ".SPE":
print(f"{FILEPATH} was not .SPE file")
continue
with open(FILEPATH, mode="rb") as input_file:
data = read_spe(input_file.read())[0]
if len(data[0]) != 1024 or len(data) != 1024:
print(f"this SPE file is not 1024*1024 but was {len(data)} * {len(data[0])}")
with open("out/" + os.path.splitext(FILEPATH)[0] + '.txt', mode="w") as f:
content = "\n".join(
list(map(lambda x: "\n".join(list(map(lambda i: f"{i[0]+1},{x[0]+1},{i[1]}", enumerate(x[1])))), enumerate(data))))
f.write(content + "\n")
print(f"[INFO] saved as {os.path.splitext(FILEPATH)[0] + '.txt'}")
print(f"done: converted {len(os.listdir('out'))} files.")
input("press enter to exit")
try:
import numpy as np
except:
print('please install numpy first with this command "$ pip install numpy"')
input("press enter to exit")
exit()
try:
main()
except :
print('sorry, something went wrong')
import traceback
traceback.print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment