Useful Python by Langridge Stuart;

Useful Python by Langridge Stuart;

Author:Langridge, Stuart; [Stuart Langridge]
Language: eng
Format: epub
ISBN: 978-1-925836-57-8
Publisher: SitePoint
Published: 2022-04-06T23:00:00+00:00


For this, we’ll need to read the metadata from Python, and the best way to do any image manipulation is with Pillow, the Python Imaging Library. The metadata we want here is called IPTC, defined in extreme detail by the International Press Telecommunications Council in the IPTC Photo Metadata Standard. In particular, we want the image’s title tag, with code 2:120. We can define a convenient variable for that:

IPTC_Title = (2, 120)

Next, let’s loop over each of our images. Python’s glob module is useful for this. A glob (a term from Unix prehistory, short for “global”) is a pattern using a wildcard character and file specification to gather a list of all files matching that pattern. (For example, *.txt will match a list of files such as myfile.txt, 1.txt, everything.txt, and so on.) In Python, glob.glob("*.txt") does the same thing. It returns a list of matching filenames, which we can then iterate over. We can also open each one with the Python Imaging Library:

import glob, os from PIL import Image for image_filename in glob.glob("*.jpg"): im = Image.open(image_filename)

Here, we have a reference to each image. Pillow can do all sorts of image manipulations, such as resizing, changing the content, saving in different formats, and so on. But for now, all we want to do is read the metadata. For that, we use Pillow’s IptcImagePlugin, and we can have it print out the relevant image title:

from PIL import Image, IptcImagePlugin for image_filename in glob.glob("*.jpg"): im = Image.open(image_filename) iptc = IptcImagePlugin.getiptcinfo(im) im.close() title = iptc[IPTC_Title].decode("utf-8")

Now that we know the title for a given image, we can use it to construct a new filename, and then use os.rename to rename the file to this new name. The os.rename function does more than simply renaming; it can also move a file to a different directory or different disk. But in this case, we’re only renaming the file in place:

destination_filename = f"{title}.jpg" print(f"Renaming {image_filename} -> {destination_filename}") if os.path.exists(destination_filename): print(f"{destination_filename} already exists; skipping") else: os.rename(image_filename, destination_filename)

We’re being a little careful here. If the destination_filename already exists, then os.rename can—depending on our OS—either overwrite it without warning or throw an exception. So we check beforehand whether that file exists, and if it does, we assume that we’ve already handled this image and therefore skip it.

Now, a list of the directory shows the files all renamed according to their titles. We could look in the file manager for this, but we’re in Python, so let’s use Python for it:

>>> import os >>> os.listdir() ['avocado.jpg', 'kiwifruit.jpg', 'lemon.jpg', 'apple.jpg', 'strawberry.jpg', 'rename-images.py', 'orange.jpg']



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.