Skip to content

Instantly share code, notes, and snippets.

@puhitaku
Created November 5, 2020 09:49
Show Gist options
  • Save puhitaku/2770c976539fc6f406f27484e96c83f8 to your computer and use it in GitHub Desktop.
Save puhitaku/2770c976539fc6f406f27484e96c83f8 to your computer and use it in GitHub Desktop.
透明背景に黒一色なPNG文字絵文字をちょっと縮小して白フチ付けるやつ
# Runs on Python 3.8 + Pillow
# Should run also on Python 3.7+
from pathlib import Path
from PIL import Image, ImageOps, ImageFilter
def main():
Path('./stroked').mkdir(exist_ok=True)
for fn in Path('.').glob('*.png'):
strokify(fn)
def strokify(fn):
orig = Image.open(fn)
orig.thumbnail((120, 120), Image.BILINEAR)
stroke = Image.new('RGB', (128, 128), (255, 255, 255))
stroke.paste(orig, (4, 4), mask=orig)
stroke = ImageOps.invert(stroke)
stroke = stroke.filter(ImageFilter.MaxFilter(9)).convert('L')
output = Image.new('RGBA', (128, 128), (255, 255, 255, 0))
white = Image.new('RGB', (128, 128), (255, 255, 255))
output.paste(white, mask=stroke)
output.paste(orig, (4, 4), mask=orig)
output.save(f'./stroked/{fn}')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment