Python uses a combination of loops to arrange and output images

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.