使用python以循环方式裁剪图像 [英] cropping an image in a circular way, using python
本文介绍了使用python以循环方式裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个以循环方式裁剪图像的脚本.
I want to create a script that crops an image in a circular way.
我有一个服务器,可以接收所有类型的图片(大小相同),并且希望服务器裁剪接收到的图像.
I have a server which receives all kind of pictures (all of the same size) and I want the server to crop the received image.
例如,打开此图像:
对此:
我希望能够将其另存为PNG(具有透明背景).
I want to be able to save it as a PNG (with a transparent background).
这怎么办?
推荐答案
这里是一种方法:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image, ImageDraw
# Open the input image as numpy array, convert to RGB
img=Image.open("dog.jpg").convert("RGB")
npImage=np.array(img)
h,w=img.size
# Create same size alpha layer with circle
alpha = Image.new('L', img.size,0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0,0,h,w],0,360,fill=255)
# Convert alpha Image to numpy array
npAlpha=np.array(alpha)
# Add alpha layer to RGB
npImage=np.dstack((npImage,npAlpha))
# Save with alpha
Image.fromarray(npImage).save('result.png')
这篇关于使用python以循环方式裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文