Return to site

Exiftool Python

broken image


PyExifTool is a Python library to communicate with an instance of PhilHarvey's excellent ExifTool command-line application. The libraryprovides the class exiftool.ExifTool that runs the command-linetool in batch mode and features methods to send commands to thatprogram, including methods to extract meta-information from one ormore image files. Since exiftool is run in batch mode, only asingle instance needs to be launched and can be reused for manyqueries. This is much more efficient than launching a separateprocess for every single query.

  1. Python Exiftool Subprocess
  2. Install Exiftool
  3. Exiftool Python Cheat
  4. GitHub - Exiftool/exiftool: ExifTool Meta Information Reader ...

Digital photos contain EXIF data which can be read and displayed by suitable software. In this post I will demonstrate reading EXIF data using Python. Pretty much any smartphone, tablet or digital camera embeds EXIF data into each photograph it takes, with details of the device itself as well as the individual photo. A quick and easy way to extract the geotag information from a folder of photos (in this case drone images), and output the data to a summary file for viewing.

Getting PyExifTool

  1. PyExifTool PyExifTool is a Python library to communicate with an instance of Phil Harvey's excellent ExifTool command-line application.
  2. See full list on exiftool.org.

The source code can be checked out from the github repository with

Alternatively, you can download a tarball. There haven't been anyreleases yet.

Installation

PyExifTool runs on Python 2.6 and above, including 3.x. It has beentested on Windows and Linux, and probably also runs on other Unix-likeplatforms.

You need an installation of the exiftool command-line tool. Thecode has been tested with version 8.60, but should work with version8.40 or above (which was the first production version of exiftoolfeaturing the -stay_open option for batch mode).

PyExifTool currently only consists of a single module, so you cansimply copy or link this module to a place where Python finds it, oryou can call

to automatically install that module.

Documentation

Exiftool Python

The documentation is available athttp://smarnach.github.com/pyexiftool/.

Licence

PyExifTool is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the licence, or(at your option) any later version, or the BSD licence.

PyExifTool is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See COPYING.GPL or COPYING.BSD for more details.

Python Exiftool Subprocess

DigiKam and other image organizers are perfect for viewing and tagging images one by one. But if you need to quickly and safely move, copy, rename, reorganize, extract preview jpegs, and/or tag hundreds or thousands of images at once, ExifTool is the tool of choice.

Written around 2010. Minor update in March 2015.

Exiftool is a mature and reliable metadata reading and writing application. Although this article was written in 2010, Exiftool and the Exiftool syntax seldom change except to add new tags and new capabilities. I put this article up on my website mostly so I would have a quick reference for the Exiftool commands that I use the most often. This doesn't mean that all the command examples on this page still work exactly the same as they did when I first posted the article. But they probably do.

Install Exiftool

As always when working from the command line, do your homework before trusting any commands you find on the internet. And test, test, test before deploying.

Tags & groups: Where does image metadata come from?

More Exiftool Python images

The documentation is available athttp://smarnach.github.com/pyexiftool/.

Licence

PyExifTool is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the licence, or(at your option) any later version, or the BSD licence.

PyExifTool is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See COPYING.GPL or COPYING.BSD for more details.

Python Exiftool Subprocess

DigiKam and other image organizers are perfect for viewing and tagging images one by one. But if you need to quickly and safely move, copy, rename, reorganize, extract preview jpegs, and/or tag hundreds or thousands of images at once, ExifTool is the tool of choice.

Written around 2010. Minor update in March 2015.

Exiftool is a mature and reliable metadata reading and writing application. Although this article was written in 2010, Exiftool and the Exiftool syntax seldom change except to add new tags and new capabilities. I put this article up on my website mostly so I would have a quick reference for the Exiftool commands that I use the most often. This doesn't mean that all the command examples on this page still work exactly the same as they did when I first posted the article. But they probably do.

Install Exiftool

As always when working from the command line, do your homework before trusting any commands you find on the internet. And test, test, test before deploying.

Tags & groups: Where does image metadata come from?

If you've ever used digiKam, ExifTool or other software to look at your image metadata, you've noticed that there's a whole lot of it. Each individual bit of metadata information has a 'tag name'; the tag name lets you know which bit of metadata information you are looking at. As you may or may not already know, metadata included in image files comes from several different sources, each of which gets its own ExifTool Group:

  • 'File' information isn't metadata that is actually embedded in the image, rather it comes from system information.
  • Your camera writes EXIF (Exchangeable image file format) metadata, the standards for which are written by JEITA, the Japanese Electronics and Information Technology Indutries Association.
  • Your camera also generates proprietary (but mostly decoded) MakerNotes metadata.
  • IPTC metadata is written to satisfy requirements of the International Press Telecommunications Council's 'IPTC Information Interchange Model').
  • XMP metadata is metadata written according to Adobe's XMP standard.
  • Composite metadata is constructed for the ExifTool user's convenience, by combining other metadata. The list of composite metadata is surprisingly long and comprises some of the most useful metadata you can view, use, and modify at the command line using ExifTool.

Warning: ExifTool and digiKam (and other imaging softwares) don't always use the same tag and group names. As ExifTool was the first and is (imho) by far the most complete, safe, well-tested, well-documented, and up-to-date software you can use to manipulate metadata, I think it is confusing that lesser, later softwares (in the realm of metadata manipulation) don't follow ExifTool's lead in the matter of tag and group names. (But then again, I didn't write any of these other softwares, so who am I to complain?)

