Captioning

Caption .txt sidecar files: format, naming and bulk editing

The short answer

A caption sidecar is a plain-text file that sits next to an image and shares its base name: forest_01.png pairs with forest_01.txt. For tag captions, write one line of tags separated by a comma and a space, save it as UTF-8, and use one style (spaces or underscores) for the whole dataset. ImgTagger can load images and their .txt files together, edit tags across the batch, and export a ZIP of updated captions, with or without the images.

Sidecar caption files are the most common way to store training captions. Being plain text, they open in any editor and are easy to script. They are also unforgiving: one wrong character in a file name and the caption is silently ignored.

This page covers the format rules, how the ImgTagger tag editor reads and writes these files, and how to fix the mistakes that most often break a dataset.

What a sidecar file is

A sidecar is a separate file that carries information about another file. In a dataset, the image holds the pixels, a .txt beside it holds the caption, and the two are paired purely by name:

forest_01.png     forest_01.txt
forest_02.webp    forest_02.txt

A tag caption is a single line:

no humans, scenery, forest, tree, sky, day, outdoors

Trainers such as kohya-ss sd-scripts find a caption by taking the image's path, dropping its extension and adding the caption extension. That is the whole lookup, which is why naming matters so much.

A training folder with a caption file beside each pictureEach picture has a text file with the same name and a .txt ending in the same folder, and the text file holds its caption.my-dataset/cat.jpgcat.txt“orange tabby cat, sitting, sofa”dog-park.pngdog-park.txt“golden retriever, running, grass”IMG_0412.webpIMG_0412.txt“a bicycle against a wall”Same name, different ending, same folder.
A made-up example of the usual layout (a "sidecar" file). The trainer pairs a picture with its caption by the name, so a misspelled name means no caption.

Naming rules

  • Same base name, different extension. The base name is everything before the last dot, so forest_01.png needs forest_01.txt.
  • One caption per base name. If cat_01.png and cat_01.jpg share a folder, both point at cat_01.txt. Give every image a unique base name.
  • Match the case. Windows and macOS usually ignore case in file names, but Linux doesn't, and many training machines run Linux. There, Cat_01.png won't find cat_01.txt.

When you drop files in, ImgTagger handles names like this:

  • A .txt is matched to the image with the same base name, ignoring case, so Cat_01.TXT still pairs with cat_01.png. On export, the .txt is named exactly after the image, which fixes case mismatches.
  • A .txt with no matching image shows a "No matching image found" warning, and its tags are discarded. Drop it again once the image is loaded.
  • Dropping a .txt for an image that is already loaded adds its tags to the existing ones rather than replacing them.
  • Dropping an image with the same file name as one already loaded replaces the picture and keeps its tags. Handy after you crop or retouch a file, as long as the name and extension stay the same.
  • Images are identified by file name alone. Two different files called 001.png from different folders can't both be loaded: the second replaces the first and inherits its tags.

How sd-scripts looks for a caption

Trainers differ in the details, and the details decide whether a caption is used or silently skipped. These are from the kohya-ss sd-scripts source as of late October 2026, for the folder-based dataset most LoRA guides use.

  1. It lists images by extension. The list is .png, .jpg, .jpeg, .webp and .bmp, each in all lower case or all upper case (AVIF and JPEG XL join only when their optional libraries are installed). Python's file matching is case-sensitive on Linux, so a file ending .Jpg or .Png is not found there at all. The log line "found directory ... contains N image files" is the quickest check: compare N with the count you expect.
  2. It builds the caption name. The image path minus its extension, plus caption_extension, so forest_01.png looks for forest_01.txt when you set .txt. On Linux the case must match here too, including .txt against .TXT.
  3. It has a fallback name. If the base name splits into five or more parts at underscores, and no caption has the full name, it also tries the name with the last four parts removed, a leftover from the way face-cropped copies were once named. So mira_a_b_c_d.png without its own caption would pick up a stray mira.txt. It only bites when such a file exists, but it explains an image that trains with someone else's caption.
  4. It reads the first line. The file is opened as UTF-8 and only the first line is kept, trimmed. A file that is not valid UTF-8 stops the run with an error naming it, and an empty file stops it with "caption file is empty".

What happens when no caption is found depends on how the dataset is set up, and is covered in the LoRA captioning guide's trainer section. Other trainers follow the same sidecar idea with their own rules, so read your trainer's docs for its extension list and fallback before a long run.

Separators and spacing

