58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import os
|
|
from PIL import Image
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
class Converter:
|
|
def convert(self, original_format, target_format, path, mode):
|
|
target_format = target_format.lower()
|
|
original_format = original_format.lower()
|
|
|
|
SUPPORTED_FORMATS = {"jpg", "jpeg", "png", "webp", "bmp", "tiff", "gif"}
|
|
if target_format not in SUPPORTED_FORMATS:
|
|
print(f"Unsupported format: {target_format}")
|
|
return
|
|
|
|
if mode == "single":
|
|
img = Image.open(path)
|
|
filename, _ = os.path.splitext(path)
|
|
|
|
if target_format in ("jpg", "jpeg"):
|
|
img = img.convert('RGB')
|
|
img.save(f"{filename}.{target_format}", "JPEG")
|
|
else:
|
|
img.save(f"{filename}.{target_format}", target_format.upper())
|
|
|
|
print("Done!")
|
|
|
|
elif mode == "mass":
|
|
result_dir = os.path.join(path, "result")
|
|
os.makedirs(result_dir, exist_ok=True)
|
|
|
|
images = [
|
|
f for f in os.listdir(path)
|
|
if f.lower().endswith(f".{original_format}")
|
|
]
|
|
|
|
print("In queue:", images, "\n")
|
|
|
|
def process_image(filename):
|
|
try:
|
|
img = Image.open(os.path.join(path, filename))
|
|
name, _ = os.path.splitext(filename)
|
|
save_path = os.path.join(result_dir, f"{name}.{target_format}")
|
|
|
|
if target_format in ("jpg", "jpeg"):
|
|
img = img.convert("RGB")
|
|
img.save(save_path, "JPEG")
|
|
else:
|
|
img.save(save_path, target_format.upper())
|
|
|
|
print(f"{filename} Done!")
|
|
except Exception as e:
|
|
print(f"{filename} failed: {e}")
|
|
|
|
with ThreadPoolExecutor() as executor:
|
|
executor.map(process_image, images)
|
|
|
|
converter = Converter()
|