python以環(huán)狀形式組合排列圖片并輸出的方法
本文實(shí)例講述了python以環(huán)狀形式組合排列圖片并輸出的方法。分享給大家供大家參考。具體分析如下:
這段代碼可以自定義一個(gè)空白畫板,然后將指定的圖片以圓環(huán)狀的方式排列起來,用到了pil庫(kù),可以通過:
pip install pil 的方式安裝。
具體代碼如下:
__author__ = 'www.dbjr.com.cn'
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))
#下面的三個(gè)圖片是3個(gè) 50x50 的pngs 圖片,使用了絕對(duì)路徑,需要自己進(jìn)行替換成你的圖片路徑
imageFilenames = ["d:/www.dbjr.com.cn/images/1.png", "d:/www.dbjr.com.cn/images/2.png", "d:/www.dbjr.com.cn/images/3.png"] * 5
images = [Image.open(filename) for filename in imageFilenames]
arrangeImagesInCircle(img, images)
img.save("output.png")
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- python 排列組合之itertools
- Python實(shí)現(xiàn)的排列組合計(jì)算操作示例
- Python2.7基于笛卡爾積算法實(shí)現(xiàn)N個(gè)數(shù)組的排列組合運(yùn)算示例
- Python實(shí)現(xiàn)的簡(jiǎn)單排列組合算法示例
- Python編程之黑板上排列組合,你舍得解開嗎
- Python使用itertools模塊實(shí)現(xiàn)排列組合功能示例
- Python使用combinations實(shí)現(xiàn)排列組合的方法
- python實(shí)現(xiàn)求解列表中元素的排列和組合問題
- Python 列表(List)操作方法詳解
- Python中列表(list)操作方法匯總
- Python列表list排列組合操作示例
相關(guān)文章
Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境的教程圖解
這篇文章主要介紹了Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06python+mysql實(shí)現(xiàn)教務(wù)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python+mysql實(shí)現(xiàn)教務(wù)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Pytorch mask_select 函數(shù)的用法詳解
今天小編就為大家分享一篇Pytorch mask_select 函數(shù)的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解
今天小編就為大家分享一篇對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12django創(chuàng)建簡(jiǎn)單的頁面響應(yīng)實(shí)例教程
這篇文章主要給大家介紹了關(guān)于django如何創(chuàng)建簡(jiǎn)單的頁面響應(yīng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式
今天小編就為大家分享一篇pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python中Timedelta轉(zhuǎn)換為Int或Float方式
這篇文章主要介紹了Python中Timedelta轉(zhuǎn)換為Int或Float方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07