2012-11-14 2 views
0

이미지 (예 : 10)의 무리가 배열 또는 PIL 개체로 생성되었습니다.파이썬은 캔버스에 원을 그리면서 이미지를 배열합니다.

그들을 원형으로 통합하여 표시해야하며 화면의 해상도에 맞게 조정해야합니다. 파이썬에서는이 작업을 수행 할 수있는 것이 무엇입니까?

나는 붙여 넣기를 사용하려했지만 해상도 캔버스와 붙여 넣기 위치를 알아내는 것이 쉽지 않은가요?

답변

5

인접한 점 사이에 일정한 각도 theta이있을 때 점들이 원으로 균일하게 배치된다고 말할 수 있습니다. theta은 2 * pi 라디안을 포인트 수로 나눈 값으로 계산할 수 있습니다. 첫번째 점은 간단한 삼각법 사용 등

X 축 각도 theta*1 각도 theta*2에서 세 번째 시점에서 제 포인트에 대하여 각 0에있을 경우에는 X를 찾을 수 있고, Y는 임의의 점의 좌표를 해당 원의 가장자리에 놓여있다. 반경 r와 원에 누워 각도 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") 

결과 :

이 수학을 사용

 
xFromCenter = r*cos(ohm) 
yFromCenter = r*sin(ohm) 

, 원에 균일하게 이미지를 배치 할 수있다 enter image description here

+0

우수 답변! – btel

관련 문제