|
| 1 | +# 趣味のPython学習 Project 02-12 |
| 2 | +# Python RAW 2 TIFF CONVERTER |
| 3 | +# ばーじょん 0.1.0 |
| 4 | + |
| 5 | +ver = "0.1.0" |
| 6 | + |
| 7 | + |
| 8 | +# NEED THIS ! get 'pillow' with pip command ! |
| 9 | +from PIL import Image |
| 10 | + |
| 11 | +# NEED THIS ! get 'numpy' with pip command ! |
| 12 | +import numpy |
| 13 | + |
| 14 | +# NEED THIS ! get 'imageio' with pip command ! |
| 15 | +import imageio |
| 16 | + |
| 17 | +# NEED THIS ! get 'rawpy' with pip command ! |
| 18 | +import rawpy as RPY |
| 19 | +from rawpy import LibRawIOError |
| 20 | + |
| 21 | +# |
| 22 | +# IMPORTANT !!! |
| 23 | +# rawpy needs LibRaw Libraly ! |
| 24 | +# https://www.libraw.org/ |
| 25 | +# |
| 26 | + |
| 27 | +# MAIN |
| 28 | + |
| 29 | +print(f"*** RAW FILE to TIFF CONVERTER VERSION {ver} ***") |
| 30 | +print("\nSELECT RAW FILE\n") |
| 31 | + |
| 32 | +while len( fnm := input("file : ") ) > 0 : |
| 33 | + |
| 34 | + try : |
| 35 | + |
| 36 | + raw = RPY.imread(fnm) |
| 37 | + |
| 38 | + width = raw.sizes.width |
| 39 | + height = raw.sizes.height |
| 40 | + clr = raw.color_desc |
| 41 | + pat = raw.raw_pattern |
| 42 | + |
| 43 | + print(f"W : {width} H : {height}") |
| 44 | + |
| 45 | + print("PATTERN:",clr) |
| 46 | + print(pat) |
| 47 | + |
| 48 | + rgb = raw.postprocess(demosaic_algorithm=RPY.DemosaicAlgorithm.LINEAR,four_color_rgb=True,output_color=RPY.ColorSpace.raw,output_bps=16) |
| 49 | + |
| 50 | + print("*** READ OK ***") |
| 51 | + |
| 52 | + except LibRawIOError: |
| 53 | + print(f"{fnm} : not found !") |
| 54 | + |
| 55 | + except AssertionError: |
| 56 | + print(f"{fnm} : type error !") |
| 57 | + |
| 58 | + else : |
| 59 | + |
| 60 | + fno = fnm + ".tif" |
| 61 | + print(f"*** SAVE : {fno} ( 48bit color ) ***") |
| 62 | + imageio.imsave(fno,rgb) |
| 63 | + |
| 64 | + if type(pat) != numpy.ndarray : continue |
| 65 | + |
| 66 | + fno = fnm + ".reb.png" |
| 67 | + print(f"*** SAVE : {fno} ( 16bit bayer pattern ) ***") |
| 68 | + |
| 69 | + img_cv = Image.new('I;16',(width,height)) |
| 70 | + |
| 71 | + for y in range(height) : |
| 72 | + for x in range(width) : |
| 73 | + |
| 74 | + if x%2 == 0 and y%2 == 0 : c = pat[0][0] |
| 75 | + if x%2 == 1 and y%2 == 0 : c = pat[0][1] |
| 76 | + if x%2 == 0 and y%2 == 1 : c = pat[1][0] |
| 77 | + if x%2 == 1 and y%2 == 1 : c = pat[1][1] |
| 78 | + if c == 3 : c = 1 |
| 79 | + |
| 80 | + img_cv.putpixel((x,y),int(rgb[y][x][c])) |
| 81 | + |
| 82 | + img_cv.save(fno) |
| 83 | + |
| 84 | + print("*** DONE ! ***") |
| 85 | + |
| 86 | +# END OF FILE |
0 commit comments