python以环状形式组合排列图片并输出的方法

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

用Python登录Gmail并发送Gmail邮件的教程

 这篇快文介绍了使用Gmail作为您的e-mail服务器,通过Python的内置SMTP库发送电子邮件。它并不复杂,我保证。 下面是如何在Python中登录GMail: &nb...

python re模块的高级用法详解

python re模块的高级用法详解

总结 以上所述是小编给大家介绍的python re模块的高级用法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于P...

Ubuntu下使用python读取doc和docx文档的内容方法

读取docx文档 使用的包是python-docx 1. 安装python-docx包 sudo pip install python-docx 2. 使用python-docx...

python列表list保留顺序去重的实例

常规通过迭代或set方法,都无法保证去重后的顺序问题 如下,我们可以通过列表的索引功能,对set结果进行序列化 old_list=["a",1,"b","a","b",2,5,1]...

Python实现合并同一个文件夹下所有PDF文件的方法示例

Python实现合并同一个文件夹下所有PDF文件的方法示例

本文实例讲述了Python实现合并同一个文件夹下所有PDF文件的方法。分享给大家供大家参考,具体如下: 一、需求说明 下载了网易云课堂的吴恩达免费的深度学习的pdf文档,但是每一节是一个...