-
Notifications
You must be signed in to change notification settings - Fork 2
/
raw-to-jpg.py
35 lines (29 loc) · 952 Bytes
/
raw-to-jpg.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
import sys, os, argparse
from rawkit.raw import Raw
import numpy as np
from PIL import Image
def raw_to_jpg(file):
try:
with Raw(file) as raw_image:
buffered_image = np.array(raw_image.to_buffer())
im = Image.frombuffer('RGB', (raw_image.metadata.width, raw_image.metadata.height), buffered_image, 'raw', 'RGB', 0, 1)
im.save(os.path.split(file)[1] + '.jpg')
print('Successfully saved file as JPG.')
except OSError:
print('Invalid file or location. Check your data. File: %s' % file)
def check_if_raw(file):
filename = file
try:
filename_ext = filename.split('.')
if filename_ext[1] == 'CR2':
print('PASS: \'%s\' is CR2 file.' % filename)
raw_to_jpg(filename)
else:
print('FAIL: \'%s\' is \'%s\', not CR2.' % (filename, filename_ext[1]))
except IndexError:
print('FAIL: \'%s\' is not a valid file.' % filename)
photo = sys.argv[1]
if os.path.exists(photo):
raw_to_jpg(photo)
else:
print("Not a valid file")