Timestamp out of 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?".
max2331
Posts: 7
Joined: 2016-09-07T04:08:38-07:00
Authentication code: 1151

Timestamp out of filename

Post by max2331 »

Hello together,

i need help for a project anc can not find the solution for specific project.
I have a project where every minute an image is produced. This image has to be overlayed to another image.
This works perfect with -composite-.

Now my problem:
The filename of the one image is like 2016081711230002.png, so basically year-month-day-hour-minute-second.
This information i need as text in my image, basically a timestamp created out of the filename.
And this has to be done every minute with a new png/filename.

Anyone an idea how to solve this?

Cheers

Max
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Timestamp out of filename

Post by snibgo »

You can write text on an image with "-annotate" or other methods. See http://www.imagemagick.org/Usage/text/

For changing the string, eg to "2016-08-17 11:23" or whatever, I would do that in a shell script.
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: Timestamp out of filename

Post by fmw42 »

What is your OS? The script to edit your file name into a time stamp will depend upon that.

How did you get the filenames to include the date/time? You must have had the data time before naming the files.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Timestamp out of filename

Post by GeeMack »

max2331 wrote:The filename of the one image is like 2016081711230002.png, so basically year-month-day-hour-minute-second. This information i need as text in my image, basically a timestamp created out of the filename. [...] And this has to be done every minute with a new png/filename. [...] Anyone an idea how to solve this?
I use several variations on this idea to timestamp satellite images from NASA and some radar images from NOAA. I usually run IM7 on Windows 10 64. ImageMagick isn't really designed to do string manipulation, but...

The idea below is in Windows syntax but can be adapted to *nix shell syntax without much trouble. It should work with any IM 6.9.3 or newer. It absolutely requires a monospace font, and your filenames must always be the same length. When I run this from the Windows command line, a timestamp is made from the filename, then that timestamp gets composited in the lower left corner of the input image to create the output.

Code: Select all

convert 2016081711230002.png ^
   -set filename:f "%[t]" ^
   -set option:labelname "%[t]" ^
   -font consolas ^
   -pointsize 24 ^
   -kerning 1 ^
   -fill white ^
   -background none ^
   ( ^
      label:"%[labelname]" ^
      -crop 16x1@ ^
      +repage ^
      label:"." -insert 14 ^
      label:":" -insert 12 ^
      label:":" -insert 10 ^
      label:"\ " -insert 8 ^
      label:"-" -insert 6 ^
      label:"-" -insert 4 ^
      +append ^
   ) ^
   -gravity southwest ^
   -geometry +10+10 ^
   -composite ^
      "_stamped_%[filename:f].png"
You get the filename into a variable using IM's built-in formatting escape "%[t]". Make that into an image with "label:". Crop the label into a stack of individual characters. Insert hyphens, colons, etc. in the right places in the stack. Then append all the pieces together to make single timestamp image. Finish by setting gravity and geometry, and composite the timestamp on the original input.

There's probably a simple variation that will just write that formatted timestamp as an image named "_stamped_2016081711230002.png". Then you can composite that onto another image if that's what you need.

The output filename in the above example is made using another IM built-in variable that holds and returns the input filename.

The best size, color, and location for that timestamp would depend on the background image.

There are more versatile — and more complicated — ways to do this sort of thing using shell variables, "for" loops, and maybe even other tools like "sed". The exact processes would depend on your OS and maybe the version of ImageMagick you're using.
Last edited by GeeMack on 2016-09-07T21:21:04-07:00, edited 3 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Timestamp out of filename

Post by fmw42 »

Clever, fully Imagemagick solution!
max2331
Posts: 7
Joined: 2016-09-07T04:08:38-07:00
Authentication code: 1151

Re: Timestamp out of filename

Post by max2331 »

Thanks already for the answers.
I'm using Mac OS X 10.11.6 and Imagemagick 6.9.5-8

The file comes from a thirdparty software, so no access to it. Similar to the satellite pictures.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Timestamp out of filename

Post by snibgo »

Ingenious solution as always from GeeMack.

If a non-monospaced font is preferred, GeeMack's output could be fed into OCR, then feed the output from that into IM. No, I'm joking, honest.
snibgo's IM pages: im.snibgo.com
max2331
Posts: 7
Joined: 2016-09-07T04:08:38-07:00
Authentication code: 1151

Re: Timestamp out of filename

Post by max2331 »

GeeMack wrote: The idea below is in Windows syntax but can be adapted to *nix shell syntax without much trouble. It should work with any IM 6.9.3 or newer. It absolutely requires a monospace font, and your filenames must always be the same length. When I run this from the Windows command line, a timestamp is made from the filename, then that timestamp gets composited in the lower left corner of the input image to create the output.

Code: Select all

convert 2016081711230002.png ^
   -set filename:f "%[t]" ^
   -set option:labelname "%[t]" ^
   -font consolas ^
   -pointsize 24 ^
   -kerning 1 ^
   -fill white ^
   -background none ^
   ( ^
      label:"%[labelname]" ^
      -crop 16x1@ ^
      +repage ^
      label:"." -insert 14 ^
      label:":" -insert 12 ^
      label:":" -insert 10 ^
      label:"\ " -insert 8 ^
      label:"-" -insert 6 ^
      label:"-" -insert 4 ^
      +append ^
   ) ^
   -gravity southwest ^
   -geometry +10+10 ^
   -composite ^
      "_stamped_%[filename:f].png"
You get the filename into a variable using IM's built-in formatting escape "%[t]". Make that into an image with "label:". Crop the label into a stack of individual characters. Insert hyphens, colons, etc. in the right places in the stack. Then append all the pieces together to make single timestamp image. Finish by setting gravity and geometry, and composite the timestamp on the original input.
Hi

i am just getting a weird label output, but no stamp on the png.

the actual filename is: 2016081711230002dBuZ.ppi.png
and I think I cannot translate it completly into unix as i am a complete beginner to Imagemagick
Could you please help a little bit more?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Timestamp out of filename

Post by fmw42 »

His code was counting on the file name containing only the timestamp information. You have two issues: 1) you include other characters (dBuZ) and 2) another period (.ppi), which is not the suffix. Fortunately Imagemagick %t will include the .ppi in the filename and uses the last period for the finding the extension. I suppose the script can be modified, but he will need to know that the number of characters in the time stamp part is always the same. The remaining character images will need to be deleted from the crop, which should be possible.

I never use periods (or spaces) in my filenames. I always use either hyphens or underscores.

(Note, it is not fair to the people who are trying to help, when you provide inaccurate information)
max2331
Posts: 7
Joined: 2016-09-07T04:08:38-07:00
Authentication code: 1151

Re: Timestamp out of filename

Post by max2331 »

The naming convention of this file is not my choice, thats how I get the images and no other possibility.

If I would have known, that it would play such a crucial role I would have given the whole filename.

Yes thats the whole filename and its always the same length.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Timestamp out of filename

Post by GeeMack »

max2331 wrote:If I would have known, that it would play such a crucial role I would have given the whole filename.
A couple of very important things to remember about the the command I posted...

It uses the filename without the extension to make a label. Then it has to crop that label into the number of pieces to equal the number of characters in the filename. The operation that does "-crop 16x1@" has to divide it into exactly the actual number of characters instead of 16.

Also notice where I set the font with "-font consolas". If you don't have that font you'd have to substitute another monospace font that is installed in your system.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Timestamp out of filename

Post by fmw42 »

If the filename is always the same length, then that length of characters would replace 16. But then you need to know how many belong to the timestamp and delete all the rest after the crop Nx1@. That should work, I believe. If your filenames are as described above N=24 and you need to -delete 16--1 (ie from character 16 to the last=-1), since IM indexes from 0. Since you are on a Mac, GeeMack's code needs to be translated from Windows syntax to Unix syntax. See http://www.imagemagick.org/Usage/windows/. That is mostly ^ goest to \ and (...) goes to \(...\)

So this seems to work for me (unix syntax):

Code: Select all

convert 2016081711230002dBuZ.ppi.png \
   -set filename:f "%[t]" \
   -set option:labelname "%[t]" \
   -font consolas \
   -pointsize 24 \
   -kerning 1 \
   -fill white \
   -background none \
   \( \
      label:"%[labelname]" \
      -crop 24x1@ \
      +repage \
      -delete 16--1 \
      label:"." -insert 14 \
      label:":" -insert 12 \
      label:":" -insert 10 \
      label:"\ " -insert 8 \
      label:"-" -insert 6 \
      label:"-" -insert 4 \
      +append \
   \) \
   -gravity southwest \
   -geometry +10+10 \
   -composite \
      "_stamped_%[filename:f].png"
-delete 16--1 could also be written -delete 16-23, since we know there are 24 characters in the name and the first 16 are the timestamp.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Timestamp out of filename

Post by GeeMack »

fmw42 wrote:If the filename is always the same length, then that length of characters would replace 16. But then you need to know how many belong to the timestamp and delete all the rest after the crop Nx1@. That should work, I believe.
That is correct. I overlooked deleting the stray characters at the end.

Another approach is to crop the label into the correct number of characters, then put that whole stack in a memory register. The timestamp can be assembled by calling images (characters) out of that stack only when they're needed. The additional characters, hyphens, colons, etc., can be easily added to the new string where they belong. Using this technique you can even re-arrange the order of the output if necessary. Also, you don't have to delete any spare characters; you just don't use them.

Something like this to replace the parentheses section in the middle of my sample code above...

Code: Select all

...
   ( ^
      label:"%[labelname]" ^
      -crop 24x1@ ^
      +repage ^
      -write mpr:tstring ^
      -delete 0--1 ^
   ) ^
   ( ^
      mpr:tstring[0-3] ^
      label:"-" ^
      mpr:tstring[4,5] ^
      label:"-" ^
      mpr:tstring[6,7] ^
      label:"\ " ^
      mpr:tstring[8,9] ^
      label:":" ^
      mpr:tstring[10,11] ^
      label:":" ^
      mpr:tstring[12,13] ^
      label:"." ^
      mpr:tstring[14,15] ^
      +append ^
   ) ^
...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Timestamp out of filename

Post by fmw42 »

Code: Select all

Another approach is to crop the label into the correct number of characters, then put that whole stack in a memory register.
Yes, I agree. That is better than my modification.
max2331
Posts: 7
Joined: 2016-09-07T04:08:38-07:00
Authentication code: 1151

Re: Timestamp out of filename

Post by max2331 »

I get an:
convert: invalid argument for option `-delete': 0–-1 @ error/convert.c/ConvertImageCommand/1265.

Here is my code right now:

Code: Select all

convert 2016081711230002dBuZ.ppi_top.png -set filename:f "%[t]" -set option:labelname "%[t]" -font consolas -pointsize 24 -kerning 1 -fill black -background none \(label:"%[labelname]" -crop 28x1@ +repage -write mpr:tstring -delete 0–-1 \) \(mpr:tstring[0-3] label:"-" mpr:tstring[4,5] label:"-" mpr:tstring[6,7] label:"\ " mpr:tstring[8,9] label:":" mpr:tstring[10,11] label:":" mpr:tstring[12,13] label:"." mpr:tstring[14,15] +append\) -gravity southwest -geometry +10+10 -composite "_stamped_%[filename:f].png"
Post Reply