Python's way of arranging pictures and outputting them in a ring form _python

Source: Internet
Author: User

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.

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.