Last active
May 18, 2022 04:42
-
-
Save BillyDoesDev/751571f24e1258a81fbbf51643aa9979 to your computer and use it in GitHub Desktop.
Generate high res wallpapers using Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
import re | |
import requests | |
from PIL import Image, UnidentifiedImageError | |
headers = { | |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0" | |
} | |
def color_pixels(fg_color:tuple, spam:Image) -> Image: | |
pixels = [] | |
r, g, b = fg_color | |
for y in range(spam.height): | |
for x in range(spam.width): | |
pixels.append((r, g, b, spam.getpixel((x,y))[-1])) | |
spam.putdata(pixels) | |
return spam | |
def generate_wallpaper(size:tuple, icon_size:tuple, fg_color:tuple, bg_color:tuple, rotation_range=(0,360), local_icon_dir="") -> Image: | |
canvas = Image.new(mode="RGBA", size=size, color=bg_color) | |
with requests.get("https://www.flaticon.com/packs/computer-122", headers=headers) as r: | |
urls = re.findall(r"https:\/\/cdn-icons-png\.flaticon\.com\/512\/\d{4}\/\d{7}\.png", r.text) | |
x, y = 0, 0 | |
print("Progress:", end=" ", flush=True) | |
while y <= size[1]: | |
heights = [] | |
while x <= size[0]: | |
print(f"{round(y*100/size[1], 1)}%".rjust(5, "0"), end="", flush=True) # Display progress | |
with open(".icon.png", "wb") as icon: | |
try: | |
if not local_icon_dir: # get icons online only if explicitly specified | |
raw_icon = requests.get(random.choice(urls), headers=headers) | |
icon.write(raw_icon.content) | |
with open(os.path.join(local_icon_dir, random.choice(os.listdir(local_icon_dir))), mode="rb") as _: | |
icon.write(_.read()) | |
icon_size_ = random.randint(*icon_size) | |
try: | |
with Image.open(".icon.png").convert("RGBA").resize((icon_size_, icon_size_)).rotate(random.randint(*rotation_range), expand=1) as sprite: | |
sprite = color_pixels(fg_color, sprite) | |
canvas.paste(sprite, (x, y), sprite) | |
# canvas.save("wallpaper.png") # see live progress or smth, idk | |
x += icon_size_ + 50 | |
heights.append(icon_size_) | |
except UnidentifiedImageError: | |
pass | |
except requests.exceptions.ConnectionError: | |
pass | |
print("\b" * 5, end="", flush=True) | |
x = 0 | |
y += max(heights) + 10 | |
print("100% "); os.remove(".icon.png") | |
return canvas | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(description="Script to generate high res wallpapers of random doodles") | |
parser.version = "1.0 [beta]" | |
parser.add_argument("-v", "--version", action="version") | |
parser.add_argument("-r", metavar="resolution", type=str, default="2160x3840", help="select resolution like <width>x<height> [Defaults to 2160x3840]") | |
parser.add_argument("-icon", metavar="icon_size", type=str, default="130,265", help="select min, max icon size like <min>,<max> [Defaults to 130,265]") | |
parser.add_argument("-rt", metavar="icon_rotation", type=str, default="0,360", help="select min, max icon rotaion angle like <min>,<max> [Defaults to 0,360]") | |
parser.add_argument("-fg", metavar="hex_code", type=str, default="2ac3de", help="set foreground color hex code [Defaults to 2ac3de]") | |
parser.add_argument("-bg", metavar="hex_code", type=str, default="1a1b26", help="set foreground color hex code [Defaults to 1a1b26]") #1b1b27 is nice too lol | |
parser.add_argument("-p", metavar="icon_dir", type=str, default="", help="use icons from a specified folder") | |
parser.add_argument("-o", metavar="output", type=str, default="wallpaper.png", help="select output file destination [Defaults to ./wallpaper.png]") | |
args = parser.parse_args() | |
hex_to_rgb = lambda x : tuple(int(x.lstrip('#')[i:i+2], 16) for i in (0, 2, 4)) | |
generate_wallpaper( | |
size=tuple(int(_) for _ in args.r.split('x')[:2]), | |
icon_size=tuple(int(_) for _ in args.icon.split(',')[:2]), | |
rotation_range=tuple(int(_) for _ in args.rt.split(',')[:2]), | |
fg_color=hex_to_rgb(args.fg), | |
bg_color=hex_to_rgb(args.bg), | |
local_icon_dir=args.p, | |
).save(args.o) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Special thanks to the good folks who made this -> https://avogadr.io/ | |
# For direct usage without download, do `curl <raw_url> | python - [args]` | |
import os | |
import random | |
import argparse | |
try: | |
import PIL, requests | |
from PIL import Image | |
except ImportError: | |
if os.name == "nt": # Yeah, this failsafe is mostly for windows users | |
os.system("py -m pip install Pillow") | |
os.system("py -m pip install requests") | |
import PIL, requests | |
from PIL import Image | |
else: | |
print("Please install the necessary requirements") | |
headers = { | |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0" | |
} | |
## Edit this list to determine which compounds show up | |
COMPOUNDS = [ | |
"acetic%20acid", | |
"acyclovir", | |
"adrenaline", | |
"aldosterone", | |
"amoxicillin", | |
"amphetamine", | |
"aspartate", | |
"aspirin", | |
"atp", | |
"benzene", | |
"caffeine", | |
"capsaicin", | |
"carbon%20dioxide", | |
"ceftriaxone", | |
"chlorophyll", | |
"cocaine", | |
"cortisol", | |
"cyclobutane", | |
"diazepam", | |
"diclofenac", | |
"dopamine", | |
"estradiol", | |
"ethanol", | |
"fentanyl", | |
"fructose", | |
"glucose", | |
"glycerol", | |
"glycine", | |
"heroin", | |
"histamine", | |
"hydrochlorothiazide", | |
"hydrocortisone", | |
"ibuprofen", | |
"lactose", | |
"lidocaine", | |
"lsd", | |
"luciferin", | |
"melatonin", | |
"methamphetamine", | |
"morphine", | |
"nicotine", | |
"noradrenaline", | |
"oxytocin", | |
"ozone", | |
"paracetamol", | |
"penicillin v", | |
"progesterone", | |
"salt", | |
"secretin", | |
"serotonin", | |
"sucrose", | |
"testosterone", | |
"tmp", | |
"truvada", | |
"vitamin%20a", | |
"vitamin%20b1", | |
"vitamin%20b2", | |
"vitamin%20b3", | |
"vitamin%20b5", | |
"vitamin%20b6", | |
"vitamin%20b7", | |
"vitamin%20b9", | |
"vitamin%20c", | |
"vitamin%20d", | |
"vitamin%20e", | |
"warfarin", | |
"xanax", | |
"xylitol ", | |
] | |
## For example, you could have the list like so to have only a single compound | |
# COMPOUNDS = ["cocaine"] ## LMAO | |
parser = argparse.ArgumentParser(description="Script to generate high res wallpapers of your favorite chemical compounds") | |
parser.version = "1.0 [beta]" | |
parser.add_argument("-v", "--version", action="version") | |
parser.add_argument("-s", metavar="size", type=str, help="set canvas size (resolution), like <width>x<height> [Defaults to 1440x2560]") | |
parser.add_argument("-fg", metavar="hex_code", type=str, help="set foreground color hex code. [Defaults to FC9B62]") | |
parser.add_argument("-bg", metavar="RGB_code", type=str, help="set foreground color RGB code, like R,G,B [Defaults to 19,20,28]") | |
parser.add_argument("-r", metavar="bool", type=str, help="set whether compounds should be randomly rotated or not [Defaults to 1]") | |
args = parser.parse_args() | |
WIDTH, HEIGHT = args.s.lower().split("x")[:2] if args.s else (1440, 2560) # canvas resolution | |
FG_COLOR = args.fg.strip() if args.fg else "FC9B62" # foreground hex code | |
BG_COLOR = tuple(int(_) for _ in args.bg.split(',')[:3]) if args.bg else (19, 20, 28) # background RGB code | |
ROTATE = True if not args.r else args.r == "1" # randomly rotates the compounds | |
def generate_canvas(size=(WIDTH, HEIGHT), bg_color=BG_COLOR) -> Image: | |
with Image.new(mode="RGBA", size=size, color=bg_color) as canvas: | |
return canvas | |
def draw_compounds(base: Image) -> Image: | |
x, y = 0, 0 | |
print("Progress:", end=" ", flush=True) | |
while y <= HEIGHT: | |
heights = [] | |
while x <= WIDTH: | |
print(f"{round(y*100/HEIGHT, 1)}%".rjust(5, "0"), end="", flush=True) # Display progress | |
with requests.get( | |
f"https://avogadr.io/api/name/{random.randint(WIDTH//3,WIDTH//1.8)}/{random.randint(HEIGHT//3,HEIGHT//1.8)}/{FG_COLOR}/{random.choice(COMPOUNDS)}", | |
params={"rotation": 0 if not ROTATE else random.randint(0, 10)}, | |
headers=headers, | |
) as r, open("compound.png", mode="wb") as compound_raw: | |
compound_raw.write(r.content) | |
try: | |
with Image.open("compound.png").convert("RGBA") as compound: | |
width, height = compound.size | |
base.paste(compound, (x, y), compound) # 3rd param for transparency mask | |
# base.save("wall.png") # Uncomment to see changes in real time | |
x += round(0.97 * width) | |
heights.append(height) | |
except PIL.UnidentifiedImageError: | |
pass | |
print("\b" * 5, end="", flush=True) | |
y += round(0.97 * max(heights)) | |
x = 0 | |
print("100% "); os.remove("compound.png") | |
return base | |
if __name__ == "__main__": | |
draw_compounds(generate_canvas()).save("wall.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pillow==9.0.1 | |
requests==2.27.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment