The examples in this article describe how Python arranges pictures and outputs in a looped fashion. Share to everyone for your reference. The specific analysis is as follows:
This code can customize a blank artboard, and then arrange the specified pictures in a doughnut-like manner, using the PIL library, which you can use:
Pip install PIL installation.
The specific code is as follows:
Copy Code code as follows:
#-*-Coding:utf-8-*-
__author__ = ' www.jb51.net '
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 the way to the edge of the image.
#If we do so, then when we paste images onto the circle, those images'll partially 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 we images would go.
#so we must subtract half the height/width of the image to find where their top-left the corners 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, using an absolute path, you need to replace it with your image path
Imagefilenames = ["D:/www.jb51.net/images/1.png", "D:/www.jb51.net/images/2.png", "d:/www.jb51.net/images/3.png"] * 5
Images = [Image.open (filename) for filename in imagefilenames]
Arrangeimagesincircle (img, images)
Img.save ("Output.png")
I hope this article will help you with your Python programming.