-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmet_print.py
More file actions
79 lines (66 loc) · 2.99 KB
/
Copy pathmet_print.py
File metadata and controls
79 lines (66 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""Print a Met Museum artwork on the S01 thermal printer.
Pulls a random piece (optionally matching a search term) from the Metropolitan
Museum of Art's open collection, dithers the image, and prints the title, artist,
date, and medium.
Examples:
python met_print.py # random artwork
python met_print.py --q cat
python met_print.py --q landscape
python met_print.py --no-print
"""
from __future__ import annotations
import argparse
from pathlib import Path
import random
import urllib.parse
from print_common import ROOT, Card, fit_dither, get_bytes, get_json
SEARCH = "https://collectionapi.metmuseum.org/public/collection/v1/search?hasImages=true&q={q}"
OBJECT = "https://collectionapi.metmuseum.org/public/collection/v1/objects/{id}"
TERMS = ["painting", "portrait", "landscape", "flowers", "ship", "mythology",
"still life", "cat", "horse", "garden", "river", "moon", "dragon"]
def main() -> int:
parser = argparse.ArgumentParser(description="Print a Met Museum artwork on the S01 thermal printer.")
parser.add_argument("--q", default=None, help="Search term (default: random theme).")
parser.add_argument("--out", type=Path, default=ROOT / "met.png")
parser.add_argument("--darkness", type=int, choices=range(1, 6), default=3)
parser.add_argument("--bottom-feed", type=int, default=24)
parser.add_argument("--no-print", action="store_true")
args = parser.parse_args()
term = args.q or random.choice(TERMS)
print(f"Browsing the Met for '{term}' ...")
ids = get_json(SEARCH.format(q=urllib.parse.quote(term))).get("objectIDs") or []
if not ids:
raise SystemExit(f"No artworks found for {term!r}")
art = None
for oid in random.sample(ids, min(15, len(ids))):
o = get_json(OBJECT.format(id=oid))
if o.get("primaryImageSmall"):
art = o
break
if not art:
raise SystemExit("Couldn't find an artwork with an image; try again")
title = art["title"] or "Untitled"
artist = art.get("artistDisplayName") or "Unknown artist"
date = art.get("objectDate") or "?"
medium = art.get("medium") or "?"
dept = art.get("department") or "?"
print(f" {title} - {artist} ({date})")
card = Card()
n = len(title)
if n <= 22:
card.title(title.upper(), size=22)
else:
# long museum titles: wrap instead of shrinking off the edges
card.para(title.upper(), size=16, bold=True, center=True, max_lines=3)
card.gap(2).divider(dashed=True).gap(2)
card.heading(artist, size=14)
card.line(date, size=12, bold=False, center=True)
card.gap(4).image(fit_dither(get_bytes(art["primaryImageSmall"]), card.inner_w, 360, contrast=1.1))
card.gap(2).divider()
card.para(medium, size=12)
card.kv("Department", dept)
card.footer("metmuseum.org", "open access")
return card.finish(args.out, args.bottom_feed, args.darkness, do_print=not args.no_print)
if __name__ == "__main__":
raise SystemExit(main())