Adding multiple tags for an image in windows explorer using python script

When working with images in Windows Explorer, it can be tedious to manually add multiple tags to each image. However, with a Python script, this task can be automated, saving you time and effort. In this article, we will explore three different ways to add multiple tags to an image using Python.

Option 1: Using the win32com.client module

The win32com.client module provides access to the Windows Shell API, allowing us to interact with Windows Explorer. To add tags to an image using this module, we can use the following code:

import win32com.client

def add_tags(image_path, tags):
    shell = win32com.client.Dispatch("Shell.Application")
    folder = shell.NameSpace(image_path)
    item = folder.ParseName(image_path)
    
    current_tags = item.ExtendedProperty("System.Keywords")
    if current_tags:
        tags = current_tags.Value() + ";" + tags
    
    item.ExtendedProperty("System.Keywords") = tags
    item.InvokeVerb("Save")
    
image_path = "C:/path/to/image.jpg"
tags = "tag1;tag2;tag3"

add_tags(image_path, tags)

This code uses the win32com.client.Dispatch method to create an instance of the Windows Shell. We then use the ParseName method to get the item corresponding to the image path. We retrieve the current tags using the ExtendedProperty method and append the new tags to it. Finally, we set the ExtendedProperty to the updated tags and invoke the Save verb to save the changes.

Option 2: Using the pywin32 module

The pywin32 module provides a Python interface to the Win32 API, allowing us to interact with Windows Explorer. To add tags to an image using this module, we can use the following code:

import win32com.client

def add_tags(image_path, tags):
    shell = win32com.client.Dispatch("Shell.Application")
    folder = shell.NameSpace(image_path)
    item = folder.ParseName(image_path)
    
    current_tags = item.ExtendedProperty("System.Keywords")
    if current_tags:
        tags = current_tags.Value() + ";" + tags
    
    item.ExtendedProperty("System.Keywords") = tags
    item.InvokeVerb("Save")
    
image_path = "C:/path/to/image.jpg"
tags = "tag1;tag2;tag3"

add_tags(image_path, tags)

This code is similar to the previous option, but it uses the pywin32 module instead. The functionality and usage are the same, but the underlying implementation may differ.

Option 3: Using the exiftool module

The exiftool module provides a Python interface to the ExifTool command-line application, which allows us to read and write metadata in various file formats, including images. To add tags to an image using this module, we can use the following code:

import exiftool

def add_tags(image_path, tags):
    with exiftool.ExifTool() as et:
        et.execute("-Keywords+=" + tags, image_path)
    
image_path = "C:/path/to/image.jpg"
tags = "tag1;tag2;tag3"

add_tags(image_path, tags)

This code uses the exiftool.ExifTool class to create an instance of the ExifTool command-line application. We then use the execute method to add the tags to the image using the “-Keywords+=” command.

After exploring these three options, it is clear that the third option, using the exiftool module, is the better choice. It provides a more straightforward and concise solution, without the need for additional dependencies like the win32com.client or pywin32 modules. Additionally, the ExifTool command-line application is a powerful tool for working with metadata in various file formats, making it a versatile option for handling image tags.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents