python将画布上的图像排列成一个圆圈

python将画布上的图像排列成一个圆圈,python,python-imaging-library,Python,Python Imaging Library,我有很多图片,比如说10张,我已经生成了数组或者PIL对象 我需要将它们集成到一个循环的方式来显示它们,并且它应该根据屏幕的分辨率进行自我调整,python中有什么东西可以做到这一点吗 我尝试过使用粘贴,但是弄清楚分辨率画布和粘贴位置很痛苦,想知道是否有更简单的解决方案?我们可以说,当相邻点之间存在恒定角度θ时,点均匀地排列在一个圆中。θ可以计算为2*pi弧度除以点数。第一个点与x轴成0角,第二个点与θ*1角,第三个点与θ*2角,以此类推 使用简单的三角学,可以找到圆边上任何点的X和Y坐标。对于

我有很多图片,比如说10张,我已经生成了数组或者PIL对象

我需要将它们集成到一个循环的方式来显示它们,并且它应该根据屏幕的分辨率进行自我调整,python中有什么东西可以做到这一点吗


我尝试过使用粘贴,但是弄清楚分辨率画布和粘贴位置很痛苦,想知道是否有更简单的解决方案?

我们可以说,当相邻点之间存在恒定角度θ时,点均匀地排列在一个圆中。θ可以计算为2*pi弧度除以点数。第一个点与x轴成0角,第二个点与θ*1角,第三个点与θ*2角,以此类推

使用简单的三角学,可以找到圆边上任何点的X和Y坐标。对于位于半径为r的圆上的角度为ohm的点:

结果:


我们可以说,当相邻点之间存在恒定角度θ时,点均匀地排列在一个圆中。θ可以计算为2*pi弧度除以点数。第一个点与x轴成0角,第二个点与θ*1角,第三个点与θ*2角,以此类推

使用简单的三角学,可以找到圆边上任何点的X和Y坐标。对于位于半径为r的圆上的角度为ohm的点:

结果:

xFromCenter = r*cos(ohm) yFromCenter = r*sin(ohm)
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, curImg in enumerate(imagesToArrange):
        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 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))

#red.png, blue.png, green.png are simple 50x50 pngs of solid color
imageFilenames = ["red.png", "blue.png", "green.png"] * 5
images = [Image.open(filename) for filename in imageFilenames]

arrangeImagesInCircle(img, images)

img.save("output.png")