Skip to content

Instantly share code, notes, and snippets.

@gtmanfred
Last active March 29, 2024 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtmanfred/34df96fdf204eee83b40f83265a0b7b6 to your computer and use it in GitHub Desktop.
Save gtmanfred/34df96fdf204eee83b40f83265a0b7b6 to your computer and use it in GitHub Desktop.
Script for formatting card images for Avery 5395 Name Labels for making proxies.
"""
Script for formatting card images for Avery 5395 Name Labels for making proxies.
"""
import argparse
import csv
import json
import sys
from urllib.request import urlopen
from fpdf import FPDF
from PIL import Image
TEMPLATES = {
"officedepot": {
"xstart": 19/32,
"yfirst": 3/4,
"ysecond": 4 + 1/2,
"xdiff": (3 + 1/16 - 18/32),
},
"avery": {
},
}
def _parse_args():
parser = argparse.ArgumentParser(
description="Convert CubeCobra csv to pdf of images for Proxies",
)
parser.add_argument('csv', type=argparse.FileType('r'))
return parser.parse_args()
def main():
args = _parse_args()
images = []
reader = csv.DictReader(args.csv)
for idx, card in enumerate(reader):
if card["maybeboard"] == "true":
continue
set_name, colnum = card["Set"], card["Collector Number"]
url = f'https://api.scryfall.com/cards/{set_name}/{colnum}'
data = json.load(urlopen(url))
images.append(data["image_uris"]["png"])
_make_pdf(images)
def _make_pdf(images, template="officedepot"):
pdf = FPDF(format="letter", unit="in", orientation="landscape")
temp = TEMPLATES[template]
for idx, image in enumerate(images):
print(idx, "/", len(images))
if not idx % 8:
pdf.add_page()
x, y = temp["xstart"], temp["yfirst"]
elif not idx % 4:
x, y = temp["xstart"], temp["ysecond"]
else:
x += temp["xdiff"]
im = Image.open(urlopen(image))
pdf.image(
im,
x=x, y=y,
w=2 + 1/3,
keep_aspect_ratio=True,
)
pdf.output("cards.pdf")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment