I’m not sure a batch solution is much faster – I mean you can instantly select an entire directory of files. But let me repost the thing I just deleted. I actually felt a little silly for having made it.
This seems to work, but it is absolutely experimental. We always say to make a backup, but this time I really mean it.
The code below (made with the help of AI, yes) works in python3 (not earlier versions). You can change the line on the bottom to match your file image directory and desired output file. I used “output.json”.
Once you have your output file, drag and drop it into your TW file. Every title in the import dialogue should say “A tiddler with this title already exists”. If it doesn’t, then you need to change the incoming name to match. Then import. You made a backup, right?
So, this seems to work, but I don’t understand how TW creates its own image export. It puts a lot of stuff on the front of the base64 ASCII. I don’t know if any of that is important, but the import seems to work without it.
This was done on Linux. Don’t know if there will be problems with Windows, that doesn’t recognise the difference between “Title”, “tItle”, and “TITLE”.
Edit: If you do it this way, any tags or extra fields in your original tiddlers will be lost. So maybe someone will come up with a way to save and then re-apply those attributes after import.
import os
import base64
import json
from datetime import datetime
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded = base64.b64encode(image_file.read()).decode("utf-8")
return encoded
def create_tiddler(image_path):
image_name = os.path.basename(image_path)
title = os.path.splitext(image_name)[0]
encoded_image = image_to_base64(image_path)
mime_type = f"image/{os.path.splitext(image_name)[1][1:].lower()}"
tiddler = {
"created": datetime.now().strftime("%Y%m%d%H%M"),
"title": image_name,
"type": mime_type,
"text": f"{encoded_image}",
"tags": "image"
}
return tiddler
def pack_images_to_json(image_folder, output_file):
tiddlers = []
for filename in os.listdir(image_folder):
if filename.lower().endswith((".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp")):
image_path = os.path.join(image_folder, filename)
tiddlers.append(create_tiddler(image_path))
with open(output_file, "w") as f:
json.dump(tiddlers, f, indent=2)
# Example usage:
# pack_images_to_json("path/to/image/folder", "output.json")
pack_images_to_json("./files","output.json")