python以环状形式组合排列图片并输出的方法
本文实例讲述了python以环状形式组合排列图片并输出的方法。分享给大家供大家参考。具体分析如下:
这段代码可以自定义一个空白画板,然后将指定的图片以圆环状的方式排列起来,用到了pil库,可以通过:
pipinstallpil的方式安装。
具体代码如下:
#-*-coding:utf-8-*-
__author__='www.nhooo.com'
importmath
fromPILimportImage
defarrangeImagesInCircle(masterImage,imagesToArrange):
imgWidth,imgHeight=masterImage.size
#wewantthecircletobeaslargeaspossible.
#butthecircleshouldn'textendallthewaytotheedgeoftheimage.
#Ifwedothat,thenwhenwepasteimagesontothecircle,thoseimageswillpartiallyfallovertheedge.
#sowereducethediameterofthecirclebythewidth/heightofthewidest/tallestimage.
diameter=min(
imgWidth -max(img.size[0]forimginimagesToArrange),
imgHeight-max(img.size[1]forimginimagesToArrange)
)
radius=diameter/2
circleCenterX=imgWidth /2
circleCenterY=imgHeight/2
theta=2*math.pi/len(imagesToArrange)
foriinrange(len(imagesToArrange)):
curImg=imagesToArrange[i]
angle=i*theta
dx=int(radius*math.cos(angle))
dy=int(radius*math.sin(angle))
#dxanddygivethecoordinatesofwherethecenterofourimageswouldgo.
#sowemustsubtracthalftheheight/widthoftheimagetofindwheretheirtop-leftcornersshouldbe.
pos=(
circleCenterX+dx-curImg.size[0]/2,
circleCenterY+dy-curImg.size[1]/2
)
masterImage.paste(curImg,pos)
img=Image.new("RGB",(500,500),(255,255,255))
#下面的三个图片是3个50x50的pngs图片,使用了绝对路径,需要自己进行替换成你的图片路径
imageFilenames=["d:/www.nhooo.com/images/1.png","d:/www.nhooo.com/images/2.png","d:/www.nhooo.com/images/3.png"]*5
images=[Image.open(filename)forfilenameinimageFilenames]
arrangeImagesInCircle(img,images)
img.save("output.png")
希望本文所述对大家的Python程序设计有所帮助。