Extract metadata from Canon CR2 and Nikon NEF raw files
#!/usr/bin/env python | |
import sys | |
import os | |
from rawphoto.cr2 import Cr2 | |
from rawphoto.nef import Nef | |
def print_entries(raw, ifd, level=1): | |
for name in ifd.entries: | |
e = ifd.entries[name] | |
if name in ifd.subifds or isinstance(name, tuple): | |
if isinstance(name, tuple): | |
for n in name: | |
print(level * "\t" + n + ":") | |
print_entries(raw, ifd.subifds[n], level + 1) | |
else: | |
print(level * "\t" + name + ":") | |
print_entries(raw, ifd.subifds[name], level + 1) | |
else: | |
if isinstance(name, str): | |
if e.tag_type_key is 0x07: | |
print(level * "\t" + "{}: {}".format( | |
name, | |
"[Binary blob]" | |
)) | |
else: | |
print(level * "\t" + "{}: {}".format( | |
name, | |
ifd.get_value(e) | |
)) | |
metadata = {} | |
filepath = sys.argv[1] | |
(filepath_no_ext, ext) = os.path.splitext(filepath) | |
filename_no_ext = os.path.basename(filepath_no_ext) | |
ext = ext.upper() | |
if ext == '.CR2': | |
raw = Cr2(filename=filepath) | |
elif ext == '.NEF': | |
raw = Nef(filename=filepath) | |
else: | |
raise TypeError("Format not supported") | |
for i in range(len(raw.ifds)): | |
ifd = raw.ifds[i] | |
print("IFD #{}".format(i)) | |
print_entries(raw, ifd) | |
# Hax. | |
for subifd in ifd.subifds: | |
if isinstance(subifd, int): | |
print("Subifd ", subifd) | |
print_entries(raw, ifd.subifds[subifd], 1) | |
with open(filename_no_ext + "_thumb.jpg", "wb") as outfile: | |
try: | |
outfile.write(raw.thumbnail_image) | |
print("Wrote {}_thumb.jpg to disk...".format(filename_no_ext)) | |
except: | |
pass | |
with open(filename_no_ext + "_preview.jpg", "wb") as outfile: | |
try: | |
outfile.write(raw.preview_image) | |
print("Wrote {}_preview.jpg to disk...".format(filename_no_ext)) | |
except: | |
pass | |
with open(filename_no_ext + "_uncompressed.jpg", "wb") as outfile: | |
try: | |
outfile.write(raw.uncompressed_full_size_image) | |
print("Wrote {}_uncompressed.jpg to disk...".format(filename_no_ext)) | |
except: | |
pass | |
with open(filename_no_ext + "_raw.data", "wb") as outfile: | |
outfile.write(raw.raw_data) | |
print("Wrote {}_raw.data to disk...".format(filename_no_ext)) | |
raw.close() |
This comment has been minimized.
This comment has been minimized.
Sorry, I haven't looked at this in years and we no longer maintain rawkit, so I'm afraid it's not something I can help with. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Hi ! I try to get the exposure time of my pictures with your code but I got 1004 instead of 90.2 s. Any Idea about where this difference come from ?
Cheers