|
6 | 6 | from tqdm import tqdm |
7 | 7 | import cv2 |
8 | 8 | import numpy as np |
| 9 | +import tempfile |
9 | 10 |
|
10 | 11 | parser = argparse.ArgumentParser() |
11 | 12 | parser.add_argument("-i", "--input", help="input directory with pngs") |
12 | 13 | parser.add_argument("-o", "--output", help="output pdf file") |
| 14 | +parser.add_argument("--quality", type=int, default=40, help="quality of the images") |
13 | 15 | parser.add_argument("--no-watermark", action='store_true', help="dont include a watermark") |
14 | 16 | args = parser.parse_args() |
15 | 17 |
|
|
34 | 36 | pdf.set_author("A. Amini, A.P. Amini. MIT 6.S191") |
35 | 37 | pdf.set_margins(0, 0, 0) |
36 | 38 |
|
| 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 | + |
37 | 54 | # imagelist is the list with all image filenames |
38 | 55 | files = sorted(glob.glob(os.path.join(args.input, "*.jpeg"))) |
39 | 56 | files.sort(key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)]) |
40 | 57 | import pdb; pdb.set_trace() |
41 | 58 | for image in tqdm(files): |
42 | 59 | pdf.add_page() |
43 | | - pdf.image(image, x=0, y=0, w=1280, h=720) |
| 60 | + image_data = cv2.imread(image) |
| 61 | + |
44 | 62 | 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) |
46 | 85 |
|
47 | 86 | pdf.output(args.output, "F") |
0 commit comments