catarot/catarot.py

303 lines
8.7 KiB
Python
Raw Permalink Normal View History

import asyncio
import logging
from string import digits
import sys
import os
from catprinter.cmds import PRINT_WIDTH, cmds_print_img, cmds_print_tight_img, cmd_feed_paper
from catprinter.ble import run_ble
from catprinter.img import read_img, text_to_img, texts_to_img
from enum import Enum
import random
from sys import stdin
import csv
2023-06-24 02:53:41 +00:00
club_picture = ("\n"+' ______________ '+
' |.------------.| '+
' || || '+
' ||cyberia.club|| '+
' || || '+
" |'------------'| "+
" `)___________(' "+
' [===== -- o ]--. '+
" '-------------' \ "+
' [::::::::::: :::] ) '+
' `"""""""""""""""` /T\ '+
' \_/ ')
def make_logger(log_level):
logger = logging.getLogger('catprinter')
logger.setLevel(log_level)
h = logging.StreamHandler(sys.stdout)
h.setLevel(log_level)
logger.addHandler(h)
return logger
def print_img(
img,
devicename,
darker,
logger
):
logger.info(f'✅ Read image: {img.shape} (h, w) pixels')
data = cmds_print_img(img, dark_mode=darker)
logger.info(f'✅ Generated BLE commands: {len(data)} bytes')
# Try to autodiscover a printer if --devicename is not specified.
autodiscover = not devicename
return asyncio.run(run_ble(data, devicename, autodiscover, logger))
def print_img_then_txts(
img,
title_txt,
body_txt,
devicename,
darker,
logger
):
logger.info(f'✅ Read image: {img.shape} (h, w) pixels')
bin_img_data = cmds_print_tight_img(img, dark_mode=darker)
title_txt_img = text_to_img(
text="\n"+title_txt.replace("\\n", "\n"),
font_name="Z003-MediumItalic.otf",
font_size=30,
show_preview=False,
bold=False,
logger=logger
)
body_txt_img = text_to_img(
2023-06-24 02:53:41 +00:00
text=body_txt.replace("\\n", "\n"),
font_name="Ubuntu-R.ttf",
font_size=18,
show_preview=False,
bold=False,
logger=logger
)
2023-06-24 02:53:41 +00:00
club_txt_img = text_to_img(
2023-06-24 03:36:10 +00:00
text=club_picture_1,
2023-06-24 02:53:41 +00:00
font_name="FreeMono.ttf",
font_size=22,
show_preview=False,
bold=True,
logger=logger
)
#txt_img = texts_to_img(
# text=[title_txt, body_txt],
# font_name="Ubuntu-R.ttf",
# font_size=30,
# show_preview=False,
# bold=False,
# logger=logger
#)
bin_title_txt_img_data = cmds_print_tight_img(title_txt_img, dark_mode=darker)
bin_body_txt_img_data = cmds_print_img(body_txt_img, dark_mode=darker)
2023-06-24 02:53:41 +00:00
bin_club_txt_img_data = cmds_print_img(club_txt_img, dark_mode=darker)
#data = bin_title_txt_img_data + bin_body_txt_img_data
2023-06-24 02:53:41 +00:00
data = bin_img_data + bin_title_txt_img_data + bin_body_txt_img_data + bin_club_txt_img_data
logger.info(f'✅ Generated BLE commands: {len(data)} bytes')
# Try to autodiscover a printer if --devicename is not specified.
autodiscover = not devicename
return asyncio.run(run_ble(data, devicename, autodiscover, logger))
def image(filename, log_level, img_binarization_algo, show_preview, devicename, darker):
log_level = getattr(logging, log_level.upper())
logger = make_logger(log_level)
if not os.path.exists(filename):
logger.info('🛑 File not found. Exiting.')
return
bin_img = read_img(filename, PRINT_WIDTH,
logger, img_binarization_algo, show_preview)
if bin_img is None:
logger.info(f'🛑 No image generated. Exiting.')
return
return print_img(
img=bin_img,
devicename=devicename,
darker=darker,
logger=logger
)
def text(text, font, font_size, log_level, show_preview, bold, devicename):
log_level = getattr(logging, log_level.upper())
logger = make_logger(log_level)
bin_img = text_to_img(
text=text.replace("\\n", "\n"),
font_name=font,
font_size=font_size,
show_preview=show_preview,
bold=bold,
logger=logger
)
if bin_img is None:
logger.info(f'🛑 No text image generated. Exiting.')
return
return print_img(
img=bin_img,
devicename=devicename,
darker=True, # Text mode
logger=logger
)
class card_data:
def __init__(self, inv, name, descu, desci, f):
self.card_name = name
self.card_desc_upright = descu
self.card_desc_inverted = desci
self.filename = f
def build_card_index():
cards = []
with open('card_index.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in spamreader:
c=card_data(False, row[0], row[1], row[2], row[3])
cards.append(c)
return cards
def lookup_card(card_index, card_key):
inverse = False
2023-06-24 02:53:41 +00:00
print("looking up " + card_key)
for card in card_index:
if "inv" in card_key:
inverse = True
card_key=card_key.strip("inv-")
if card.filename == card_key:
if inverse:
return (card.card_name + "(Inverted)", card.card_desc_inverted)
else:
return (card.card_name + "", card.card_desc_upright)
return ("oops", "card not found")
2023-06-24 02:53:41 +00:00
club_picture = " _______________"+"\n cyberia.club"
club_picture_1 = ("\n"+' ______________ '+
'\n |.------------.| '+
'\n || || '+
'\n ||cyberia.club|| '+
'\n || || '+
"\n |'------------'| "+
"\n `)___________(' "+
'\n [===== -- o ]--. '+
"\n '-------------' \ "+
'\n [::::::::::: :::] ) '+
'\n `"""""""""""""""` /T\ '+
'\n \_/ ')
class decks(Enum):
RIDER_WAITTE = 0
MCNUTTANDREW = 1
OCCULTIFY = 2
2022-05-03 17:49:31 +00:00
JOKE = 3
def get_mcnutt_key(img_name):
2022-04-19 20:29:54 +00:00
card_key = ""
if img_name.startswith('0') or img_name.startswith('1'):
#is major arcana
card_key = img_name.split(' ')[0]
else:
#is minor arcana
suit = img_name.split(' ')[0]
if suit == "Coins":
card_key += "pe"
elif suit == "Swords":
card_key +="sw"
elif suit =="Cups":
card_key +="cu"
elif suit == "Wands":
card_key += "wa"
value = img_name.split(' ')[1].split('.')[0]
if value == "Ace":
card_key += "01"
elif value == "King":
card_key += "ki"
elif value == "Queen":
card_key += "qu"
elif value == "Knight":
card_key += "ki"
elif value == "Princess":
card_key += "pa"
elif value == '10':
card_key += value
elif value == '20':
card_key += value
elif value == '21':
card_key += value
else:
2022-04-19 20:29:54 +00:00
card_key += "0"+value
return card_key
def print_card(card_index, deck = decks.RIDER_WAITTE):
img_name = ""
img_path = ""
card_title_txt = ""
card_body_txt = ""
if deck == decks.RIDER_WAITTE:
img_name = random.choice(os.listdir('images'))
img_path = 'images/' + img_name
2023-06-24 02:53:41 +00:00
print(img_name)
card_title_txt, card_body_txt = lookup_card(card_index, img_name.split('.')[0])
elif deck == decks.MCNUTTANDREW:
img_name = random.choice(os.listdir('exported-cards'))
img_path = 'exported-cards/' + img_name
2023-06-24 02:53:41 +00:00
print(img_name)
card_key = get_mcnutt_key(img_name)
card_title_txt, card_body_txt = lookup_card(card_index, card_key)
elif deck == decks.OCCULTIFY:
img_name = random.choice(os.listdir('occultify-deck'))
img_path = 'occultify-deck/' + img_name
print(img_name)
card_title_txt, card_body_txt = lookup_card(card_index, img_name.split('.')[0])
2022-05-03 17:49:31 +00:00
elif deck == decks.JOKE:
img_name = random.choice(os.listdir('joke-deck'))
img_path = 'joke-deck/' + img_name
print(img_name)
card_title_txt = 'joke card'
card_body_txt = 'this is a funny card'
ll = getattr(logging, "INFO")
logger = make_logger(ll)
2023-06-24 02:53:41 +00:00
print("card name is ", card_title_txt)
print("path is ", img_path)
bin_card_img = read_img(img_path, PRINT_WIDTH,
logger, img_binarization_algo="mean-threshold", show_preview=False)
print_img_then_txts(img = bin_card_img, title_txt = card_title_txt.strip(), body_txt = card_body_txt.strip(), devicename = False, darker = True, logger = logger)
return
if __name__ == '__main__':
c = build_card_index()
print_card(c)