home · contact · privacy
Add crop/margin symmetry option to bookmaker script.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 20 Sep 2023 20:27:53 +0000 (22:27 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 20 Sep 2023 20:27:53 +0000 (22:27 +0200)
bookmaker.py

index dd5b2e741fc78fbe2789ee53def885d0db0d9db8..9e66775f4ae9aaefa9a5d3b2783f865fc51faeff 100755 (executable)
@@ -12,6 +12,7 @@ parser.add_argument("-p", "--pages", dest="page_range", help="page range, e.g.,
 parser.add_argument("-c", "--crop", dest="crop_range", help="crops left, bottom, right, top – e.g., '10,10,10,10'")
 parser.add_argument("-n", "--nup4", dest="nup4", action='store_true', help="puts 4 input pages onto 1 output page")
 parser.add_argument("-a", "--analyze", dest="analyze", action="store_true", help="print lines identifying spine, page borders")
+parser.add_argument("-t", "--symmetry", dest="symmetry", action="store_true", help="alternate horizontal crops between odd and even pages")
 args = parser.parse_args()
 
 with open(args.input_file, 'rb') as file:
@@ -61,12 +62,17 @@ with open(args.input_file, 'rb') as file:
 
     writer = pypdf.PdfWriter()
     if not args.nup4:
+        odd_page = True
         for page in pages_to_add:
-            page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom))
+            if args.symmetry and odd_page:
+                page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom))
+            else:
+                page.add_transformation(pypdf.Transformation().translate(tx=-crop_right, ty=-crop_bottom))
             page.add_transformation(pypdf.Transformation().scale(zoom, zoom))
             page.mediabox.right = cropped_width * zoom
             page.mediabox.top = cropped_height * zoom
             writer.add_page(page)
+            odd_page = not odd_page
     else:
         n_pages_per_axis = 2
         points_per_mm = 2.83465
@@ -111,7 +117,10 @@ with open(args.input_file, 'rb') as file:
             # in-section transformations: align pages on top, left-hand pages to left, right-hand to right
             page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top))))
             if i == 0 or i == 2:
-                page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
+                if args.symmetry:
+                    page.add_transformation(pypdf.Transformation().translate(tx=-crop_right))
+                else:
+                    page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
             elif i == 1 or i == 3:
                 page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right))))
             page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor))