async def download_scryfall_png(c: Card) -> Optional[str]: file_path = re.sub('.jpg$', '.png', determine_filepath([c])) if not fetch_tools.acceptable_file(file_path): await download_scryfall_card_image(c, file_path, version='png') if fetch_tools.acceptable_file(file_path): return file_path return None
async def download_scryfall_card_image(c: Card, filepath: str, version: str = '') -> bool: try: if c.is_double_sided(): paths = [ re.sub('.jpg$', '.a.jpg', filepath), re.sub('.jpg$', '.b.jpg', filepath) ] await fetch_tools.store_async(scryfall_image(c, version=version), paths[0]) if c.layout == 'transform' or c.layout == 'modal_dfc': await fetch_tools.store_async( scryfall_image(c, version=version, face='back'), paths[1]) if c.layout == 'meld': await fetch_tools.store_async( scryfall_image(c, version=version, face='meld'), paths[1]) if (fetch_tools.acceptable_file(paths[0]) and fetch_tools.acceptable_file(paths[1])): save_composite_image(paths, filepath) else: await fetch_tools.store_async(scryfall_image(c, version=version), filepath) except FetchException as e: print('Error: {e}'.format(e=e)) return fetch_tools.acceptable_file(filepath)
async def download_scryfall_image(cards: List[Card], filepath: str, version: str = '') -> bool: card_names = ', '.join(c.name for c in cards) print(f'Trying to get scryfall images for {card_names}') image_filepaths = [] for c in cards: card_filepath = determine_filepath([c]) if not fetch_tools.acceptable_file(card_filepath): await download_scryfall_card_image(c, card_filepath, version) if fetch_tools.acceptable_file(card_filepath): image_filepaths.append(card_filepath) if len(image_filepaths) > 1: save_composite_image(image_filepaths, filepath) return fetch_tools.acceptable_file(filepath)
def test_imagedownload() -> None: filepath = '{dir}/{filename}'.format(dir=configuration.get('image_dir'), filename='island.jpg') if fetch_tools.acceptable_file(filepath): os.remove(filepath) c = [oracle.load_card('Island')] assert image_fetcher.download_image(c) is not None
def test_fallbackimagedownload() -> None: filepath = '{dir}/{filename}'.format(dir=configuration.get('image_dir'), filename='nalathni-dragon.jpg') if fetch_tools.acceptable_file(filepath): os.remove(filepath) c = [oracle.load_card('Nalathni Dragon')] assert image_fetcher.download_image(c) is not None
def download_bluebones_image(cards: List[Card], filepath: str) -> bool: print('Trying to get image for {cards}'.format(cards=', '.join(c.name for c in cards))) try: fetch_tools.store(bluebones_image(cards), filepath) except FetchException as e: print('Error: {e}'.format(e=e)) return fetch_tools.acceptable_file(filepath)
async def generate_banner(names: List[str], background: str, v_crop: int = 33) -> str: cards = [oracle.load_card(name) for name in names] out_filepath = determine_filepath(cards, f'banner-{background}{v_crop}-') if fetch_tools.acceptable_file(out_filepath): return out_filepath canvas = Image.new('RGB', (1920, 210)) c = oracle.load_card(background) file_path = await download_scryfall_art_crop(c) if file_path: with Image.open(file_path) as img: h = v_crop / 100 * 1315 canvas.paste(img.resize((1920, 1315), Image.BICUBIC).crop((0, h, 1920, h + 210))) n = math.ceil(len(cards) / 2) x = 800 for c in cards[:n]: ip = await download_scryfall_png(c) with Image.open(ip) as img: img = img.resize((160, 213), Image.LANCZOS) canvas.paste(img, (x, 30)) x = x + img.width + 10 x = 900 for c in cards[n:]: ip = await download_scryfall_png(c) with Image.open(ip) as img: img = img.resize((160, 213), Image.LANCZOS) canvas.paste(img, (x, 60)) x = x + img.width + 10 canvas.save(out_filepath) return out_filepath
async def fake_send_file(channel: TextChannel, image_file: str, content: Optional[str]) -> None: print('Uploading "{0}", with additional text "{1}"'.format( image_file, content)) channel.calls += 1 assert channel is not None assert image_file is not None and fetch_tools.acceptable_file( image_file) assert content != ''
async def download_image_async(cards: List[Card]) -> Optional[str]: filepath = determine_filepath(cards) if fetch_tools.acceptable_file(filepath): return filepath if await download_scryfall_image(cards, filepath, version='border_crop'): return filepath if download_bluebones_image(cards, filepath): return filepath return None