A method by which Python arranges pictures and outputs in a ring form

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

  • Related Article

    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.