How to move the images in the folder then copy it into separate folder respective with the refDes in the image description

Question:

Imagine a situation that there is a lot of tiff images in the folder and need to sort it and move to respective folder by read inside content of the tiff image (Image description)

When I tried to code that make it read the image description in the tiff images and want to move the image corresponding to the refdes info in the image, it shows this type of error…

File "", line 225, in makedirs
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect

Here is the sample tiff images im working on it, It’s GDrive link

Note that output_folder is to let me easy to navigate the output folder whereby inside of the output folder is refdes naming folder
This is the code im working on it

import os
import shutil
import tifffile

folder_path = "Input"
output_folder = "Output"

results = []

for filename in os.listdir(folder_path):
    if filename.endswith(".tif") or filename.endswith(".tiff"):
        file_path = os.path.join(folder_path, filename)
        with tifffile.TiffFile(file_path) as tif:
            description = tif.pages[0].description
            metadata = dict(tuple(line.split('=', 1)) for line in description.splitlines())

            result_dict = {
                "filename": filename,
                "integrationLevel": metadata['integrationLevel'],
                "binningMode": metadata['binningMode'],
                "resolution": metadata['resolution'],
                "refDes": metadata['refDes']
            }
            results.append(result_dict)

for result in results:
    refDes_folder = os.path.join(output_folder, result["refDes"])
    if not os.path.exists(refDes_folder):
        os.makedirs(refDes_folder)

    shutil.copy2(os.path.join(folder_path, result["filename"]), refDes_folder)

print("Done")

Asked By: Neko Lim

||

Answers:

You can replace any invalid characters with a valid character, such as an underscore (_), before creating the directory. You can do this using the re.sub() method from the re module, which allows you to replace all occurrences of a pattern in a string using a replacement string.

Here’s an example of how you can modify your code to replace any invalid characters in the refDes value before creating the directory:

import os
import re
import shutil
import tifffile

folder_path = "Input"
output_folder = "Output"

results = []

for filename in os.listdir(folder_path):
    if filename.endswith(".tif") or filename.endswith(".tiff"):
        file_path = os.path.join(folder_path, filename)
        with tifffile.TiffFile(file_path) as tif:
            description = tif.pages[0].description
            metadata = dict(tuple(line.split('=', 1)) for line in description.splitlines())

            result_dict = {
                "filename": filename,
                "integrationLevel": metadata['integrationLevel'],
                "binningMode": metadata['binningMode'],
                "resolution": metadata['resolution'],
                "refDes": metadata['refDes']
            }
            results.append(result_dict)

for result in results:
    # Replace any invalid characters in the refDes value with an underscore
    refDes_folder = os.path.join(output_folder, re.sub(r'[\/:*?"<>|]', '_', result["refDes"]))
    if not os.path.exists(refDes_folder):
        os.makedirs(refDes_folder)

    shutil.copy2(os.path.join(folder_path, result["filename"]), refDes_folder)

print("Done")
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.