-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextract_raw_image.py
61 lines (52 loc) · 2.27 KB
/
extract_raw_image.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
from __future__ import print_function
#import microscope
#import picamera
import numpy as np
#import matplotlib.pyplot as plt
import time
import picamera_array
import cv2
import PIL.Image
import PIL.ExifTags
from dump_exif import exif_data_as_string
import sys
full_resolution=(3280,2464)
class DummyCam(object):
resolution = full_resolution
revision = 'IMX219'
sensor_mode = 0
def load_raw_image(filename, ArrayType=picamera_array.PiSharpBayerArray, open_jpeg=False):
with open(filename, mode="rb") as file:
jpeg = file.read()
cam = DummyCam()
bayer_array = ArrayType(cam)
bayer_array.write(jpeg)
bayer_array.flush()
if open_jpeg:
jpeg = PIL.Image.open(filename)
# with thanks to https://stackoverflow.com/questions/4764932/in-python-how-do-i-read-the-exif-data-for-an-image
exif_data = jpeg._getexif()
return bayer_array, jpeg, exif_data
return bayer_array
def extract_file(filename):
"""Extract metadata and raw image from a file"""
print("converting {}...".format(filename))
bayer_array, jpeg, exif_data = load_raw_image(filename, open_jpeg=True)
# extract EXIF metadata from the image
root_fname, junk = filename.rsplit(".j", 2) #get rid of the .jpeg extension
with open(root_fname + "_exif.txt", "w") as f:
f.write(exif_data_as_string(jpeg))
# extract raw bayer data
cv2.imwrite(root_fname + "_raw16.tif", bayer_array.demosaic()*64)
cv2.imwrite(root_fname + "_raw8.png", (bayer_array.demosaic()//4).astype(np.uint8))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} <filename.jpg> ...".format(sys.argv[0]))
print("Specify one or more filenames corresponding to Raspberry Pi JPEGs including raw Bayer data.")
print("Each file will be processed, to produce three new files:")
print("<filename>_raw16.tif will contain the full raw data as a 16-bit TIFF file (the lower 6 bits are empty).")
print("<filename>_raw8.png will contain the top 8 bits of the raw data, in an easier-to-handle file.")
print("<filename>_exif.txt will contain the EXIF metadata extracted as a text file - this includes analogue gain.")
sys.exit(0)
for filename in sys.argv[1:]:
extract_file(filename)