LoginSignup
0
0

More than 5 years have passed since last update.

pillow で図形をトリムして透過にする

Posted at

こちらのプログラムを改造して、変換されら部分を透過にしました。
pillow で図形をトリム
入力データ
in01.png

出力データ
out01.png

circle_transparent.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   circle_transparent.py
#
#                       Oct/02/2018
#
# --------------------------------------------------------------------
import sys
import math
from PIL import Image
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_in = sys.argv[1]
file_out = sys.argv[2]
sys.stderr.write(file_in + "\n")
sys.stderr.write(file_out + "\n")
#
img = Image.open(file_in)
img = img.convert("RGBA")
pixdata = img.load()

width, height = img.size
sys.stderr.write("%d\t%d\n" % (width,height))
#
xx_center = width / 2
yy_center = height / 2
rr_max = 90
#
for xx in range (width):
    for yy in range (height):
        delt_x = abs (xx_center - xx)
        delt_y = abs (yy_center - yy)
        arm = math.sqrt (delt_x * delt_x + delt_y * delt_y)
        if (rr_max < arm):
            pixdata[xx, yy] = (255, 255, 255, 0)
#
img.save(file_out)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

実行方法

./circle_transparent.py in01.png out01.png
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0