Tag captions separate tags with commas. ImgTagger writes a comma followed by a space (smile, upper body, outdoors), which is the most common style. When it reads a file, it splits on commas and trims the spaces, so smile,upper body and smile , upper body load the same way.

Because commas are the separator, a tag can't contain one (none of the WD14 tags do). A sentence inside a tag caption also gets split at its commas: ImgTagger would show "a cat by the window" and "looking outside" as two tags, and trainers that shuffle tags would shuffle the fragments.

Keep each caption on one line. ImgTagger splits only on commas, so a file with one tag per line and no commas loads as one long tag. kohya-ss sd-scripts reads only the first line of a caption file unless its wildcard option is turned on.

Spaces or underscores

Danbooru tags use underscores (long_hair) because a space separates tags on the site. In comma-separated captions spaces are safe, and ImgTagger outputs long hair. Both styles are in wide use. What breaks things is mixing them: to your trainer, and in ImgTagger's Filter panel, long hair and long_hair are two different tags.

Mixing usually happens when you load an older dataset that used underscores and then auto-tag it. The old long_hair stays, and the tagger adds long hair beside it. To stay consistent:

  • If you work in ImgTagger, use spaces everywhere, including hand-typed tags and the "Always add these tags first" box.
  • Convert an underscore dataset before adding new tags. ImgTagger has no find-and-replace, so use an editor that can replace across many files, such as VS Code, to swap _ for a space.
  • Check what a blind replace would break: a trigger word such as mira_oc, and emoticon tags such as ^_^ and >_<, where the underscore is part of the face. ImgTagger leaves emoticon tags like these untouched in its own output.

Encoding and line endings

Save caption files as plain UTF-8. It is what most trainers expect: kohya-ss sd-scripts reads captions as UTF-8 and stops with an error on a file it can't decode. If your editor offers both "UTF-8" and "UTF-8 with BOM", pick plain UTF-8. Some scripts read the invisible byte-order mark as part of the first tag, which is usually your trigger word.

ImgTagger writes UTF-8 without a BOM, as one line with no line break at the end. When it reads a file it drops any BOM, so loading a dataset and exporting it again cleans that up. A file saved in an older encoding, such as Shift-JIS, shows replacement characters (�) in the tag editor. Re-save it as UTF-8 in an editor that can detect the original encoding, then load it again.

Line endings (Windows CRLF or Unix LF) don't matter for a one-line file. A trailing line break is harmless in ImgTagger and in sd-scripts, since both trim whitespace. Just avoid extra lines.

Loading an existing dataset into ImgTagger

  1. Drag the whole dataset folder onto the tagger page. Subfolders are included, and hidden system files such as .DS_Store are skipped. You can also select the images and .txt files themselves and drag them in, or click the big drop box and pick them in the file dialog. ZIP files aren't read, so unzip them first.
  2. ImgTagger reads every caption before it loads the images, so each image picks up its caption however large the batch is.
  3. Open the Filter panel and look for the ∅ untagged entry. Images listed there that should have captions didn't match their files, so check the warnings and the naming mistakes below.

Bulk editing: add, prepend and remove

The tagger's side panels change many captions at once. Most actions come in a "Selected" and an "All" version. "Selected" means images that are checked and visible under the current filter, so the Filter panel is how you target a subset.

  • Add Tags adds one or more comma-separated tags in Append (end) or Prepend (start) mode. Images that already have a tag are skipped. In Prepend mode, they go to the front in the order you typed them.
  • Remove Tags removes whole tags, ignoring case and treating underscores and spaces alike, so long_hair also removes long hair. The same panel has buttons to clear all tags.
  • Filter panel lists every tag with its count. Each tag's trash button deletes that exact spelling from every image in one click.

To rename a tag, use four steps: filter by the old tag in Any mode, click Select All, add the new tag to the selected images, then remove the old tag from all images. The new tag lands at the end (or the start, in Prepend mode) rather than where the old one was.

Exporting: tags only or images and tags

Click Download and choose what goes into the ZIP. ImgTagger remembers your choices for next time.

IncludeSelectionZIP nameUse it when
.txt tag filesAll Itemstags.zipThe images are already in place. Unzip over the folder to replace the old captions.
Images and .txtAll Itemsimages_tags.zipYou want a complete, self-contained copy.
.txt tag filesSelected Onlyselected_tags.zipYou edited a subset and only want those captions.
Images and .txtSelected Onlyselected_images_tags.zipYou are splitting off a smaller dataset, such as every image that matches a filter.

