Last active
May 13, 2025 04:36
-
-
Save me-suzy/aaffea93dbb1a7a658100e13455d3b8d to your computer and use it in GitHub Desktop.
Pentru PDF (folosind PyPDF2 și reportlab)
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 PyPDF2 import PdfReader, PdfWriter | |
from reportlab.pdfgen import canvas | |
from reportlab.lib.pagesizes import letter | |
import io | |
def add_old_paper_background(input_pdf, output_pdf): | |
# Citește PDF-ul original | |
reader = PdfReader(input_pdf) | |
writer = PdfWriter() | |
for page_num in range(len(reader.pages)): | |
page = reader.pages[page_num] | |
# Creează un background gălbui | |
packet = io.BytesIO() | |
c = canvas.Canvas(packet, pagesize=letter) | |
# Setează culoarea de fundal | |
c.setFillColorRGB(0.98, 0.97, 0.89) # Galben deschis | |
c.rect(0, 0, letter[0], letter[1], fill=1) | |
c.save() | |
# Combină fundalul cu pagina originală | |
packet.seek(0) | |
background = PdfReader(packet).pages[0] | |
background.merge_page(page) | |
writer.add_page(background) | |
# Salvează PDF-ul modificat | |
with open(output_pdf, 'wb') as output_file: | |
writer.write(output_file) | |
--------- | |
import os | |
from pathlib import Path | |
from PIL import Image, ImageEnhance | |
from PyPDF2 import PdfReader, PdfWriter | |
from reportlab.pdfgen import canvas | |
from reportlab.lib.pagesizes import letter, A4 | |
import io | |
def make_old_book_style_image(image_path, output_path): | |
"""Transformă o imagine într-un stil de carte veche cu fundal gălbui""" | |
try: | |
# Deschide imaginea | |
img = Image.open(image_path) | |
# Convertește la RGB dacă nu este deja | |
if img.mode != 'RGB': | |
img = img.convert('RGB') | |
# Obține dimensiunile | |
width, height = img.size | |
# Creează o imagine cu culoarea de fundal dorită (galben deschis) | |
old_paper = Image.new('RGB', (width, height), (250, 245, 220)) | |
# Combină imaginea originală cu fundalul gălbui | |
result = Image.blend(old_paper, img, 0.85) | |
# Ajustează contrastul pentru text mai citet | |
enhancer = ImageEnhance.Contrast(result) | |
result = enhancer.enhance(1.15) | |
# Ajustează culoarea pentru un efect mai cald | |
enhancer = ImageEnhance.Color(result) | |
result = enhancer.enhance(0.9) | |
# Ajustează luminozitatea | |
enhancer = ImageEnhance.Brightness(result) | |
result = enhancer.enhance(0.95) | |
# Salvează rezultatul | |
result.save(output_path, quality=95) | |
return True | |
except Exception as e: | |
print(f"Eroare la procesarea imaginii: {e}") | |
return False | |
def add_old_paper_background_pdf(input_pdf_path, output_pdf_path): | |
"""Adaugă un fundal de carte veche la un PDF""" | |
try: | |
# Citește PDF-ul original | |
reader = PdfReader(input_pdf_path) | |
writer = PdfWriter() | |
for page_num in range(len(reader.pages)): | |
page = reader.pages[page_num] | |
# Obține dimensiunea paginii | |
page_width = float(page.mediabox[2]) | |
page_height = float(page.mediabox[3]) | |
# Creează un background gălbui | |
packet = io.BytesIO() | |
c = canvas.Canvas(packet, pagesize=(page_width, page_height)) | |
# Setează culoarea de fundal (galben deschis/crem) | |
c.setFillColorRGB(0.98, 0.96, 0.86) # Culoare gălbuie ca de carte veche | |
c.rect(0, 0, page_width, page_height, fill=1) | |
# Adaugă un gradient subtil pentru efect mai realist | |
c.setFillColorRGB(0.95, 0.93, 0.83, alpha=0.3) | |
c.rect(0, 0, page_width * 0.1, page_height, fill=1) | |
c.rect(page_width * 0.9, 0, page_width * 0.1, page_height, fill=1) | |
c.save() | |
# Combină fundalul cu pagina originală | |
packet.seek(0) | |
background_pdf = PdfReader(packet) | |
background_page = background_pdf.pages[0] | |
background_page.merge_page(page) | |
writer.add_page(background_page) | |
# Salvează PDF-ul modificat | |
with open(output_pdf_path, 'wb') as output_file: | |
writer.write(output_file) | |
return True | |
except Exception as e: | |
print(f"Eroare la procesarea PDF-ului: {e}") | |
return False | |
def process_all_files(input_folder): | |
"""Procesează toate fișierele imagine și PDF dintr-un folder""" | |
input_path = Path(input_folder) | |
# Creează subdirectoare pentru rezultate | |
images_output_path = input_path / "old_style_images" | |
pdfs_output_path = input_path / "old_style_pdfs" | |
images_output_path.mkdir(exist_ok=True) | |
pdfs_output_path.mkdir(exist_ok=True) | |
# Tipuri de fișiere | |
image_extensions = ['.jpg', '.jpeg', '.png', '.tiff', '.tif', '.bmp'] | |
# Statistici | |
images_processed = 0 | |
pdfs_processed = 0 | |
errors = 0 | |
print(f"Procesez fișierele din: {input_folder}") | |
print(f"Imaginile vor fi salvate în: {images_output_path}") | |
print(f"PDF-urile vor fi salvate în: {pdfs_output_path}") | |
print("-" * 60) | |
# Parcurge toate fișierele din folder | |
for file_path in input_path.iterdir(): | |
if file_path.is_file(): | |
# Procesează imagini | |
if file_path.suffix.lower() in image_extensions: | |
output_file = images_output_path / file_path.name | |
print(f"Procesez imaginea: {file_path.name}") | |
if make_old_book_style_image(file_path, output_file): | |
images_processed += 1 | |
print(f" ✓ Salvat: {output_file.name}") | |
else: | |
errors += 1 | |
print(f" ✗ Eroare la procesare") | |
# Procesează PDF-uri | |
elif file_path.suffix.lower() == '.pdf': | |
output_file = pdfs_output_path / file_path.name | |
print(f"Procesez PDF-ul: {file_path.name}") | |
if add_old_paper_background_pdf(file_path, output_file): | |
pdfs_processed += 1 | |
print(f" ✓ Salvat: {output_file.name}") | |
else: | |
errors += 1 | |
print(f" ✗ Eroare la procesare") | |
print() | |
print("-" * 60) | |
print(f"Procesare completă!") | |
print(f"Imagini procesate: {images_processed}") | |
print(f"PDF-uri procesate: {pdfs_processed}") | |
print(f"Erori totale: {errors}") | |
print(f"\nRezultatele sunt în:") | |
print(f" - Imagini: {images_output_path}") | |
print(f" - PDF-uri: {pdfs_output_path}") | |
if __name__ == "__main__": | |
# Procesează toate fișierele din d:\77\ | |
process_all_files(r"d:\77") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment