3

I am creating a bash script that renames photos so that they include date and time in their file name. I would like to build a filename with ImageMagick's identify utility.

identify -format "IMG_%[EXIF:DateTime].jpg" myphoto.jpg

I would like to apply formatting to date and time. How can I do it with ImageMagick? Does identify support formatting?

Vytenis Bivainis
  • 2,308
  • 21
  • 28
  • 2
    You'd probably be better served by `exiftool` for that. – Mark Setchell Aug 19 '18 at 22:14
  • Sadly the answer seems to be that you can't do the formatting with ImageMagick alone. There are various alternatives. – Vytenis Bivainis Aug 20 '18 at 20:17
  • 1
    Wow, `exiftool` is one hell of a tool. I had seen it existed previously, but now I see that it is very powerful (though its documentation could be better). Here's the solution to my problem (but not the answer to my question): `exiftool "-FileName – Vytenis Bivainis Aug 23 '18 at 20:50
  • "If you find the need to use "find" or "awk" in conjunction with ExifTool, then you probably haven't discovered the full power of ExifTool" - excerpt from https://owl.phy.queensu.ca/~phil/exiftool/. Some more information can be found at https://www.sno.phy.queensu.ca/~phil/exiftool/faq.html, command line examples https://owl.phy.queensu.ca/~phil/exiftool/examples.html. – Vytenis Bivainis Aug 23 '18 at 20:53

2 Answers2

5

Use the date utility to reformat the date value. Run man date, and checkout other questions/answers for examples.

img_date=$(identify -format "%t_%[EXIF:DateTime]" input.jpeg)
#=> 2018:07:25 06:28:41
my_date=$(date -j -u -f "%Y:%m:%d %H:%M:%S" "$img_date" +"%Y-%m-%d")
#=> 2018-07-25
convert input.jpeg "output_${my_date}.jpeg"
#=> output_2018-07-25.jpeg
emcconville
  • 23,800
  • 4
  • 50
  • 66
3

You can do most of it in Imagemagick. But Imagemagick does not have built-in date-time formatting. So you have to capture the date and time and pipe it to your OS to reformat. Then capture the image name without the suffix. Then convert your input to a new image with the desired output name.

Here is an example using Unix tr to reformat the captured data and time where I change spaces to underscores and colons to hyphens.

datetime=$(identify -format "%[EXIF:DateTime]" P1050001.JPG | tr " " "_" | tr ":" "-")

inname=$(identify -format "%t" P1050001.JPG)

convert P1050001.JPG ${inname}_${datetime}.jpg


This creates a new file with the name of P1050001_2009-12-05_16-59-51.jpg

Alternately, you can combine the first two steps as:

newname=$(identify -format "%t_%[EXIF:DateTime]" P1050001.JPG | tr " " "_" | tr ":" "-")

convert P1050001.JPG ${newname}.jpg


If you do not want to use convert to create a new image, then you can use your OS to rename the original.

All of this may be easier done with exiftool as Mark Setchell has suggested.

fmw42
  • 46,825
  • 10
  • 62
  • 80