The ZIP is flat, with no subfolders, and every caption is named after its image. An image with no tags still gets a .txt file, which is empty.

Checking a dataset folder before training

ImgTagger's ∅ untagged entry catches images without tags, but only for files you load. A folder that has been through renames, merges and other tools deserves a direct check where it will be trained. This script for macOS, Linux, Git Bash or WSL, run inside the folder, prints one line per problem and nothing when the folder is clean:

# images with no caption
for img in *.png *.jpg *.jpeg *.webp; do
  [ -e "$img" ] || continue
  [ -f "${img%.*}.txt" ] || echo "no caption: $img"
done
# captions with no image
for t in *.txt; do
  found=no
  for e in png jpg jpeg webp; do [ -e "${t%.txt}.$e" ] && found=yes; done
  [ $found = yes ] || echo "no image: $t"
done
# empty captions, captions with more than one line, captions that start with a BOM
for t in *.txt; do
  [ -s "$t" ] || echo "empty: $t"
  [ "$(grep -c '' "$t")" -gt 1 ] && echo "several lines: $t"
  head -c 3 "$t" | grep -q $'\xef\xbb\xbf' && echo "BOM: $t"
done
# files a trainer may skip: other formats and mixed-case extensions
ls | grep -vE '\.(png|jpg|jpeg|webp|bmp|txt|PNG|JPG|JPEG|WEBP|BMP|TXT)$'

Each kind of line points to a fix further down this page: "no image" and "no caption" to the naming mistakes, "empty" to empty caption files, "BOM" to the encoding section, and "several lines" to a caption that only keeps its first line. Anything the last command prints is a file the trainer will probably pass over, such as a .gif, a .Jpg or a stray .DS_Store. Line counting uses grep -c '' because ImgTagger writes captions without a final line break, which makes wc -l report one line too few.

When a trainer wants one metadata file

Not every trainer reads sidecars. Two common alternatives keep all captions in one file, and both are easy to build from ImgTagger's .txt export.

metadata.jsonl for Hugging Face diffusers

The diffusers text-to-image LoRA example reads a folder in the Hugging Face imagefolder layout, which needs a metadata.jsonl beside the images. Each line is one JSON object with a file_name and, by the script's default --caption_column, a text field (see the datasets ImageFolder docs). This Python script, run inside the folder, writes one from the sidecars:

import json, pathlib

exts = {".png", ".jpg", ".jpeg", ".webp"}
with open("metadata.jsonl", "w", encoding="utf-8") as out:
    for img in sorted(pathlib.Path(".").iterdir()):
        cap = img.with_suffix(".txt")
        if img.suffix.lower() in exts and cap.exists():
            text = cap.read_text(encoding="utf-8-sig").strip()
            row = {"file_name": img.name, "text": text}
            out.write(json.dumps(row, ensure_ascii=False) + "\n")

Reading with utf-8-sig drops a byte-order mark if one slipped in. Run the folder check first, since an image without a .txt is simply left out of the file.

JSON metadata for the sd-scripts fine-tuning method

The fine-tuning dataset in sd-scripts reads a metadata file instead of sidecars, passed with --in_json or a subset's metadata_file. Its metadata specification allows a single JSON object keyed by image path or a JSON Lines file, and reads three fields per image: caption, tags (joined onto the caption) and image_size, which lets bucketing skip opening each image. The repository's merge_dd_tags_to_metadata.py builds the file from a folder of .txt tag files, so caption in ImgTagger first and merge afterwards. Keep one metadata file per image folder: the config guide strongly recommends a separate file and subset for each directory.

Common mistakes and how to fix them

Double extensions (image.png.txt)

Here the base name is image.png rather than image. Trainers looking for image.txt won't find it, and ImgTagger warns that there is no matching image. It usually comes from a script that added .txt to the whole file name, or from Windows hiding known extensions so you never saw the real name.

Fix: first make extensions visible (Windows 11 File Explorer: View → Show → File name extensions; macOS Finder: Settings → Advanced → Show all filename extensions). Then rename in bulk. In a macOS or Linux terminal, inside the folder:

for f in *.png.txt; do mv "$f" "${f%.png.txt}.txt"; done

In Windows PowerShell:

Get-ChildItem *.png.txt | Rename-Item -NewName { $_.Name.Replace('.png.txt', '.txt') }

Repeat with .jpg or .webp in place of .png as needed.

Hidden system files

Operating systems leave small hidden files in folders. macOS adds .DS_Store, plus ._ files on drives formatted for Windows and on network shares (and a __MACOSX folder inside ZIPs made with Finder). Windows adds Thumbs.db and desktop.ini. Some dataset tools try to read these as images or captions and fail, and in ImgTagger a ._name.txt file triggers a "No matching image" warning.

Fix: an ImgTagger export only contains images and the captions it writes, so exporting a fresh copy leaves them behind. (Our dataset cleanup workflow covers the rest of the tidy-up.) To clean a folder in place on macOS or Linux:

find . -name '._*' -delete
find . -name '.DS_Store' -delete

On Windows, turn on hidden items in File Explorer's View menu and delete Thumbs.db and desktop.ini from the dataset folder.

Names that no longer match after renaming

Batch-renaming the images but not the captions orphans every caption. In ImgTagger you will see a run of "No matching image" warnings and a large ∅ untagged count.

Fix: undo the rename if you still can (File Explorer and Finder both can, straight away), then rename images and captions together. The safest order is to rename first and caption second. If a captioned set must be renamed, this loop for macOS, Linux, Git Bash or WSL gives each image and its caption the same new name. Run it on a copy, and choose a prefix no current file uses:

i=1
for img in *.png *.jpg *.webp; do
  [ -e "$img" ] || continue
  base="${img%.*}"; ext="${img##*.}"
  new=$(printf "forest_%03d" "$i")
  mv "$img" "$new.$ext"
  [ -f "$base.txt" ] && mv "$base.txt" "$new.txt"
  i=$((i+1))
done

Duplicate tags

Duplicates come in two kinds. Exact repeats, such as smile, smile, usually come from merging captions from two sources or from typing in the tag editor. Near-duplicates are one tag in two spellings: long hair and long_hair, or Smile and smile.

Fix: exact repeats disappear when you load the files into ImgTagger and export them again. For near-duplicates, search the Filter panel (searching long shows both spellings together), Filter by the variant in Any mode, click Select All, and add the spelling you keep to the selected images. Then click the variant's trash button in the Filter panel. (Remove Tags would delete both spellings, since it ignores case and underscores.) For tags that are spelled right but not worth keeping, see the tag pruning guide.

Edited captions the trainer ignores

You fix a batch of captions, train again, and nothing changes. In sd-scripts, the usual cause is cache_info = true in the dataset config. It saves every image's size and caption into metadata_cache.json inside the image folder, and on later runs it reads captions from that file instead of the .txt files. New images are missed the same way.

Fix: delete metadata_cache.json after any caption or image change, and the next run rebuilds it. The config guide suggests the option for datasets of thousands of images, where the time saved is real; on a LoRA-sized set, leaving it off avoids the trap.

Empty caption files

An empty .txt is usually an image you forgot to tag. ImgTagger exports one for every untagged image, and kohya-ss sd-scripts stops with a "caption file is empty" error when it finds one. Fix: before exporting, check the ∅ untagged entry in the Filter panel, then tag those images or leave them out of the export. It is one of the checks in our LoRA captioning workflow.

Put this into practice

Run WD14 tagger models on a whole folder, then search, filter and bulk-edit the tags before exporting .txt caption files. It runs in your browser. Open the Batch WD14 Tagger & Editor.

Frequently asked questions

Can I use .caption files instead of .txt?

ImgTagger reads and writes .txt files only. kohya-ss sd-scripts looks for .caption by default, but the simplest fix is to tell it to read .txt with --caption_extension=".txt". If another tool insists on .caption, change the extensions in bulk with a file renamer after you export.

Does exporting change my image files?

No. ImgTagger puts your original files into the ZIP byte for byte, with no resizing, recompression or metadata changes. The tagger works on a resized copy in memory and never writes it out.

Can one caption file hold a sentence and a tag list?

Yes, two ways. On one line, separate them with a marker such as ||| and set it as keep_tokens_separator in sd-scripts, so the sentence stays whole while the tags shuffle. On two lines, turn on enable_wildcard and sd-scripts picks one line at random each time. Both are explained in our tags vs sentence captions guide. ImgTagger itself reads a file as comma-separated tags, so assemble such files after you finish editing tags.

Can ImgTagger keep my subfolders when it exports?

No. The ZIP is always flat. If your dataset is split across folders, such as one folder per concept, load and export one folder at a time.