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


Posted in Python onMarch 17, 2015

本文实例讲述了python以环状形式组合排列图片并输出的方法。分享给大家供大家参考。具体分析如下:

这段代码可以自定义一个空白画板,然后将指定的图片以圆环状的方式排列起来,用到了pil库,可以通过:
pip install pil 的方式安装。

具体代码如下:

# -*- coding: utf-8 -*-

__author__ = '3water.com'

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:/3water.com/images/1.png", "d:/3water.com/images/2.png", "d:/3water.com/images/3.png"] * 5

images = [Image.open(filename) for filename in imageFilenames]

arrangeImagesInCircle(img, images)

img.save("output.png")

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Windows下用py2exe将Python程序打包成exe程序的教程
Apr 08 Python
Python实现对比不同字体中的同一字符的显示效果
Apr 23 Python
简单谈谈python的反射机制
Jun 28 Python
浅析Python中yield关键词的作用与用法
Nov 29 Python
Python实现查找匹配项作处理后再替换回去的方法
Jun 10 Python
python分析作业提交情况
Nov 22 Python
python写入已存在的excel数据实例
May 03 Python
如何使用python3获取当前路径及os.path.dirname的使用
Dec 13 Python
python getopt模块使用实例解析
Dec 18 Python
python输出pdf文档的实例
Feb 13 Python
Python pysnmp使用方法及代码实例
Aug 24 Python
Python操作CSV格式文件的方法大全
Jul 15 Python
python将ip地址转换成整数的方法
Mar 17 #Python
python实现模拟按键,自动翻页看u17漫画
Mar 17 #Python
python通过pil为png图片填充上背景颜色的方法
Mar 17 #Python
python按照多个字符对字符串进行分割的方法
Mar 17 #Python
python通过floor函数舍弃小数位的方法
Mar 17 #Python
python常规方法实现数组的全排列
Mar 17 #Python
python标准算法实现数组全排列的方法
Mar 17 #Python
You might like
PHP入门速成(2)
2006/10/09 PHP
Thinkphp5+uploadify实现的文件上传功能示例
2018/05/26 PHP
jQuery中复合属性选择器用法实例
2014/12/31 Javascript
JavaScript数据结构与算法之集合(Set)
2016/01/29 Javascript
javascript+HTML5 Canvas绘制转盘抽奖
2020/05/16 Javascript
jquery获取复选框checkbox的值的简单实现方法
2016/05/26 Javascript
JS小数运算出现多为小数问题的解决方法
2016/06/02 Javascript
jQuery实用小技巧_输入框文字获取和失去焦点的简单实例
2016/08/25 Javascript
Javascript中函数名.length属性用法分析(对比arguments.length)
2016/09/16 Javascript
JavaScript之WebSocket技术详解
2016/11/18 Javascript
在vue-cli项目中使用bootstrap的方法示例
2018/04/21 Javascript
Node.js中的child_process模块详解
2018/06/08 Javascript
vue和webpack安装命令详解
2018/06/15 Javascript
详解SPA中前端路由基本原理与实现方式
2018/09/12 Javascript
vue给对象动态添加属性和值的实例
2019/09/09 Javascript
从表单校验看JavaScript策略模式的使用详解
2020/10/17 Javascript
python实现计算资源图标crc值的方法
2014/10/05 Python
python中__call__内置函数用法实例
2015/06/04 Python
Python随机函数库random的使用方法详解
2019/08/21 Python
基于Python和PyYAML读取yaml配置文件数据
2020/01/13 Python
Django Haystack 全文检索与关键词高亮的实现
2020/02/17 Python
python实现IOU计算案例
2020/04/12 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
css3模拟jq点击事件的实例代码
2017/07/06 HTML / CSS
墨尔本复古时尚品牌:Dangerfield
2018/12/12 全球购物
Python面试题集
2012/03/08 面试题
servlet面试题
2012/08/20 面试题
目标责任书范本
2014/04/16 职场文书
小学生环保标语
2014/06/13 职场文书
工伤事故赔偿协议书范文
2014/09/24 职场文书
2014年后备干部工作总结
2014/12/08 职场文书
幼儿园2015年度工作总结
2015/04/01 职场文书
农业项目合作意向书
2015/05/08 职场文书
读《茶花女》有感:山茶花的盛开与凋零
2020/01/17 职场文书
Vue3中的Refs和Ref详情
2021/11/11 Vue.js
sql注入报错之注入原理实例解析
2022/06/10 MySQL