{"slug": "add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line", "title": "Add a watermark to thousands of images with ImageMagick from the command line", "summary": "A developer built a command-line ImageMagick watermarking workflow that processes 2,000 photos in 56 seconds by running one ImageMagick process per CPU core with MAGICK_THREAD_LIMIT=1, after a naive parallel loop proved slower due to thread contention. The script writes watermarked copies to a separate folder preserving subfolder structure, skips already-processed files on reruns, and handles filenames with special characters. The developer wrapped it in cmdbook, a tool that turns placeholder-marked scripts into GUI forms with dropdowns for position and opacity.", "body_md": "You have a folder of photos, with subfolders, and every one needs a \"© 2026 Jane\" in the corner before it goes online.\n\nHow do you do it?\n\nYou ask an AI. It gives you `mogrify`, which is short, but it overwrites your originals. You ask for a version that saves copies. Then you ask for a loop over the subfolders, and one that doesn't break on `Bob's photo 1.JPG`.\n\nIt works, slowly. A plain loop does one image at a time: 2,000 photos take about 9 minutes on my Mac. So you ask how to run several at once, and get `xargs -P`. Now it's even slower: each ImageMagick also starts a thread per core, and 20 of them fight over the CPU. Mine did 480 photos in 10 minutes. One more line fixes it: `MAGICK_THREAD_LIMIT=1`.\n\nYou save the script somewhere: a Markdown note, or a `watermark.sh` file. Next time the text is different, or the corner, and you have to find the script and edit the middle of a long `magick` line.\n\nWith [cmdbook](https://printf.app/cmdbook), the script has one place to live, and it gets a GUI for free.\n\n**1. Save the script once.** Create a command in cmdbook and paste the script (it's at the end).\n\n**2. Mark what changes.** The folder, the text, the corner and the opacity become placeholders:\n\n```\nDIR=\"{{prompt:folder}}\"\nTEXT=\"{{prompt:text?© Your Name}}\"\nPOSITION={{prompt:position?southeast|southwest|northeast|northwest|center}}\nOPACITY={{prompt:opacity?60|40|80|100}}\n```\n\nA list like `southeast|southwest|...` becomes a dropdown. The first one is the default.\n\n**3. Click Run.** cmdbook shows a form. Drag the folder onto it, type your text, pick a corner, press Enter.\n\n**4. Watch it run.**\n\n```\nWatermarking with 20 jobs at a time...\n  failed: ./products/broken.png\n  500 done\n  1000 done\n  1500 done\n  2000 done\n\nWatermarked 2000, skipped 0 (already done), failed 1.\nSaved in: /Users/jane/Photos/watermarked\n```\n\n2,000 photos (most of them 12 megapixels) in 56 seconds on my Mac. Your originals are not touched: the results go to a `watermarked` folder, with the same subfolders. Run it again after adding photos, and only the new ones are done.\n\nNext time, your folder, text and corner are already filled in.\n\nThat's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.\n\nTry cmdbook here: [printf.app/cmdbook](https://printf.app/cmdbook)\n\nThe full script (macOS, needs ImageMagick)\n\n```\nDIR=\"{{prompt:folder}}\"\nTEXT=\"{{prompt:text?© Your Name}}\"\nPOSITION={{prompt:position?southeast|southwest|northeast|northwest|center}}\nOPACITY={{prompt:opacity?60|40|80|100}}\n\nexport PATH=\"/opt/homebrew/bin:/usr/local/bin:$PATH\"\ncommand -v magick &gt;/dev/null || { echo \"ImageMagick not found. Install it: brew install imagemagick\"; exit 1; }\ncd \"$DIR\" || exit 1\n\n# Originals are never changed: results go to a \"watermarked\" folder,\n# with the same subfolders.\nexport OUTDIR=\"watermarked\" POSITION\nexport TEXT=\"${TEXT//%/%%}\"                       # % is special in ImageMagick text\nexport ALPHA=$(awk -v o=\"$OPACITY\" 'BEGIN { print o / 100 }')\nexport HALF=$(awk -v o=\"$OPACITY\" 'BEGIN { print o / 200 }')   # thin, lighter outline so it reads on light images too\nJOBS=$(sysctl -n hw.ncpu 2&gt;/dev/null || nproc)    # one ImageMagick per CPU core\nexport MAGICK_THREAD_LIMIT=1                      # ...each using one thread, or they fight over the cores\n\necho \"Watermarking with $JOBS jobs at a time...\"\nfind . -path \"./$OUTDIR\" -prune -o -type f \\( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.webp' \\) -print0 |\n  xargs -0 -n 20 -P \"$JOBS\" /bin/bash -c '\n    for IN; do\n      OUT=\"$OUTDIR/${IN#./}\"\n      if [ -e \"$OUT\" ] &amp;&amp; [ \"$OUT\" -nt \"$IN\" ]; then echo skip; continue; fi\n      mkdir -p \"$(dirname \"$OUT\")\"\n      # Text size follows the image: 1/25 of the shorter side.\n      S=$(magick identify -format \"%[fx:min(w,h)]\" \"$IN[0]\" 2&gt;/dev/null) || { echo \"fail $IN\"; continue; }\n      if magick \"$IN\" -auto-orient -gravity \"$POSITION\" -pointsize $((S / 25)) \\\n           -fill \"rgba(255,255,255,$ALPHA)\" -stroke \"rgba(0,0,0,$HALF)\" -strokewidth $((S / 1500 + 1)) \\\n           -annotate +$((S / 40))+$((S / 40)) \"$TEXT\" \"$OUT\" 2&gt;/dev/null; then\n        echo ok\n      else\n        rm -f \"$OUT\"; echo \"fail $IN\"\n      fi\n    done' _ |\n  awk '$1 == \"ok\" { ok++ } $1 == \"skip\" { skip++ } $1 == \"fail\" { fail++; sub(/^fail /, \"\"); print \"  failed: \" $0 }\n       (ok + skip + fail) % 500 == 0 { printf \"  %d done\\n\", ok + skip + fail }\n       END { printf \"\\nWatermarked %d, skipped %d (already done), failed %d.\\nSaved in: %s/watermarked\\n\", ok, skip, fail, ENVIRON[\"PWD\"]; exit fail &gt; 0 }'\n```\n\n", "url": "https://wpnews.pro/news/add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line", "canonical_source": "https://dev.to/yingtse/add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line-18el", "published_at": "2026-11-22 01:02:00+00:00", "updated_at": "2026-11-22 01:12:39.014201+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["ImageMagick", "cmdbook", "macOS"], "also_reported_by": [], "takeaway": "Running one ImageMagick process per CPU core with MAGICK_THREAD_LIMIT=1 cut 2,000 12-megapixel watermarks to 56 seconds, versus 480 photos in 10 minutes when parallel processes each spawned a thread per core.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line", "markdown": "https://wpnews.pro/news/add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line.md", "text": "https://wpnews.pro/news/add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line.txt", "jsonld": "https://wpnews.pro/news/add-a-watermark-to-thousands-of-images-with-imagemagick-from-the-command-line.jsonld"}}