import os | |
import glob | |
from PIL import Image | |
def convert_high_bit_depth_images(root_dir, threshold_bit_depth): | |
print(f"Searching for images in {root_dir} and its subdirectories...") | |
for filepath in glob.iglob(os.path.join(root_dir, '**/*.(jpg|jpeg|png)'), recursive=True): | |
print(f"Found image: {filepath}") | |
with Image.open(filepath) as img: | |
bit_depth = img.bits | |
print(f"Image bit depth: {bit_depth} bits") | |
if bit_depth > threshold_bit_depth: | |
print(f"Converting image to 16-bit: {filepath}") | |
img = img.convert('I;16') | |
img.save(filepath) | |
print(f"Conversion complete: {filepath}") | |
else: | |
print(f"Image bit depth is already 16-bit or lower: {filepath}") | |
print("Conversion complete!") | |
def main(): | |
root_dir = 'E:\\training_dir' | |
threshold_bit_depth = 16 | |
convert_high_bit_depth_images(root_dir, threshold_bit_depth) | |
if __name__ == "__main__": | |
main() | |