python以環(huán)狀形式組合排列圖片并輸出的方法
本文實(shí)例講述了python以環(huán)狀形式組合排列圖片并輸出的方法。分享給大家供大家參考。具體分析如下:
這段代碼可以自定義一個空白畫板,然后將指定的圖片以圓環(huán)狀的方式排列起來,用到了pil庫,可以通過:
pip install pil 的方式安裝。
具體代碼如下:
__author__ = 'm.fzitv.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個 50x50 的pngs 圖片,使用了絕對路徑,需要自己進(jìn)行替換成你的圖片路徑
imageFilenames = ["d:/m.fzitv.net/images/1.png", "d:/m.fzitv.net/images/2.png", "d:/m.fzitv.net/images/3.png"] * 5
images = [Image.open(filename) for filename in imageFilenames]
arrangeImagesInCircle(img, images)
img.save("output.png")
希望本文所述對大家的Python程序設(shè)計有所幫助。
- python 排列組合之itertools
- Python實(shí)現(xiàn)的排列組合計算操作示例
- Python2.7基于笛卡爾積算法實(shí)現(xiàn)N個數(shù)組的排列組合運(yùn)算示例
- Python實(shí)現(xiàn)的簡單排列組合算法示例
- Python編程之黑板上排列組合,你舍得解開嗎
- Python使用itertools模塊實(shí)現(xiàn)排列組合功能示例
- Python使用combinations實(shí)現(xiàn)排列組合的方法
- python實(shí)現(xiàn)求解列表中元素的排列和組合問題
- Python 列表(List)操作方法詳解
- Python中列表(list)操作方法匯總
- Python列表list排列組合操作示例
相關(guān)文章
Keras中Sequential模型和Functional模型的區(qū)別及說明
這篇文章主要介紹了Keras中Sequential模型和Functional模型的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
用python實(shí)現(xiàn)刷點(diǎn)擊率的示例代碼
今天小編就為大家分享一篇用python實(shí)現(xiàn)刷點(diǎn)擊率的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
利用python將xml文件解析成html文件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇利用python將xml文件解析成html文件的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
詳解Python虛擬機(jī)是如何實(shí)現(xiàn)閉包的
Python中的閉包是一個強(qiáng)大的概念,允許函數(shù)捕獲和訪問其周圍的作用域,即使這些作用域在函數(shù)執(zhí)行完畢后也能被訪問,這篇文章將著重討論P(yáng)ython虛擬機(jī)是如何實(shí)現(xiàn)閉包的,文中有相關(guān)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-12-12
Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問題完整實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者問題,簡單描述了生產(chǎn)者、消費(fèi)者問題的概念、原理,并結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)生產(chǎn)者、消費(fèi)者問題的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
PyQt5+QtChart實(shí)現(xiàn)繪制區(qū)域圖
QChart是一個QGraphicScene中可以顯示的QGraphicsWidget。本文將利用QtChart實(shí)現(xiàn)區(qū)域圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12

