Skip to content

Instantly share code, notes, and snippets.

@arisada
Created June 20, 2022 10:21
Show Gist options
  • Save arisada/844421135f5c3e4d0a533ce67e7a3bc5 to your computer and use it in GitHub Desktop.
Save arisada/844421135f5c3e4d0a533ce67e7a3bc5 to your computer and use it in GitHub Desktop.
Sort Sony ARW files for astrophotography
#!/usr/bin/env python3
"""
Automatically move Sony ARW raw files into directories based on the guessed
content (brut/flat/offset/dark/darkflat).
Usage: ./sortpics.py *.ARW
"""
import sys
import os
import shutil
import numpy as np
import tempfile
import PIL.Image
from exifread import process_file
def main(args):
for filename in args[1:]:
with open(filename, 'rb') as f:
tags = process_file(f)
if not tags:
print("No exif in", filename)
continue
keys = tags.keys()
for i in keys:
#print("%s : %s"%(i, tags[i]))
pass
iso = str(tags["EXIF ISOSpeedRatings"])
exposure = str(tags["EXIF ExposureTime"])
jpeg = tags["JPEGThumbnail"]
avg = -1
with tempfile.TemporaryFile() as temp:
temp.write(jpeg)
temp.seek(0)
image = PIL.Image.open(temp)
image_seq = image.getdata()
image_array = np.array(image_seq)
avg = np.average(image_array)
print(exposure)
if '/' in exposure:
a, b = exposure.split('/')
f_exposure = float(a)/float(b)
else:
f_exposure = float(exposure)
if avg > 15 and f_exposure > 1:
img_type = "bruts"
elif avg > 15 and f_exposure < 1:
img_type = "flats"
elif avg < 3 and f_exposure < 1e-3:
img_type = "offsets"
elif avg < 3 and f_exposure > 1:
img_type = "darks"
elif avg < 3 and f_exposure < 1:
img_type = "darkflats"
else:
img_type = "inconnu"
print ("file:", filename, "iso:", iso, "exposure:", exposure, "type:", img_type, "average: ", avg)
if img_type != "inconnu":
path_root = os.path.dirname(os.path.realpath(filename))
path_dirname = img_type + "_ISO" + iso + "_" + exposure.replace("/", "_") + "s"
path_filename = os.path.basename(filename)
#print(path_root, path_dirname, path_filename)
try:
os.mkdir('/'.join((path_root, path_dirname)))
except FileExistsError:
pass
dest = '/'.join((path_root, path_dirname, path_filename))
print("move %s to %s"%(filename, dest))
shutil.move(filename, dest)
if __name__== "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment