Batch Adding Text (File Name) to JPEG Images
November 25, 2008 2:33 PM   Subscribe

I have a bunch (thousands) of JPEG pictures of my company's parts. The JPEG file names are the part numbers of the parts (eg. 1234.jpg is a picture of part number 1234. Here's the tricky part...

...none of the JPEG files show the part number in the actual image itself. Is there a batch application out there that will take these JPEG files and add a visible file name to one corner of the image?
posted by ZenMasterThis to Technology (9 answers total) 5 users marked this as a favorite
Best answer: I can't vouch for it, but this product was my third hit for "stamp filename onto image" in Google. It lets you set font, style, color, location... and will use filename as a variable.
posted by rokusan at 2:47 PM on November 25, 2008

ImageMagick to the rescue!

That is, it does add text to images, via the command line. Someone with a little bit of scripting knowledge could turn it into an automated process with little effort.
posted by cgg at 2:54 PM on November 25, 2008

Seconding ImageMagick. See this page specifically.
posted by roue at 2:59 PM on November 25, 2008

Oh yeah, ImageMagick. I've even used that. :)

You'd have to write your own code to use it for this, though.
posted by rokusan at 3:10 PM on November 25, 2008

Yeah, Imagemagick.

To convert a single image:
convert 1234.jpg -fill red -gravity South -pointsize 20 -annotate +0+5 'Part No. 1234' part1234_annotated.jpg

The arguments to convert make the label 20 points, in red, and place it ("-gravity South") along the bottom of the image.

A new image is created, leaving the old image unchanged.

If you're running the bash shell (and have the standard linux directory list program) this,entered at the bash prompt, will run the above on all your jpgs:

for f in [0-9]*.jpg; do fn=${f%.*}; convert ${fn}.jpg -fill black -gravity South -pointsize 20 -annotate +0+5 "Part No.${fn}" ${fn}_annotated.jpg ;done
posted by orthogonality at 3:38 PM on November 25, 2008 [1 favorite]

General script for doing a Thing on every file in directory:

1) Download Python.

2) Write this into a file called

import os.path
dir = "c:\your\folder\here"

for filename in os.listdir(dir):
fullpath = os.path.join(dir, filename)
command = "cp " + fullpath + "c:\."

3) Run the script in a command line via "python"

The above script goes through all the files in the "dir" and copies them to the root of the c:\ drive. Replace the stuff that is assigned to "command" to whatever you find to do what you want in ImageMajick or whatever.

On Preview:
Based on what orthogonality found in ImageMajick:

Replace command = "cp " + fullpath + "c:\." with:

command = "convert " + fullpath + " -fill red -gravity South -pointsize 20 -annotate +0+5 'Part No. " + filename[0:-4] + "' " + filename[0:-4] + "_annotated.jpg"

I would highly recommend doing this with a bunch of test images before you try it on any sensitive stuff. I didn't test the script, just wrote it out of my head.
posted by zhivota at 3:51 PM on November 25, 2008 [1 favorite]

Crap, metafilter killed my indenting. After the for loop, all the statements have to be indented. This is required.
posted by zhivota at 3:52 PM on November 25, 2008

Response by poster: Thanks, rokusan, this is exactly what I need!

(I had Googled "put ..." and "place ..." but not "stamp ...")
posted by ZenMasterThis at 4:02 PM on November 25, 2008

If you are using OSX, automator can do this. Just download the adobe photoshop actions, there is one that can add text, including file name, as a watermark. As a bonus you can pick where on the image it does it, and even have it apply a layer style. The layer style would come in handy so that you can apply a stroke to the text, which would make sure it is readable on the image.
posted by travis08 at 5:25 PM on November 25, 2008

« Older What do I need to reverse-engineer hardware and...   |   Can a shy person succeed in marketing? Newer »
This thread is closed to new comments.