国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-23 00:22:07
字體:
來源:轉載
供稿:網友

本文實例講述了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個 50x50 的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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德化县| 澜沧| 崇礼县| 承德县| 昌都县| 古蔺县| 漯河市| 于田县| 隆德县| 平陆县| 克东县| 江阴市| 嘉兴市| 乌兰浩特市| 绥棱县| 张家川| 柯坪县| 界首市| 金坛市| 柘城县| 伊金霍洛旗| 濮阳县| 高清| 宜黄县| 左贡县| 嘉义县| 梁平县| 北票市| 禹城市| 荆门市| 孟津县| 德兴市| 唐山市| 华亭县| 乐昌市| 郴州市| 沧州市| 固原市| 扶风县| 琼结县| 确山县|