-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw2tiff.py
100 lines (66 loc) · 2.24 KB
/
raw2tiff.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
# 趣味のPython学習 Project 02-12
# Python RAW 2 TIFF CONVERTER
# ばーじょん 0.1.3
ver = "0.1.3"
# NEED THIS ! get 'pillow' with pip command !
from PIL import Image
# NEED THIS ! get 'numpy' with pip command !
import numpy
# NEED THIS ! get 'imageio' with pip command !
import imageio
# NEED THIS ! get 'rawpy' with pip command !
import rawpy as RPY
from rawpy import LibRawFileUnsupportedError
from rawpy import LibRawIOError
#
# IMPORTANT !!!
# rawpy needs LibRaw Libraly !
# https://www.libraw.org/
#
# MAIN
print(f"*** RAW FILE to TIFF CONVERTER VERSION {ver} ***")
print("\nSELECT RAW FILE\n")
while len( fnm := input("file : ") ) > 0 :
try :
# CHECK EXIST
f = open(fnm,'rb')
f.close()
raw = RPY.imread(fnm)
width = raw.sizes.width
height = raw.sizes.height
clr = raw.color_desc
pat = raw.raw_pattern
print(f"W : {width} H : {height}")
print("PATTERN:",clr)
print(pat)
print("*** READ OK ***")
except FileNotFoundError:
print(f"{fnm} : not found !")
except LibRawIOError:
print(f"{fnm} : decode error !")
except LibRawFileUnsupportedError:
print(f"{fnm} : type error !")
else :
fno = fnm + ".tif"
print(f"*** SAVE : {fno} ( 48bit color ) ***")
tif = raw.postprocess(demosaic_algorithm=RPY.DemosaicAlgorithm.LINEAR,output_bps=16)
imageio.imsave(fno,tif)
ptn = True
if type(pat) != numpy.ndarray :
ptn = False
fno = fnm + ".rb.png"
print(f"*** SAVE : {fno} ( 16bit bayer pattern ) ***")
rgb = raw.postprocess(half_size=True,four_color_rgb=True,output_color=RPY.ColorSpace.raw,output_bps=16)
img_cv = Image.new('I;16',(width,height))
for y in range(height) :
for x in range(width) :
# RGGB CONVERT
if x%2 == 0 and y%2 == 0 : c = 0
if x%2 == 1 and y%2 == 0 : c = 1
if x%2 == 0 and y%2 == 1 : c = 3
if x%2 == 1 and y%2 == 1 : c = 2
if ptn == False and c == 3 : c = 1
img_cv.putpixel((x,y),int(rgb[y//2][x//2][c]))
img_cv.save(fno)
print("*** DONE ! ***")
# END OF FILE