pee2pee
pee2pee

Reputation: 3802

Image exif/metadata reading/writing?

Been looking around as there still doesn't appear to be a well documented and up-to-date for image exif/metadata manipulation.

I have the following code using piexif

import os
import piexif

for root, dirs, files in os.walk("C:\\Users\\Alan Partridge\\Desktop\\images"):
    if not files:
        continue
    prefix = os.path.basename(root)
    for f in files:
        #print os.path.join(root, f)
        exif_dict = piexif.load(os.path.join(root, f))
        print exif_dict
        for ifd in ("0th", "Exif", "GPS", "1st"):
            for tag in exif_dict[ifd]:
                print(piexif.TAGS[ifd][tag]["name"], exif_dict[ifd][tag])
        #os.rename(os.path.join(root, f), os.path.join(root, "{} ID {}".format(prefix, f)))

It loops through folders renaming files depending on their folder name (commented out) but for now, it outputs whatever data the library can find.

At the moment, it can find ImageDescription, which is fine. My question is how would I go about amending it? Unfortunately I can't make head nor take of how to do this. Maybe I'm missing something obvious?

Upvotes: 1

Views: 5227

Answers (2)

lazyList
lazyList

Reputation: 571

Image Exif information can be found in this document:

http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

To use piexif library to build exif information, and write to a destination file: (python)

Build exif info to preserve orientation from source file , which is read by windows to display it correct direction.

source: http://piexif.readthedocs.io/en/latest/functions.html?highlight=save

zeroth_ifd = {piexif.ImageIFD.Orientation: image_orientation,
              piexif.ImageIFD.ImageWidth: int(new_width),
              piexif.ImageIFD.ImageLength: int(new_height),
              piexif.ImageIFD.Software: u"piexif"
              }

exif_dict = {"0th":zeroth_ifd}
exif_bytes = piexif.dump(exif_dict)

with open(dest, 'r+b') as f:
    with Image.open(dest,'r') as image:
        image.save(dest, "jpeg", exif=exif_bytes)

Upvotes: 0

mcepl
mcepl

Reputation: 2786

Take a look at samples at https://piexif.readthedocs.io/en/latest/sample.html . You are missing whole piexif.dump() and img.save() operation.

Upvotes: 0

Related Questions