Created
October 7, 2025 21:00
-
-
Save me-suzy/faca2a7c7db3524ca888f959e6b0f02a to your computer and use it in GitHub Desktop.
convert tiff to jpg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from pathlib import Path | |
def tiff_to_jpg(folder_input, folder_output=None): | |
""" | |
Convertește toate TIFF-urile în JPG cu calitate maximă. | |
""" | |
folder_input = Path(folder_input) | |
if folder_output is None: | |
folder_output = folder_input / "jpg" | |
else: | |
folder_output = Path(folder_output) | |
folder_output.mkdir(exist_ok=True) | |
# Găsește toate TIFF-urile | |
fisiere_tiff = sorted(list(folder_input.glob("*.tif")) + | |
list(folder_input.glob("*.tiff")) + | |
list(folder_input.glob("*.TIF")) + | |
list(folder_input.glob("*.TIFF"))) | |
if not fisiere_tiff: | |
print("❌ Nu s-au găsit fișiere TIFF!") | |
return | |
print(f"📁 Folder input: {folder_input}") | |
print(f"📊 Găsite {len(fisiere_tiff)} fișiere TIFF") | |
print(f"💾 Output JPG: {folder_output}\n") | |
print("=" * 70) | |
succese = 0 | |
for i, fisier_tiff in enumerate(fisiere_tiff, 1): | |
print(f"[{i}/{len(fisiere_tiff)}] {fisier_tiff.name}...", end=" ", flush=True) | |
try: | |
# Deschide TIFF | |
img = Image.open(fisier_tiff) | |
# Convertește în RGB dacă e necesar | |
if img.mode == 'RGBA': | |
# Creează fundal alb pentru transparență | |
background = Image.new('RGB', img.size, (255, 255, 255)) | |
background.paste(img, mask=img.split()[3] if len(img.split()) == 4 else None) | |
img = background | |
elif img.mode not in ('RGB', 'L'): | |
img = img.convert('RGB') | |
# Nume fișier JPG | |
nume_jpg = fisier_tiff.stem + ".jpg" | |
cale_jpg = folder_output / nume_jpg | |
# Salvează cu calitate maximă | |
# quality=100: calitate maximă | |
# subsampling=0: fără subsampling (calitate maximă) | |
# optimize=True: optimizează fișierul | |
img.save(cale_jpg, 'JPEG', | |
quality=100, | |
subsampling=0, | |
optimize=True, | |
dpi=img.info.get('dpi', (300, 300))) | |
print("✓") | |
succese += 1 | |
except Exception as e: | |
print(f"✗ Eroare: {e}") | |
print("\n" + "=" * 70) | |
print(f"✓ CONVERSIE COMPLETĂ!") | |
print(f"✓ {succese}/{len(fisiere_tiff)} fișiere convertite cu succes") | |
print(f"✓ JPG-uri salvate în: {folder_output}") | |
# RULEAZĂ | |
tiff_to_jpg(r"g:\Hidrotehnica 1960 (Anul 5, nr. 1-12)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment