Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Created February 9, 2020 19:56
Show Gist options
  • Save Jerakin/20085bcb2507bcfd5186350a3d75e0db to your computer and use it in GitHub Desktop.
Save Jerakin/20085bcb2507bcfd5186350a3d75e0db to your computer and use it in GitHub Desktop.
from PIL import Image
from pathlib import Path
import json
root = Path(__file__).parent
output = root / "assembled_tokens"
tokens = root / "images"
token_source = root / "token_source"
data_file_folder = root.parent.parent.parent / "assets" / "datafiles"
pokemon_folder = data_file_folder / "pokemon"
data_file = data_file_folder / "index_order.json"
with data_file.open("r", encoding="utf-8") as fp:
index_order = json.load(fp)
def assemble(index):
name, types = get_info(index)
new_im = Image.open(token_source / "background.png")
print("Saving out", name)
if len(types) == 1:
image1 = Image.open((token_source / types[0].lower()).with_suffix(".png"))
new_im.paste(image1, mask=image1)
else:
mask = Image.open((token_source / "mask").with_suffix(".png"))
image1 = Image.open((token_source / types[0].lower()).with_suffix(".png"))
image2 = Image.open((token_source / types[1].lower()).with_suffix(".png"))
ring = Image.composite(image1, image2, mask)
new_im.paste(ring, mask=ring)
token_image = Image.open(tokens / "token_{}.png".format(index))
output_image = Image.alpha_composite(new_im, token_image)
with open((output / name).with_suffix(".png"), "wb") as fp:
output_image.save(fp)
def get_info(index):
species = index_order[str(index)]
name = species[0]
name = name.replace(" ♀", "-f")
name = name.replace(" ♂", "-m")
name = name.replace("é", "e")
pokemon_json = pokemon_folder / "{}.json".format(name)
with pokemon_json.open("r") as fp:
pokemon_data = json.load(fp)
return name, pokemon_data["Type"]
for i in range(720-580):
assemble(i+580)
from pathlib import Path
import requests
import time
import shutil
import re
from bs4 import BeautifulSoup
root = Path(__file__).parent
output = root / "images"
dirty_reg = re.compile('<a class="internal" href="(//archives.bulbagarden.net/media/upload.*)" title="BT{3}.png">BT{3}.png</a>')
if not output.exists():
output.mkdir()
def get_url_from_source(source):
soup = BeautifulSoup(source, 'html.parser')
for image in soup.find_all("img"):
if image["height"] == "128" and image["width"] == "128":
return image["src"][2:]
return None
def download_image(url, name):
r = requests.get("http://" + url, stream=True)
print("Got Image ", name)
if r.status_code == 200:
with open(name, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
print("Image copied")
else:
print("Error")
def download(index):
raw_url = "https://bulbapedia.bulbagarden.net/wiki/File:BT{:03d}.png".format(index,)
file_name = Path(output / "token_{}.png".format(index)).absolute()
r = requests.get(raw_url)
url = get_url_from_source(r.content)
if url:
download_image(url, file_name)
else:
print("No Match", index)
for i in range(720):
download(i+155)
time.sleep(0.5)
@Jerakin
Copy link
Author

Jerakin commented Feb 9, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment