|
1 | 1 | # 趣味のPython学習 Project 02-12 |
2 | 2 | # Python RAW 2 TIFF CONVERTER |
3 | | -# ばーじょん 0.1.2 |
| 3 | +# ばーじょん 0.1.3 |
4 | 4 |
|
5 | | -ver = "0.1.2" |
| 5 | +ver = "0.1.3" |
6 | 6 |
|
7 | 7 | # NEED THIS ! get 'pillow' with pip command ! |
8 | 8 | from PIL import Image |
|
15 | 15 |
|
16 | 16 | # NEED THIS ! get 'rawpy' with pip command ! |
17 | 17 | import rawpy as RPY |
| 18 | +from rawpy import LibRawFileUnsupportedError |
18 | 19 | from rawpy import LibRawIOError |
19 | 20 |
|
20 | 21 | # |
|
32 | 33 |
|
33 | 34 | try : |
34 | 35 |
|
35 | | - raw = RPY.imread(fnm) |
| 36 | +# CHECK EXIST |
| 37 | + f = open(fnm,'rb') |
| 38 | + f.close() |
36 | 39 |
|
37 | | - tif = raw.postprocess(demosaic_algorithm=RPY.DemosaicAlgorithm.LINEAR,output_bps=16) |
| 40 | + raw = RPY.imread(fnm) |
38 | 41 |
|
39 | 42 | width = raw.sizes.width |
40 | 43 | height = raw.sizes.height |
| 44 | + |
41 | 45 | clr = raw.color_desc |
42 | 46 | pat = raw.raw_pattern |
43 | 47 |
|
|
48 | 52 |
|
49 | 53 | print("*** READ OK ***") |
50 | 54 |
|
51 | | - except LibRawIOError: |
| 55 | + except FileNotFoundError: |
52 | 56 | print(f"{fnm} : not found !") |
53 | 57 |
|
54 | | - except AssertionError: |
| 58 | + except LibRawIOError: |
| 59 | + print(f"{fnm} : decode error !") |
| 60 | + |
| 61 | + except LibRawFileUnsupportedError: |
55 | 62 | print(f"{fnm} : type error !") |
56 | 63 |
|
57 | 64 | else : |
58 | 65 |
|
59 | 66 | fno = fnm + ".tif" |
60 | 67 | print(f"*** SAVE : {fno} ( 48bit color ) ***") |
61 | | - imageio.imsave(fno,tif) |
62 | 68 |
|
63 | | - if type(pat) != numpy.ndarray : continue |
| 69 | + tif = raw.postprocess(demosaic_algorithm=RPY.DemosaicAlgorithm.LINEAR,output_bps=16) |
| 70 | + imageio.imsave(fno,tif) |
64 | 71 |
|
65 | | - rgb = raw.postprocess(half_size=True,four_color_rgb=True,output_color=RPY.ColorSpace.raw,output_bps=16) |
| 72 | + ptn = True |
| 73 | + if type(pat) != numpy.ndarray : |
| 74 | + ptn = False |
66 | 75 |
|
67 | 76 | fno = fnm + ".rb.png" |
68 | 77 | print(f"*** SAVE : {fno} ( 16bit bayer pattern ) ***") |
69 | 78 |
|
| 79 | + |
| 80 | + rgb = raw.postprocess(half_size=True,four_color_rgb=True,output_color=RPY.ColorSpace.raw,output_bps=16) |
70 | 81 | img_cv = Image.new('I;16',(width,height)) |
71 | 82 |
|
72 | 83 | for y in range(height) : |
73 | 84 | for x in range(width) : |
74 | 85 |
|
| 86 | +# RGGB CONVERT |
75 | 87 | if x%2 == 0 and y%2 == 0 : c = 0 |
76 | 88 | if x%2 == 1 and y%2 == 0 : c = 1 |
77 | 89 | if x%2 == 0 and y%2 == 1 : c = 3 |
78 | 90 | if x%2 == 1 and y%2 == 1 : c = 2 |
79 | 91 |
|
| 92 | + if ptn == False and c == 3 : c = 1 |
| 93 | + |
80 | 94 | img_cv.putpixel((x,y),int(rgb[y//2][x//2][c])) |
81 | 95 |
|
82 | 96 | img_cv.save(fno) |
|
0 commit comments