Skip to content

Instantly share code, notes, and snippets.

@MayerDaniel
Last active July 12, 2021 19:32
Show Gist options
  • Save MayerDaniel/4494d6a7a856521544356a34026ecc8f to your computer and use it in GitHub Desktop.
Save MayerDaniel/4494d6a7a856521544356a34026ecc8f to your computer and use it in GitHub Desktop.
'''
background_assets.py
usage:
background_assets.py /path/or/glob/to/files/*.png
a small image manipulation script with hardcoded presets to help my wonderful
girlfriend automate asset creation for her game and teach me about PIL
It requires python3 (which is likely already installed) and pillow (https://pillow.readthedocs.io/en/stable/)
pillow can be installed using pip: "pip3 install pillow"
pip can be installed using this guide: https://pip.pypa.io/en/stable/installing/
'''
from pathlib import Path
from PIL import Image, ImageFilter, ImageEnhance
import sys
import os
def blur(path,r=0xae,g=0x86,b=0xf0,t=0.23,rad=2.0):
'''
takes the following arguments (defaults are middle blur):
path of the file
red, green, blue arguments for the tint
the transparency percentage
the radius of the gaussian blur
'''
img = Image.open(path)
img = img.convert("RGBA")
datas = img.getdata()
newData = []
for item in datas:
if item[3] > 0:
newData.append((r, g, b, int(255 * t)))
else:
newData.append((r, g, b, 0))
img.putdata(newData)
img2 = Image.open(path)
img2 = img2.convert("RGBA")
out = Image.alpha_composite(img2, img)
out = out.filter(ImageFilter.GaussianBlur(radius = rad))
return out
if __name__ == '__main__':
'''
hardcoded presets
'''
blur_args = (0xae,0x86,0xf0,0.23,2.0)
dblur_args = (0xe5,0xd2,0xd7,0.42,4.3)
Path("./middleblur").mkdir(parents=True, exist_ok=True)
Path("./deepblur").mkdir(parents=True, exist_ok=True)
for input_image_path in sys.argv[1:]:
root, ext = os.path.splitext(os.path.basename(input_image_path))
mb_image_path = './middleblur/'+root+'_Blur'+ext
db_image_path = './deepblur/'+root+'_DBlur'+ext
print('middle blurring "{}"'.format(input_image_path))
blur(input_image_path,*blur_args).save(mb_image_path, 'PNG')
print('deep blurring "{}"'.format(input_image_path))
blur(input_image_path,*dblur_args).save(db_image_path, 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment