forked from GCaptainNemo/RAF2jpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
139 lines (120 loc) · 4.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import struct
from PIL import Image
import os
import argparse
def parse_config():
parser = argparse.ArgumentParser(description="RAF2JPEG")
parser.add_argument(
"--input", "-i", type=str, required=True, help="input directory address"
)
parser.add_argument(
"--output", "-o", type=str, required=True, help="export directory address"
)
args = parser.parse_args()
return args
class RAFHeader:
# .RAF basic information
def __init__(self, filename):
with open(filename, "rb") as f:
self.type_string = struct.unpack(">16s", f.read(16))[0]
self.format_ver = struct.unpack(">4s", f.read(4))[0]
self.camera_id = struct.unpack(">8s", f.read(8))[0]
self.camera_str = struct.unpack(">32s", f.read(32))[0].decode(
"utf_8", "ignore"
)
self.offset_ver = struct.unpack(">4s", f.read(4))[0]
self.offset_unk = struct.unpack(">20s", f.read(20))[0].decode(
"utf_8", "ignore"
)
self.offset_jpg_offset = struct.unpack(">1i", f.read(4))[0]
self.offset_jpg_length = struct.unpack(">1i", f.read(4))[0]
self.offset_CFA_header_offset = struct.unpack(">1i", f.read(4))[0]
self.offset_CFA_header_length = struct.unpack(">1i", f.read(4))[0]
self.offset_CFA_offset = struct.unpack(">1i", f.read(4))[0]
self.offset_CFA_length = struct.unpack(">1i", f.read(4))[0]
class JPEG:
# Exif JFIF with thumbnail + preview
def __init__(self, filename, offset, length):
with open(filename, "rb") as f:
f.seek(offset)
self.bin = f.read(length)
def exif(self):
pass
# todo
class CFA:
def __init__(
self, filename, header_offset, header_length, data_offset, data_length
):
self.filename = filename
self.data_offset = data_offset
self.data_length = data_length
self.records = []
with open(filename, "rb") as f:
f.seek(header_offset)
self.count = struct.unpack(">1i", f.read(4))[0]
for record in range(self.count):
tag = struct.unpack(">1H", f.read(2))[0]
size = struct.unpack(">1H", f.read(2))[0]
self.records.append({"id": tag, "size": size, "data": f.read(size)})
pass
def unpack(self):
with open(self.filename, "rb") as f:
f.seek(self.data_offset)
# todo
class RAF:
def __init__(self, filename):
self.filename = filename
self.header = RAFHeader(filename)
self.jpg = JPEG(
filename, self.header.offset_jpg_offset, self.header.offset_jpg_length
)
self.CFA = CFA(
filename,
self.header.offset_CFA_header_offset,
self.header.offset_CFA_header_length,
self.header.offset_CFA_offset,
self.header.offset_CFA_length,
)
def __export_exif(self, path):
jpg_bin = self.jpg.bin
# todo
def __export_jpg(self, path):
with open(path, "wb") as f:
f.write(self.jpg.bin)
def __export_dng(self, path):
self.CFA.unpack()
# todo
def export(self, path, suffix):
eval("self._RAF__export_" + suffix.lower() + "('" + path + "." + suffix + "')")
if __name__ == "__main__":
args = parse_config()
export_path = args.output
import_path = args.input
print(f"输入文件夹是:{export_path}\n输出文件夹是:{import_path}\n")
print(f"")
inflag = input("请输入 'y' 继续执行,默认是 'y' ,否则退出:") or "y"
if inflag.upper() == "Y":
if not os.path.exists(export_path):
os.mkdir(export_path)
file_lst = os.listdir(import_path)
res_string = []
for i, name in enumerate(file_lst):
if name.split(".")[-1] == "RAF":
try:
file_path = os.path.join(import_path, name)
obj = RAF(file_path)
export_file_path = os.path.join(export_path, name.split(".")[0])
obj.export(export_file_path, "jpg")
print("success:", file_path)
res_string.append("success:" + file_path + "\n")
except Exception as e:
print("fail:", file_path)
res_string.append("fail:" + file_path + "\n")
print(e)
log_path = os.path.join(export_path, "res.log")
with open(log_path, "w+") as f:
print(f"日志文件在:{log_path}")
for res in res_string:
f.write(res)
else:
print("退出程序...")