Use current date and time as filename

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ArcSyn
Posts: 2
Joined: 2016-07-22T12:54:54-07:00
Authentication code: 1151

Use current date and time as filename

Post by ArcSyn »

Is there a simple way to set the output file name as the current date and time?

Windows 10 x64
ImageMagick 7.0.2-Q16

My current setup is fairly simple. Input a folder of pictures, crop them down, then save.

Code: Select all

magick *.jpg -crop 1280x1024+640+448 image.jpg
What I'd like to do is name the output files with the date and time the photo was taken, so it looks something like:
image_20160722-2215.jpg

So I'd like to use the YYYYMMDD-HHMM format with 24-hour time.

Any help would be greatly appreciated. Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Use current date and time as filename

Post by snibgo »

I would rename the files with exiftool.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Use current date and time as filename

Post by fmw42 »

Seems like you could extract the DateTime EXIF tag in IM using string formats. See http://www.imagemagick.org/script/escape.php. You may need to parse the data from the string format to get it into the format you want.

In Unix, I would write a loop over each image and do the following. Note, I have to change the datetime format, since it comes out of IM string format as 2015:12:05 07:00:18. Otherwise, it could all be done in one command line.

Code: Select all

datetime=`magick DSC_0001.JPG -format "%[EXIF:datetime]\n" info: | tr -d ":" | tr " " "-"`
imagename=`magick DSC_0001.JPG -format "%t" info:`
echo "$datetime"
20151205-070018
echo "$imagename"
DSC_0001
magick DSC_0001.JPG -crop WxH+X+Y +repage ${imagename}_${datetime}.jpg
ArcSyn
Posts: 2
Joined: 2016-07-22T12:54:54-07:00
Authentication code: 1151

Re: Use current date and time as filename

Post by ArcSyn »

Thank you for the ideas!
Post Reply