2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pillow で画像に余白を追加

Posted at

次のページを参考にしました。
Python, Pillowで画像の上下左右に余白を追加しサイズ変更

入力データ
in01.jpg

出力データ
out01.png

add_mergin.py
# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	add_mergin.py
#
#						Oct/02/2018
#
# --------------------------------------------------------------------
import sys
from PIL import Image
# --------------------------------------------------------------------
def add_margin(pil_img, top, right, bottom, left, color):
	width, height = pil_img.size
	new_width = width + right + left
	new_height = height + top + bottom
	result = Image.new(pil_img.mode, (new_width, new_height), color)
	result.paste(pil_img, (left, top))
	return result
# --------------------------------------------------------------------
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")
width, height = img.size
sys.stderr.write("%d\t%d\n" % (width,height))
#
top = 60
right = 40
left = 40
bottom = 0
img = add_margin(img, top, right, bottom, left, (0, 0, 255))
#
width, height = img.size
sys.stderr.write("%d\t%d\n" % (width,height))
#
img.save(file_out)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------

実行方法

./add_mergin.py in01.jpg out01.png
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?