Basic exifTool commands for digital photography

  1. Examine your raw file's metadata

    The very most basic thing you can do with ExifTool is display your image file metadata. And if you've never peaked to see what is in there, you should; it is fascinating to see all the information hidden inside your digital camera raw file's metadata:

    exiftool /media/digikam/rawpeg/08/0807/080702_153429.cr2

    Below is a tiny excerpt from the terminal output you'd see if you ran the above command (replacing my image file name with your image file name, of course) on one of your digital camera raw files:

    File Modification Date/Time : 2011:01:20 12:02:35-05:00Modify Date : 2008:07:02 15:34:29Date/Time Original : 2008:07:02 15:34:29Create Date : 2008:07:02 15:34:29

    In my image raw files, the Modify Date, Date/Time Original, and Create Date are always the same. The File Modification Date/Time isn't part of the metadata embedded in the image; instead it is actually the time stamp in your operating system's file tree, which ExifTool displays for your convenience.

    The next command shows the same information but instead of using 'friendly' metadata names like 'Modify Date' it uses the metadata names that you would use when writing more complicated ExifTool commands

    exiftool -s -G /media/digikam/rawpeg/08/0807/080702_153429.cr2

    If you add the options '-s' and '-G' after the word 'exiftool' (as above), you'll see:

    [File] FileModifyDate : 2011:01:20 12:02:35-05:00[EXIF] ModifyDate : 2008:07:02 15:34:29[EXIF] DateTimeOriginal : 2008:07:02 15:34:29[EXIF] CreateDate : 2008:07:02 15:34:29
    • -s means instead of showing the 'friendly' names like 'Create Date', show the names you need to use when you write ExifTool commands. So instead of 'Create Date' you see 'CreateDate'.
    • -G means also show the metadata Group to which the metadata tag belongs.
  2. Rename image files according to their creation date

    The following command renames all 'cr2' (Canon) and 'mrw' (Minolta) raw files, in the 'test' folder and subfolders, according to the metadata Create Date and Time. Of course, to use this command (and all the following commands) you need to replace '/media/ingest/test' (or 'media/ingest/oldfolder', etc) with your own actual filepath and folder name.

    exiftool '-filename
    • '-filename means rename the image file using the image's creation date and time.
    • -d means 'Set format for date/time values'.
    • %y%m%d_%H%M%S%%-c.%%le, used in conjunction with '-d' specifies the format to use for the date and time when renaming the file. Breaking the format down:
    • %y%m%d_ means the first part of the new file name should be composed of the last two digits of the creation-date year, followed by the month and day, both represented by two digits. The underscore _ means put in an underscore after the date part of the file name.
    • %H%M%S means add the hour, minute, and second of the creation time, all represented by two digits.
    • %%-c means that if two images have the same file name up to this point in the naming process, add 'a copy number which is automatically incremented' to give each image a unique name. Note the doubled %% — necessary because of something called 'escaping' that I don't fully understand. The '-' before the 'c' isn't really necessary, but it puts a dash before the copy number.
    • .%%le means keep the original file name extension, but make it lower-case if it was originally upper-case, a nice option when cameras insist on using 'CR2' instead of 'cr2'. (If you prefer upper-case extensions, then use .%%ue. If you prefer to keep the original case intact, use .%%e.)
    • -ext cr2 -ext mrw means only rename files with the 'cr2' or 'mrw' extension. To rename all image files in the source folder, don't specify any extensions.
    • -r means 'execute this command recursively for every image file in the top 'source' folder (that is, the folder where all the files to be renamed are located), and also for the image files in all the source folder's subfolders, sub-subfolders, and so on'.
    • /media/ingest/test is the absolute path to the top folder holding all the images to be renamed (your path will be different, of course).

    Additional notes on renaming files with ExifTool

    Raw file metadata typically contains (at least) four different times for each image file. In the above command, if there is no creation date information for a given file, ExifTool writes an error to the terminal. If you explore the ExifTool documentation you'll find ways to specify alternatives in case one time is missing from the metadata but another time is available.

    ExifTool is not limited to renaming files based on dates. ExifTool can rename (and also copy or move) an image based on just about any bit of metadata that happens to be embedded in the image.

    When you rename, copy, move, or otherwise modify images with ExifTool, there is a neat command line option '-P' that 'preserves' the File Modification Date/Time, which is nice if the file modification date and time happens to be the actual date and time the image was created on the camera card.

  3. Move or Copy image files into folders by year and month

    Move:

    exiftool '-Directory

    Copy:

    exiftool -o . '-Directory

    The two lines of code above move or copy a whole bunch of files from one place to another. The only difference between the two commands is the 'copy' command has one additional option: '-o . ', which means 'copy over, leaving the source files in place'.

    • '-Directory means that you want the destination folders to which the images will be move to be named using the image creation date.
    • -d means 'Set format for date/time values'.
    • /media/ingest/newfolder/%y/%y%m is the path to the folders to which the images will be moved or copied. 'newfolder' will be the root folder for the folders created according to the image creation date. The '/%y/%y%m' part specifies the format to use when creating the subfolders. In this example, there will be 2-digit 'year' folders, each containing 4-digit 'yearmonth' subfolders.
    • -r means act recursively over the source folder and its subfolders (just as in the example command above for renaming files).
    • /media/ingest/oldfolder is the source folder. Before running the move or copy command, the images are all located in this folder.


    Exiftool move and copy notes:

    The originating folder, 'oldfolder' above, may or may not have subfolders, and these subfolders can be anything (jpegs in one folder, raws in another, sorted by camera card, whatever). If 'oldfolder' does have subfolders, the subfolders are traversed because I used the '-r' option. To avoid traversing the subfolders in 'oldfolder', don't use the '-r' option.

    ExifTool creates any folders it needs as it executes the command. In other words, if 'newfolder' doesn't exist, ExifTool will create it. If, for example, an image in 'oldfolder' has a creation date of 2011-Jan-01, ExifTool will create a folder named '11' under 'newfolder', and a folder named '1101' under '11'.

    See the first picture on this page for a picture of the resulting '/yy/yymm' file tree.

  4. Extract the preview image embedded in raw files

    Most (all?) raw files have one or more embedded images (small jpegs that show what the raw file will more or less look like after raw processing). One of these images is an embedded preview image. This embedded preview image is the same image that digiKam or Geeqie show you when you preview raw files. ExifTool can write that embedded preview image out as a separate image file.

    The excerpt below (from the results of running 'exiftool -s -G /path/to/imagefile' at the command line) shows information about two of the three binary image blobs that are embedded in my Canon cr2 files:

    [Composite] PreviewImage: (Binary data 377134 bytes, use -b option to extract)(. . . other intervening metadata . . .)[Composite] ThumbnailImage: (Binary data 6391 bytes, use -b option to extract)

    Don't worry — the preview image is still embedded in the raw file after running the following command:

    exiftool -b -PreviewImage -w _preview.jpg -ext cr2 -r /media/images/digikam/rawpeg
    • '-b' means 'Output data in binary format' (a digital image is a type of 'binary blob' — if you want to know more, you are on your own).
    • '-PreviewImage' means you are interested in the PreviewImage embedded in the raw file.
    • '-w' means 'write out a copy of the embedded binary image blob as a separate image file'.
    • '_preview.jpg' means 'write the image file as a jpeg file with '-preview' appended to the end of the original raw file's base file name'.
    • '-ext cr2' means 'only execute this command for image files that have the extension 'cr2'.
    • '-r' means 'execute this command recursively for every file in the top folder and all subfolders'.
    • '/media/images/digikam/rawpeg' is the absolute path to the top folder for the hierarchy of folders I want ExifTool to traverse as it writes out the preview image.

    Notes on extracting the embedded preview

    You should move and rename your raw files before writing out the embedded jpegs. Else you are very likely to produce jpeg preview files with names that don't match their corresponding raw files.

    ExifTool has been capable of safely and efficiently writing metadata to raw files for several years. Nonetheless, test, test, test before turning ExifTool loose on your precious raw files.

    Adobe originated the idea of the XMP sidecars to hold metadata and other information. But I've never liked XMP sidecars because there is no immediate visual confirmation that the XMP file really goes with the corresponding raw file. I also don't trust the ability of image organizing programs like digiKam (or any other image organizer — the forums are too full of people lamenting that they just lost their metadata) to safely write to my raw files. So I use ExifTool to extract the preview jpegs that are embedded in raw files to serve as handy jpeg 'sidecars' to all my raw files. Then when I'm done tagging and rating my jpeg sidecar files with digiKam, I use ExifTool to copy the sidecar metadata over to the corresponding raw file.

  5. Copy metadata from the raw file over to the corresponding preview jpeg sidecar

    Once you've extracted the preview jpeg, you might like to copy all the metadata from the corresponding raw file over to the preview jpeg file:

    exiftool -tagsfromfile %d%-.8f.cr2 -all:all -r -overwrite_original -ext jpg /media/images/digikam/rawpeg
    • '-tagsfromfile' means copy metadata from the source file (the raw files) to the destination files (the preview jpegs). See the ExifTool documentation for how to specify which tags to copy if you don't want them all.
    • '%d%-.8f.cr2' is a bit tricky. It means look for files with the 'cr2' extension and the same name as the destination preview jpeg files, but without the last 8 characters of the name (the '-preview' part of the name).
    • '-all:all' is also tricky. These days there are 'preferred' ways to store metadata according to the people who are working on metadata standards. Metadata is arranged in various groups (exif, iptc, xmp, maker notes, and more). '-all:all' means write all the metadata AND keep all the metadata exactly as grouped and written in the source file (regardless of whether the source file uses the currently 'preferred' way to store metadata). Consult the ExifTool documentation for more details.
    • '-overwrite_original' means what it says. Without this option, instead ExifTool would save the existing preview jpegs under the name 'filename.extension.original' and would write out a new file under the original file name and extension. You should always be sure you know what you are doing (and have back-ups) if you use the 'overwrite_original' option. However, in this case, letting ExifTool write out new files for all the just-written-out preview jpegs seems silly.


    How much raw file metadata do you want in your jpeg 'sidecars'

    ExifTool doesn't automatically copy over any metadata from the raw file to the extracted preview. You have to decide how much (if any) metadata you want to copy over. 'All the metadata' is probably overkill, but it was the easy, so I did.

    Is it safe to let ExifTool overwrite raw files?

    In my opinion, ExifTool is VERY safe to use. But your images and your metadata are also VERY important. Keep back-ups until you are sure everything happened exactly as you wanted it to happen. Keep back-ups anyway. Other softwares beside ExifTool write image metadata — you never know when a backup will save you from retagging thousands of images.

    ExifTool and XML sidecar files

    One last note: I don't like XML sidecar files. But I'm not you. If you use XML sidecar files, of course ExifTool can read, write, copy, etc XML sidecar metadata just as easily as jpeg and raw file metadata.

  6. Copy hierarchical tags written by digiKam from the preview jpeg sidecar files over to the corresponding raw files

    Once you've used digiKam or other image organizing software to add tags and ratings and captions and so forth to a whole bunch of your preview jpeg 'sidecar' files, you might like to copy the preview jpeg metadata back over to the corresponding raw file. The following command line assumes that the raw files and their sidecars are located in the same folder, with the same base name:

    exiftool -tagsfromfile %d%f_preview.jpg -TagsList -r -overwrite_original -ext mrw /media/b3/newfolder/03

    You've already seen most of the options in the above command used in previous commands.

    • -TagsList tells ExifTool to only copy the particular metadata tag called '-TagsList' from one file to another. If you also wanted several other specific tags to be copied over, you'd just add them to the command.
    • %d%f_preview.jpg works together with -ext cr2 as follows:
    • %d%f means 'all files in folder 'd' with file name 'f'.
    • %d%f_preview.jpg is the name of the source file, the file from which the metadata will be copied. -ext cr2 indicates the destination file, the file to which the copied metadata will be written.
    • The way the two parts of the command work together is that %d%f is the file name of both the source and destination file, but compared to the destination file, the source file (the preview jpeg sidecar), has '_preview' added to its file name.
  7. Set the orientation tag to normal for all images in a folder and its subfolders

    The command below is probably the trickiest command on this page and begins to reveal some of the real power of ExifTool. It tells ExifTool to open every writable image file contained in the folder 'digikam' and all its subfolders, and check see if the '-if' condition is met; if so, do something, if not, don't do anything.

    exiftool -n -r -overwrite_original -if '$Orientation ne 1' -Orientation=1 /media/images/digikam
    • '-n' is an interesting option you can use when working with ExifTool — it tells ExifTool to not translate the orientation information to words, rather leave it as an integer (in the metadata specification, the orientation tag is just an integer). Without the '-n' I would have needed to write something like 'Horizontal (normal)' instead of '1', and I am too lazy to figure out exactly what words are needed or how to properly enclose those words in quotes to let ExifTool know what I want it to do. The '-n' makes writing this particular command a lot easier.
    • As you might expect, other metadata tags also have an official way of writing the information that is not particularly communicative unless translated into something more people-friendly. For those tags, the '-n' can be very helpful if you want to see what is 'really' in the metadata.
    • '-if '$Orientation ne 1' is really cool. It says, 'see if this condition is met: if so, do something, if not, don't do anything'. The '$Orientation' part means 'the metadata tag that goes by the name 'Orientation'. 'ne' means 'not equal', but you probably already figured that out.
    • '-Orientation=1' means that if '-if '$Orientation ne 1' is true, then you want ExifTool to write the Orientation tag to the file, with the value '1' replacing whatever the previous value happened to be.

Fear and loathing at the command line

One reason the command line is scary is because it is unfamiliar. The more important reason is because if you issue the wrong command, you can wreck utter havoc on your file system, scattering your digital images all over the place. You can erase all your metadata or render it meaningless with one short, innocuous-looking command.

Exiftool Python Cheat

Stepping outside the realm of ExifTool per se, with the wrong command (such as 'rm -R', which you should never, ever use unless you know exactly what you are doing), you can erase your entire hard drive or damage system files beyond repair. If you don't know what a command is supposed to do, your very first line of defense is consulting a man page. For example, for information about 'rm', do an internet search on 'manpage rm'. And make sure you trust the man page you consult. I like the die.net man pages.

Test, test, test, and then test some more on sample data before turning ExifTool (or any other command line program) loose on your precious digital images. After all, everyone makes typos, including me. So if you blindly follow my examples, you get my typos.

GitHub - Exiftool/exiftool: ExifTool Meta Information Reader ...

Back up your original files and folders before making bulk changes with ExifTool. It is far better to have a back-up than a whole bunch of regrets in case you change your mind or make a mistake. To be on the super, super safe side, either put the back-up on an external drive and disconnect the drive, or make the back-up 'read-only' and transfer ownership to root — that way you won't accidentally wander into the wrong folder and mess up your back-up images.





broken image