Skip to content

Instantly share code, notes, and snippets.

@jnsdbr
Last active September 7, 2022 09:25
Show Gist options
  • Save jnsdbr/d5d8d0fcc897e00fb9ac18f6e608e2a6 to your computer and use it in GitHub Desktop.
Save jnsdbr/d5d8d0fcc897e00fb9ac18f6e608e2a6 to your computer and use it in GitHub Desktop.
Convert dream_web_log.txt generated by https://github.com/lstein/stable-diffusion webinterface to json
import glob
import json
import os
output_path = './outputs/img-samples/'
log_file_name = 'dream_web_log.txt'
full_log_path = output_path + log_file_name
image_list = glob.glob(output_path + '*.png')
def convert_log_to_json(log_file_path):
prompts = []
with open(log_file_path) as f:
lines = f.readlines()
for line in lines:
# Extract prompt
prompt_split = line.partition(':')[2].strip()
prompt_json = json.loads(prompt_split)
# Extract filepath
file_path_split = line.split(':')
file_path = file_path_split[0]
prompt_json['img_path'] = file_path
# Extract filename
file_name_split = file_path_split[0].split('/')
prompt_json['file_name'] = file_name_split[len(file_name_split) - 1]
# Extract seed
seed_split = file_name_split[len(file_name_split) - 1].split('.')
prompt_json['real_seed'] = seed_split[1]
if file_path in image_list:
# Add created timestamp
timestamp = str(os.path.getctime(file_path)).split('.')[0]
prompt_json['timestamp'] = timestamp
prompts.append(prompt_json)
return prompts
if __name__ == '__main__':
with open(full_log_path[:-4] + '.json', 'w') as json_file:
json.dump(convert_log_to_json(full_log_path), json_file, ensure_ascii=False, indent=4)
@jnsdbr
Copy link
Author

jnsdbr commented Sep 7, 2022

Also removes prompts for deleted images!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment