Skip to content

Instantly share code, notes, and snippets.

@zclongpop123
Created April 14, 2020 15:12
Show Gist options
  • Save zclongpop123/674ead536f11607602b8e78b0ae42676 to your computer and use it in GitHub Desktop.
Save zclongpop123/674ead536f11607602b8e78b0ae42676 to your computer and use it in GitHub Desktop.
添加图片半透明蒙版
#========================================
# author: Changlong.Zang
# mail: [email protected]
# time: Tue Apr 14 22:22:51 2020
#========================================
from PIL import Image, ImageDraw
#--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
def main():
'''
'''
with Image.open('C:/Users/zangchanglong/Desktop/the_lion_king-002.jpg') as img:
img = img.convert('RGBA')
mask = Image.new('RGBA', (img.width, img.height))
draw = ImageDraw.Draw(mask)
draw.rectangle(((0, 0), (240, img.height)), fill=(0, 0, 0, 128))
draw.rectangle(((img.width - 240, 0), (img.width, img.height)), fill=(0, 0, 0, 128))
img.alpha_composite(mask)
img = img.convert('RGB')
img.save('C:/Users/zangchanglong/Desktop/the_lion_king-003.jpg', 'jpeg')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment