Skip to content

Instantly share code, notes, and snippets.

Last active April 21, 2020 12:08
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Import Annotated Images Into Tropy
#!/usr/bin/env ruby
require 'json'
input = ARGV[0]
items = []
def create_item(title)
'template' => '',
'' => title,
'photo' => [{
'template' => '',
'mimetype' => 'unknown',
'checksum' => 'unknown',
'' => title,
'note' => [{
'html' => {
end input do |f|
item = nil
f.each do |line|
key, value = line.split(':')
value = JSON.parse(value.strip.delete_suffix(',')) unless value.nil?
case key
when 'title'
items << item unless item.nil?
item = create_item(value)
when 'path'
item['photo'][0]['path'] = value
when 'note'
item['photo'][0]['note'][0]['html']['@value'] = value
items << item unless item.nil?
puts JSON.pretty_generate({ '@graph' => items }, indent: ' ')
title: "This is the title",
path: "/Users/dupin/Desktop/a.png",
note: "This is the note"
title: "This is another title"
path: "/Users/dupin/Desktop/b.png"
note: "This is another note"
Copy link

inukshuk commented Apr 14, 2020

Run this via ruby convert.rb input.txt > output.json (replacing input.txt with the path to your input file). Note, this is currently extremely simple: assumes you really have title, path, and note lines for each item (and no multiple paths and or notes, etc.).

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