python以環狀形式組合排列圖片並輸出的方法

NO IMAGE

本文例項講述了python以環狀形式組合排列圖片並輸出的方法。分享給大家供大家參考。具體分析如下:

這段程式碼可以自定義一個空白畫板,然後將指定的圖片以圓環狀的方式排列起來,用到了pil庫,可以通過:
pip install pil 的方式安裝。

具體程式碼如下:
複製程式碼 程式碼如下:# -*- 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 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 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))
#下面的三個圖片是3個 50×50 的pngs 圖片,使用了絕對路徑,需要自己進行替換成你的圖片路徑
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”)

希望本文所述對大家的Python程式設計有所幫助。

您可能感興趣的文章:

Python2.7基於笛卡爾積演算法實現N個陣列的排列組合運算示例python標準演算法實現陣列全排列的方法python 排列組合之itertoolsPython實現的排列組合計算操作示例python實現求解列表中元素的排列和組合問題Python程式設計之黑板上排列組合,你捨得解開嗎Python使用itertools模組實現排列組合功能示例Python實現的簡單排列組合演算法示例