Skip to content

Commit c19b6d8

Browse files
committed
slide updates for watermark
1 parent 3ec8767 commit c19b6d8

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

scripts/png_to_pdf.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
from tqdm import tqdm
77
import cv2
88
import numpy as np
9+
import tempfile
910

1011
parser = argparse.ArgumentParser()
1112
parser.add_argument("-i", "--input", help="input directory with pngs")
1213
parser.add_argument("-o", "--output", help="output pdf file")
14+
parser.add_argument("--quality", type=int, default=40, help="quality of the images")
1315
parser.add_argument("--no-watermark", action='store_true', help="dont include a watermark")
1416
args = parser.parse_args()
1517

@@ -34,14 +36,51 @@
3436
pdf.set_author("A. Amini, A.P. Amini. MIT 6.S191")
3537
pdf.set_margins(0, 0, 0)
3638

39+
if not args.no_watermark:
40+
# Get the directory where this script is located
41+
script_dir = os.path.dirname(os.path.abspath(__file__))
42+
watermark_path = os.path.join(script_dir, "watermark.png")
43+
watermark_rgba = cv2.imread(watermark_path, cv2.IMREAD_UNCHANGED)
44+
if watermark_rgba is None:
45+
raise FileNotFoundError(f"Could not load watermark from {watermark_path}")
46+
47+
# Extract alpha channel (we'll use this as the watermark pattern)
48+
if watermark_rgba.shape[2] == 4:
49+
watermark_alpha = watermark_rgba[:, :, 3]
50+
else:
51+
# If no alpha channel, use grayscale as pattern
52+
watermark_alpha = cv2.cvtColor(watermark_rgba, cv2.COLOR_BGR2GRAY)
53+
3754
# imagelist is the list with all image filenames
3855
files = sorted(glob.glob(os.path.join(args.input, "*.jpeg")))
3956
files.sort(key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])
4057
import pdb; pdb.set_trace()
4158
for image in tqdm(files):
4259
pdf.add_page()
43-
pdf.image(image, x=0, y=0, w=1280, h=720)
60+
image_data = cv2.imread(image)
61+
4462
if not args.no_watermark:
45-
pdf.image("watermark.png", x=0, y=0, w=1280, h=720)
63+
# Calculate median brightness of the slide (normalized to 0-1)
64+
gray_slide = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY)
65+
median_brightness = np.median(gray_slide) / 255.0
66+
67+
# Resize watermark alpha to match the slide dimensions
68+
watermark_resized = cv2.resize(watermark_alpha, (image_data.shape[1], image_data.shape[0]))
69+
70+
# Choose watermark color based on slide brightness
71+
if median_brightness < 0.5:
72+
# Dark slide -> add white watermark
73+
watermark_color = cv2.cvtColor(watermark_resized, cv2.COLOR_GRAY2BGR)
74+
image_data = cv2.addWeighted(image_data, 1, watermark_color, 1.2, 0)
75+
else:
76+
# Light slide -> subtract black watermark
77+
watermark_color = cv2.cvtColor(watermark_resized, cv2.COLOR_GRAY2BGR)
78+
# Subtract the watermark (makes it darker where watermark exists)
79+
image_data = cv2.subtract(image_data, (watermark_color * 1.2).astype(np.uint8))
80+
81+
temp_file = tempfile.mktemp() + ".jpg"
82+
cv2.imwrite(temp_file, image_data, [int(cv2.IMWRITE_JPEG_QUALITY), args.quality])
83+
pdf.image(temp_file, x=0, y=0, w=1280, h=720)
84+
os.remove(temp_file)
4685

4786
pdf.output(args.output, "F")

0 commit comments

Comments
 (0)