thumbnail , ...

This commit is contained in:
hsafaei
2025-12-17 11:43:22 +03:30
parent f4e8832e61
commit 525b429d6a
4 changed files with 198 additions and 74 deletions
+66 -57
View File
@@ -4,15 +4,14 @@ from PIL import Image, ImageDraw, ImageFont
import numpy as np
import random
from typing import Union, List
from PIL import Image, ImageDraw, ImageFont
import math
print(fitz.version) # مثلاً ('1.24.4', '...', ...)
# ---------- تنظیمات شما ----------
INPUT_DIR = Path(r"D:\init_mahdi\project\watermark_source\in_test")
# INPUT_DIR = Path(r"D:\init_mahdi\project\watermark_source\in_test\4712")
OUTPUT_DIR = Path(r"D:\init_mahdi\project\watermark_source\o_test")
INPUT_DIR = Path(r"E:\pdf advar")
OUTPUT_DIR = Path(r".\data\qavanin_files\advar_thumb1")
WATERMARK_TEXT = [
" معاونت قوانین مجلس شورای اسلامی",
"اداره کل اسناد و اطلاع رسانی قوانین",
@@ -22,7 +21,7 @@ WATERMARK2_TEXT = "قانون‌یار"
DPI = 150
MAX_SIZE = 300
# ----------------------------------
FONT_PATH = r"Iranian Sans.ttf"
FONT_PATH = r".\app\parvande_qanon\Iranian Sans.ttf"
SUPPORTED_IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tiff", ".tif"}
SUPPORTED_PDF_EXTS = {".pdf"}
@@ -31,16 +30,17 @@ import random
from pathlib import Path
from typing import Union, List, Tuple
from PIL import Image, ImageDraw, ImageFont
import arabic_reshaper
from bidi.algorithm import get_display
def add_watermark(
img: Image.Image,
primary_text: Union[str, List[str]],
primary_text: Union[List[str]],
sec_text: str,
font_path: str = FONT_PATH,
primary_opacity: int = 50,
secondary_opacity: int = 15,
grid_rows: int = 10, # ✅ تعداد سطرهای شبکه
primary_opacity: int = 70,
secondary_opacity: int = 70,
grid_rows: int = 4, # ✅ تعداد سطرهای شبکه
grid_cols: int = 1, # ✅ تعداد ستون‌های شبکه
rotation_range: Tuple[int, int] = (-15, 13),
font_size_primary: int = 16,
@@ -93,6 +93,8 @@ def add_watermark(
draw_obj.text((x, y), text, font=font, fill=fill)
# ===== 1. واترمارک ثانویه: فقط 4 گوشه =====
sec_text = get_display(arabic_reshaper.reshape(sec_text))
if sec_text.strip():
bbox = draw.textbbox((0, 0), sec_text, font=font_s)
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
@@ -109,18 +111,23 @@ def add_watermark(
(x, y),
sec_text,
font_s,
fill_color=(255, 255, 255),
fill_color=(236, 236, 238),
stroke_color=(0, 0, 0),
opacity=secondary_opacity,
stroke_width=1,
)
# ===== 2. واترمارک اصلی: شبکه‌بندی + رندوم درون هر سلول =====
primary_texts = (
[primary_text]
if isinstance(primary_text, str)
else [t for t in primary_text if t.strip()]
)
# # ===== 2. واترمارک اصلی: شبکه‌بندی + رندوم درون هر سلول =====
# primary_texts = (
# [primary_text]
# if isinstance(primary_text, str)
# else [t for t in primary_text if t.strip()]
# )
primary_texts = []
for text in primary_text :
primary_texts.append(get_display(arabic_reshaper.reshape(text)))
if not primary_texts:
return img.convert("RGB")
@@ -149,55 +156,57 @@ def add_watermark(
x1 = x0 + cell_width
y1 = y0 + cell_height
# for text in primary_texts :
text = random.choice(primary_texts).strip()
if not text:
continue
offset = 0
if text:
# ابعاد دقیق متن
bbox = draw.textbbox((0, 0), text, font=font_p)
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
# ابعاد دقیق متن
bbox = draw.textbbox((0, 0), text, font=font_p)
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]
# ✅ موقعیت پایه: یک نقطه داخل سلول، نه الزاماً مرکز
# مثلاً 20%~80% عرض سلول و 30%~70% ارتفاع سلول
base_x = x0 + random.uniform(0.2, 0.8) * cell_width
base_y = y0 + offset + random.uniform(0.3, 0.7) * cell_height
# ✅ موقعیت پایه: یک نقطه داخل سلول، نه الزاماً مرکز
# مثلاً 20%~80% عرض سلول و 30%~70% ارتفاع سلول
base_x = x0 + random.uniform(0.2, 0.8) * cell_width
base_y = y0 + random.uniform(0.3, 0.7) * cell_height
# ✅ انحراف تصادفی در محدوده منطقی (نه بسته به safe_w که گاهی صفر می‌شد)
dx = random.uniform(-offset_limit_x, offset_limit_x)
dy = random.uniform(-offset_limit_y, offset_limit_y)
final_x = max(0, min(width - 1, base_x + dx))
final_y = max(0, min(height - 1, base_y + dy))
# ✅ انحراف تصادفی در محدوده منطقی (نه بسته به safe_w که گاهی صفر می‌شد)
dx = random.uniform(-offset_limit_x, offset_limit_x)
dy = random.uniform(-offset_limit_y, offset_limit_y)
final_x = max(0, min(width - 1, base_x + dx))
final_y = max(0, min(height - 1, base_y + dy))
# ✅ زاویه تصادفی
angle = random.randint(*rotation_range)
# ✅ زاویه تصادفی
angle = random.randint(*rotation_range)
# ساخت لایه متن
pad = 20 # padding around text for rotation
layer_w, layer_h = int(tw + 2 * pad), int(th + 2 * pad)
txt_layer = Image.new("RGBA", (layer_w, layer_h), (0, 0, 0, 0))
d = ImageDraw.Draw(txt_layer)
# ساخت لایه متن
pad = 20 # padding around text for rotation
layer_w, layer_h = int(tw + 2 * pad), int(th + 2 * pad)
txt_layer = Image.new("RGBA", (layer_w, layer_h), (0, 0, 0, 0))
d = ImageDraw.Draw(txt_layer)
draw_text_with_stroke(
d,
(pad, pad), # متن از گوشه شروع می‌شود — ساده‌تر و دقیق‌تر
text,
font_p,
fill_color=(130, 130, 135),
stroke_color=(0, 0, 0),
opacity=primary_opacity,
stroke_width=1,
)
draw_text_with_stroke(
d,
(pad, pad), # متن از گوشه شروع می‌شود — ساده‌تر و دقیق‌تر
text,
font_p,
fill_color=(255, 255, 255),
stroke_color=(0, 0, 0),
opacity=primary_opacity,
stroke_width=1,
)
rotated = txt_layer.rotate(angle, expand=True, resample=Image.BICUBIC)
rw, rh = rotated.size
rotated = txt_layer.rotate(angle, expand=True, resample=Image.BICUBIC)
rw, rh = rotated.size
# موقعیت نهایی: مرکز لایه چرخیده روی (final_x, final_y)
pos_x = int(final_x - rw / 2)
pos_y = int(final_y - rh / 2)
pos_x = max(0, min(pos_x, width - rw))
pos_y = max(0, min(pos_y, height - rh))
# موقعیت نهایی: مرکز لایه چرخیده روی (final_x, final_y)
pos_x = int(final_x - rw / 2)
pos_y = int(final_y - rh / 2)
pos_x = max(0, min(pos_x, width - rw))
pos_y = max(0, min(pos_y, height - rh))
img.alpha_composite(rotated, (pos_x, pos_y))
img.alpha_composite(rotated, (pos_x, pos_y))
# offset += 20
return img.convert("RGB")