assemble decks from a text file and output all the required images into a folder

This commit is contained in:
0ceanSlim 2023-11-15 12:54:17 -05:00
parent 85d6e42be9
commit 41750868e3
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,28 @@
1 Cruella De Vil - Miserable As Usual
2 Aladdin - Prince Ali
2 Scar - Fiery Usurper
2 Donald Duck - Boisterous Fowl
3 Mickey Mouse - Steamboat Pilot
3 Sergeant Tibbs - Courageous Cat
1 Iago - Loud-Mouthed Parrot
3 Rapunzel - Letting Down Her Hair
2 Peter Pan - Never Landing
3 Pongo - Ol' Rascal
3 Aladdin - Street Rat
2 Duke Of Weselton - Opportunistic Official
3 Megara - Pulling the Strings
2 Jasper - Common Crook
3 Horace - No-Good Scoundrel
1 Lefou - Instigator
3 Mad Hatter - Gracious Host
2 Captain - Colonel's Lieutenant
1 Stitch - Abomination
1 Aladdin - Heroic Outlaw
3 Dragon Fire
2 Stampede
2 Vicious Betrayal
2 He's Got a Sword!
1 Steal from the Rich
3 Mother Knows Best
2 Stolen Scimitar
2 Shield of Virtue

68
assembleDeck.py Normal file
View File

@ -0,0 +1,68 @@
import csv
import os
import urllib.request
# Function to download images
def download_image(image_url, folder_path, file_name):
image_path = os.path.join(folder_path, file_name)
urllib.request.urlretrieve(image_url, image_path)
# List all .txt files in the Deck Lists folder
txt_files = [file for file in os.listdir('Deck Lists') if file.endswith('.txt')]
# Display the list of .txt files and prompt for selection
print("Available text files:")
for index, file in enumerate(txt_files, start=1):
print(f"{index}. {file}")
selection = input("Enter the number corresponding to the text file: ")
# Validate the user input and extract the selected text file name
try:
selection_index = int(selection)
if 1 <= selection_index <= len(txt_files):
text_file_name = txt_files[selection_index - 1]
# Extract the deck name from the text file name
deck_name = os.path.splitext(text_file_name)[0]
# Read CSV file and store image URLs and quantities
image_urls = {}
with open('data.csv', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name_title = f"{row['name']} - {row['title']}" if row['title'] else row['name']
if name_title not in image_urls:
image_urls[name_title] = {'image': row['image'], 'qty': int(row['number'])}
# Create output folder using the deck name
output_folder = f'images/{deck_name}'
os.makedirs(output_folder, exist_ok=True)
# Read text file and organize images
with open(f'Deck Lists/{text_file_name}', 'r') as textfile:
for line in textfile:
line = line.strip()
parts = line.split(' - ')
if len(parts) >= 2:
quantity, name, title = parts[0].split()[0], ' '.join(parts[0].split()[1:]), parts[1]
else:
quantity, name = line.split()[0], ' '.join(line.split()[1:])
title = None # Set title as None for entries without titles
name_title = f"{name} - {title}" if title else name
card_info = image_urls.get(name_title)
if card_info:
qty_needed = int(quantity)
qty_available = card_info['qty']
# Download images based on quantity needed
for i in range(min(qty_needed, qty_available)):
file_name = f"{name_title} ({i + 1}).webp"
download_image(card_info['image'], output_folder, file_name)
else:
print("Invalid selection.")
except ValueError:
print("Invalid input. Please enter a number.")