Skip to content

Commit

Permalink
Added crop marks.
Browse files Browse the repository at this point in the history
  • Loading branch information
DiddiZ committed Dec 7, 2021
1 parent 717deba commit ab5d760
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mtgproxies/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mtgproxies.print_cards import print_cards_matplotlib, print_cards_fpdf
from mtgproxies.print_cards import print_cards_fpdf, print_cards_matplotlib
from mtgproxies.scans import fetch_scans_scryfall

__all__ = [
Expand Down
38 changes: 25 additions & 13 deletions mtgproxies/print_cards.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pathlib import Path

import matplotlib.pyplot as plt
Expand All @@ -8,12 +10,11 @@

from mtgproxies.plotting import SplitPages


def _occupied_space(cardsize, pos, border_crop, image_size, closed=False):
return cardsize * (pos * image_size - np.clip(2 * pos - 1 - closed, 0, None) * border_crop) / image_size
image_size = np.array([745, 1040])


scan_size = np.array([745, 1040])
def _occupied_space(cardsize, pos, border_crop, closed=False):
return cardsize * (pos * image_size - np.clip(2 * pos - 1 - closed, 0, None) * border_crop) / image_size


def print_cards_matplotlib(
Expand Down Expand Up @@ -69,12 +70,10 @@ def print_cards_matplotlib(
img = img[top:, left:]

# Compute extent
lower = (
offset + _occupied_space(cardsize, np.array([x, y]), border_crop, scan_size)
) / papersize
lower = (offset + _occupied_space(cardsize, np.array([x, y]), border_crop)) / papersize
upper = (
offset + _occupied_space(cardsize, np.array([x, y]), border_crop, scan_size) + cardsize *
(scan_size - [left, top]) / scan_size
offset + _occupied_space(cardsize, np.array([x, y]), border_crop) + cardsize *
(image_size - [left, top]) / image_size
) / papersize
extent = [lower[0], upper[0], 1 - upper[1], 1 - lower[1]] # flip y-axis

Expand Down Expand Up @@ -102,7 +101,8 @@ def print_cards_fpdf(
papersize=np.array([210, 297]),
cardsize=np.array([2.5 * 25.4, 3.5 * 25.4]),
border_crop: int = 14,
background_color=None,
background_color: tuple[int, int, int] = None,
cropmarks: bool = True
):
"""Print a list of cards to a pdf file.
Expand All @@ -120,7 +120,7 @@ def print_cards_fpdf(
if N[0] == 0 or N[1] == 0:
raise ValueError(f"Paper size too small: {papersize}")
cards_per_sheet = np.prod(N)
offset = (papersize - _occupied_space(cardsize, N, border_crop, scan_size, closed=True)) / 2
offset = (papersize - _occupied_space(cardsize, N, border_crop, closed=True)) / 2

# Ensure directory exists
Path(filepath).parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -152,11 +152,23 @@ def print_cards_fpdf(
plt.imsave(cropped_image, plt.imread(image)[top:, left:])

# Compute extent
lower = offset + _occupied_space(cardsize, np.array([x, y]), border_crop, scan_size)
size = cardsize * (scan_size - [left, top]) / scan_size
lower = offset + _occupied_space(cardsize, np.array([x, y]), border_crop)
size = cardsize * (image_size - [left, top]) / image_size

# Plot image
pdf.image(cropped_image, x=lower[0], y=lower[1], w=size[0], h=size[1])

if cropmarks and ((i + 1) % cards_per_sheet == 0 or i + 1 == len(images)):
# If this was the last card on a page, add crop marks
pdf.set_line_width(0.05)
pdf.set_draw_color(255, 255, 255)
a = cardsize * (image_size - 2 * border_crop) / image_size
b = papersize - N * a
for x in range(N[0] + 1):
for y in range(N[1] + 1):
mark = b / 2 + a * [x, y]
pdf.line(mark[0] - 0.5, mark[1], mark[0] + 0.5, mark[1])
pdf.line(mark[0], mark[1] - 0.5, mark[0], mark[1] + 0.5)

tqdm.write(f"Writing to {filepath}")
pdf.output(filepath, 'F')
2 changes: 1 addition & 1 deletion print.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from mtgproxies import fetch_scans_scryfall, print_cards_matplotlib, print_cards_fpdf
from mtgproxies import fetch_scans_scryfall, print_cards_fpdf, print_cards_matplotlib
from mtgproxies.cli import parse_decklist_spec


Expand Down

0 comments on commit ab5d760

Please sign in to comment.