This example describes how Python arranges pictures and outputs in a ring form. Share to everyone for your reference. The specific analysis is as follows:
This code can customize a blank artboard and then arrange the specified picture in a doughnut-like manner, using the PIL library, which can be used:
PIP installs the PiL way.
The specific code is as follows:
The code is as follows:
#-*-Coding:utf-8-*-
__author__ = ' www.bitsCN.com '
Import Math
From PIL import Image
def arrangeimagesincircle (Masterimage, Imagestoarrange):
ImgWidth, imgheight = masterimage.size
#we want the circle to be as large as possible.
#but the circle shouldn ' t extend all the the-the-to-edge of the image.
#If we do and then when we paste images onto the circle, those images would partially fall over the edge.
#so we reduce the diameter of the circle by the width/height of the widest/tallest image.
diameter = min (
Imgwidth-max (img.size[0] for IMG in Imagestoarrange),
Imgheight-max (img.size[1] for IMG in Imagestoarrange)
)
Radius = DIAMETER/2
Circlecenterx = IMGWIDTH/2
Circlecentery = IMGHEIGHT/2
theta = 2*math.pi/len (imagestoarrange)
For I in range (len (imagestoarrange)):
curimg = Imagestoarrange[i]
Angle = i * theta
DX = Int (RADIUS * Math.Cos (angle))
dy = Int (RADIUS * Math.sin (angle))
#dx and Dy give the coordinates of where the center of our images would go.
#so we must subtract half the height/width of the image to find where their top-left corners should is.
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 picture, use absolute path and need to replace it yourself with your picture path
Imagefilenames = ["D:/www.bitscn.com/images/1.png", "D:/www.bitscn.com/images/2.png", "D:/WWW.BITSCN.COM/IMAGES/3". PNG "] * 5
Images = [Image.open (filename) for filename in imagefilenames]
Arrangeimagesincircle (img, images)
Img.save ("Output.png")
Hopefully this article will help you with Python programming.