- 1 year ago
Adding multiple 'Tags' for an image in windows explorer using python script
To add multiple tags to an image in Windows Explorer using a Python script, you can make use of the pythoncom
and win32com.client
modules to interact with the Windows Shell. Here's an example script:
pythonimport pythoncom
import win32com.client
def add_tags_to_image(image_path, tags):
shell = win32com.client.Dispatch("Shell.Application")
namespace = shell.Namespace(image_path)
item = namespace.ParseName(image_path)
# Get the PropertyStore for the image
property_store = item.ExtendedProperty("System.Photo.Tags")
# Convert the tags to a comma-separated string
tags_string = ",".join(tags)
# Set the tags property value
property_store.Value = tags_string
# Save the changes
property_store.Commit()
# Example usage
image_path = "C:\\path\\to\\image.jpg"
tags = ["Tag1", "Tag2", "Tag3"]
# Add the tags to the image
add_tags_to_image(image_path, tags)
In this example, the add_tags_to_image
function takes the path to the image and a list of tags as input. It uses the Windows Shell to access the image file and set the System.Photo.Tags
property to the specified tags. The changes are then saved using the Commit
method of the property store.
Note that this script requires the pythoncom
and pywin32
modules to be installed. You can install them using the following command:
pip install pywin32
Make sure to replace "C:\\path\\to\\image.jpg"
with the actual path to your image file, and provide the desired tags in the tags
list.
Once you run the script, the specified tags will be added to the image file in Windows Explorer.