LoginSignup
0
0

More than 5 years have passed since last update.

pillow で図形をトリム

Posted at

入力データ
in01.jpg

出力データ
out01.jpg

circle_trim.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   circle_trim.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)
if img.mode != "RGB":
    img = img.convert("RGB")
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):
                img.putpixel((xx, yy), (255, 255, 255))
#
img.save(file_out)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

実行方法

./circle_trim.py in01.jpg out01.jpg
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