LoginSignup
0
0

More than 3 years have passed since last update.

PILで背景透過。(putalphaがNoneType Objectを返す時の対処法)【Pillow】

Last updated at Posted at 2020-09-28

fashion-wear-of-a-man-in-black-long-sleeve-3678375.jpg4.jpg4.png
たとえば、上記のような画像にマスク画像を適用して背景を透過したいとします。
以下でOK。


from PIL import Image

img.putalpha(mask)
img.save(writepath)

このときに、作った背景透過画像を変数に当て込むとエラーになります。
putalphaメソッドがNoneタイプのオブジェクトを返すからだそうです。

from PIL import Image

img = img.putalpha(mask)
img.save(writepath)
""" PIL: AttributeError: 'NoneType' object has no attribute 'save'

最初のコードとのちがいわかりましたか?
最初のコードでやると保存できます。

ちなみに、人型のマスクで切り抜く方法はこちらの記事

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
MLBoysチャンネル
Medium

相棒
note

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