def download_scryfall_card_image(card: Card, filepath: str, version: str = '') -> bool: try: if card.is_double_sided(): paths = [ re.sub('.jpg$', '.a.jpg', filepath), re.sub('.jpg$', '.b.jpg', filepath) ] internal.store(scryfall_image(card, version=version), paths[0]) if card.layout == 'transform': internal.store( scryfall_image(card, version=version, face='back'), paths[1]) if card.layout == 'meld': internal.store( scryfall_image(card, version=version, face='meld'), paths[1]) if (internal.acceptable_file(paths[0]) and internal.acceptable_file(paths[1])): save_composite_image(paths, filepath) else: internal.store(scryfall_image(card, version=version), filepath) except FetchException as e: print('Error: {e}'.format(e=e)) return internal.acceptable_file(filepath)
def download_scryfall_image(cards: List[Card], filepath: str, version: str = '') -> bool: card_names = ', '.join(card.name for card in cards) print(f'Trying to get scryfall images for {card_names}') image_filepaths = [] for card in cards: card_filepath = determine_filepath([card]) if not internal.acceptable_file(card_filepath): download_scryfall_card_image(card, card_filepath, version) if internal.acceptable_file(card_filepath): image_filepaths.append(card_filepath) if len(image_filepaths) > 1: save_composite_image(image_filepaths, filepath) return internal.acceptable_file(filepath)
def download_scryfall_image(cards: List[Card], filepath: str, version: str = '') -> bool: print('Trying to get scryfall image for {card}'.format(card=cards[0])) try: internal.store(scryfall_image(cards[0], version=version), filepath) except FetchException as e: print('Error: {e}'.format(e=e)) return internal.acceptable_file(filepath)
def download_bluebones_image(cards: List[Card], filepath: str) -> bool: print('Trying to get image for {cards}'.format(cards=', '.join(card.name for card in cards))) try: internal.store(bluebones_image(cards), filepath) except FetchException as e: print('Error: {e}'.format(e=e)) return internal.acceptable_file(filepath)
def test_imagedownload() -> None: filepath = '{dir}/{filename}'.format(dir=configuration.get('image_dir'), filename='island.jpg') if fetcher_internal.acceptable_file(filepath): os.remove(filepath) c = [oracle.load_card('Island')] assert image_fetcher.download_image(c) is not None
def download_scryfall_png(card: Card) -> Optional[str]: file_path = re.sub('.jpg$', '.png', determine_filepath([card])) if not internal.acceptable_file(file_path): download_scryfall_card_image(card, file_path, version='png') if internal.acceptable_file: return file_path return None
async def fake_send_file(channel, image_file, content=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 fetcher_internal.acceptable_file( image_file) assert content != ''
def download_image(cards: List[Card]) -> Optional[str]: filepath = determine_filepath(cards) if internal.acceptable_file(filepath): return filepath if download_bluebones_image(cards, filepath): return filepath if download_scryfall_image(cards, filepath): return filepath if download_mci_image(cards, filepath): return filepath return None
def download_mci_image(cards: List[Card], filepath: str) -> bool: printings = oracle.get_printings(cards[0]) for p in printings: print('Trying to get MCI image for {imagename}'.format(imagename=os.path.basename(filepath))) try: internal.store(mci_image(p), filepath) if internal.acceptable_file(filepath): return True except FetchException as e: print('Error: {e}'.format(e=e)) print('Trying to get fallback image for {imagename}'.format(imagename=os.path.basename(filepath))) try: img = gatherer_image(p) if img: internal.store(img, filepath) if internal.acceptable_file(filepath): return True except FetchException as e: print('Error: {e}'.format(e=e)) return False
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 internal.acceptable_file(out_filepath): return out_filepath canvas = Image.new('RGB', (1920, 210)) c = oracle.load_card(background) file_path = 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 card in cards[:n]: ip = download_scryfall_png(card) 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 card in cards[n:]: ip = download_scryfall_png(card) 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