Created
June 20, 2017 03:54
-
-
Save Frencil/63fccc3ae7a3c2d34652e1cb739b3933 to your computer and use it in GitHub Desktop.
Drop-in python script to linearly tween numeric values in processing profiles across a set of arbitrarily many RAW images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import string, os | |
START_FILE_PATH = "start.pp3" | |
END_FILE_PATH = "end.pp3" | |
RAW_FILES_DIR = "raw" | |
# Load the contents of the start and end PP3 files (equal line numbers enforced!) | |
with open(START_FILE_PATH, "r") as start_file: | |
start_lines = string.split(start_file.read(), "\n") | |
with open(END_FILE_PATH, "r") as end_file: | |
end_lines = string.split(end_file.read(), "\n") | |
if len(start_lines) != len(end_lines): | |
print "Sorry, I only work on start/end PP3s with the same number of lines!" | |
exit | |
# Create an ordered list of all CR2 files in the raw directory | |
raw_files = [] | |
for f in os.listdir(RAW_FILES_DIR): | |
if os.path.isfile(os.path.join(RAW_FILES_DIR, f)) and f.endswith("CR2"): | |
raw_files.append(f) | |
raw_files.sort() | |
if len(raw_files) < 2: | |
print "At least two RAW files required, " + str(len(raw_files)) + " found!" | |
exit | |
# Parse the values in the start and end PP3s to build a data structure | |
# representing both extremes at once. Each section is preserved separately | |
# as parameters may appear under the same name in different sections. | |
# All values are represented as strings unless they are tweenable, and then | |
# they will be tuple of exactly two ints or two floats. | |
params = {} | |
def int_or_float(val): | |
try: | |
return int(val) | |
except ValueError: | |
return float(val) | |
section = None | |
for line in start_lines: | |
if line.startswith("["): | |
section = line.replace("[","").replace("]","") | |
params[section] = {} | |
continue | |
if "=" not in line: | |
continue | |
param, raw_value = string.split(line, "=", 2) | |
try: | |
num_value = int_or_float(raw_value) | |
except ValueError: | |
params[section][param] = raw_value | |
continue | |
params[section][param] = (num_value, None) | |
print "Tweens:" | |
section = None | |
for line in end_lines: | |
if line.startswith("["): | |
section = line.replace("[","").replace("]","") | |
continue | |
if "=" not in line: | |
continue | |
param, raw_value = string.split(line, "=", 2) | |
try: | |
num_value = int_or_float(raw_value) | |
except ValueError: | |
continue; | |
if (type(params[section][param][0]) is type(num_value) and params[section][param][0] != num_value): | |
params[section][param] = (params[section][param][0], num_value) | |
print " * [" + section + "] " + param + ": " + str(params[section][param]) + " <" + ("int" if type(num_value) is int else "float") + ">" | |
else: | |
params[section][param] = raw_value | |
# Walk the raw file list generating a new PP3 file for each one | |
# Linearly tween all tweenable values | |
for idx, raw_file in enumerate(raw_files): | |
percentage = float(idx) / float(len(raw_files) - 1) | |
tweened_pp3 = "" | |
for section in params: | |
if (len(tweened_pp3) > 0): | |
tweened_pp3 += "\n" | |
tweened_pp3 += "[" + section + "]\n" | |
for param in params[section]: | |
pp3_val = params[section][param] | |
if type(params[section][param]) is tuple: | |
start, end = params[section][param] | |
if type(start) is int: | |
pp3_val = str(start + int((end - start) * percentage)) | |
else: | |
pp3_val = str(start + (end - start) * percentage) | |
tweened_pp3 += param + "=" + pp3_val + "\n" | |
pp3_file = open(os.path.join(RAW_FILES_DIR, raw_file + ".pp3"), "w") | |
pp3_file.write(tweened_pp3) | |
pp3_file.close() | |
print raw_file + ".pp3" | |
print "DONE.\nGenerated " + str(len(raw_files)) + " tweened PP3 files." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment