Last active
February 27, 2017 18:06
-
-
Save AlekseyDurachenko/5f831d6051dad70a0356685a0e815f17 to your computer and use it in GitHub Desktop.
Fix levels inside darktable xmp files (migration from version 2.0.7 to 2.2.3)
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/python3 | |
import struct | |
import binascii | |
import sys | |
if len(sys.argv) != 2: | |
print("Usage:") | |
print(" darktable_xmp_migrate_from_2_0_X_to_2_2_3.py filename") | |
print("") | |
print("Please note the file will be rewritten. Be sure you created a backup") | |
sys.exit(1) | |
out = [] | |
stage = 0 | |
stage_lines = [ | |
"<rdf:li", | |
"darktable:operation=\"levels\"", | |
"darktable:enabled", | |
"darktable:modversion", | |
"darktable:params" | |
] | |
fin = open(sys.argv[1]) | |
while True: | |
line = fin.readline() | |
if line == "": | |
break | |
linestrip = line.strip() | |
lineseg = linestrip.split("=") | |
if stage == 0 and linestrip == stage_lines[0]: | |
stage = 1 | |
out.append(line) | |
continue | |
if stage == 1 and linestrip == stage_lines[1]: | |
stage = 2 | |
out.append(line) | |
continue | |
if stage == 2 and lineseg[0] == stage_lines[2]: | |
stage = 3 | |
out.append(line) | |
continue | |
if stage == 3 and lineseg[0] == stage_lines[3]: | |
stage = 4 | |
out.append(line) | |
continue | |
if stage == 4 and lineseg[0] == stage_lines[4]: | |
stage = 0 | |
paramhex_pre = lineseg[1][1:33] | |
paramhex = lineseg[1][-25:-1] | |
infloathex1 = paramhex[0:8] | |
infloathex2 = paramhex[8:16] | |
infloathex3 = paramhex[16:24] | |
infloat1 = struct.unpack('<f', bytes.fromhex(infloathex1))[0] | |
infloat2 = struct.unpack('<f', bytes.fromhex(infloathex2))[0] | |
infloat3 = struct.unpack('<f', bytes.fromhex(infloathex3))[0] | |
outfloat1 = 1.0 - infloat3 | |
outfloat2 = 1.0 - infloat2 | |
outfloat3 = 1.0 - infloat1 | |
outfloathex1 = hex(struct.unpack('>I', struct.pack('<f', outfloat1))[0])[2:].rjust(8, '0') | |
outfloathex2 = hex(struct.unpack('>I', struct.pack('<f', outfloat2))[0])[2:].rjust(8, '0') | |
outfloathex3 = hex(struct.unpack('>I', struct.pack('<f', outfloat3))[0])[2:].rjust(8, '0') | |
out.append(" %s=\"%s%s%s%s\"\n" % (lineseg[0], paramhex_pre, outfloathex1, outfloathex2, outfloathex3,)) | |
continue | |
stage = 0 | |
out.append(line) | |
fin.close() | |
fout = open(sys.argv[1], "w") | |
for line in out: | |
fout.write(line) | |
fout.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment