This article mainly introduces how to combine and output images in a ring form in python. it involves Python's pil library-based image operation skills and has some reference value, for more information, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
This code can customize a blank canvas, and then arrange the specified images in a ring-like manner, using the pil library, you can use:
Pip install pil.
The 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 way to the edge of the image.
# If we do that, then when we paste images onto the circle, those images will 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 wocould go.
# So we must subtract half the height/width of the image to find where their top-left corners shoshould be.
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 ))
# The following three images are 3 50x50 pngs images. if you use an absolute path, replace it with your image 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 ")
I hope this article will help you with Python programming.