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 相关文章推荐
Python列表推导式的使用方法
Nov 21 Python
python遍历类中所有成员的方法
Mar 18 Python
Flask框架的学习指南之用户登录管理
Nov 20 Python
深入理解Python单元测试unittest的使用示例
Nov 18 Python
python开启摄像头以及深度学习实现目标检测方法
Aug 03 Python
将Python字符串生成PDF的实例代码详解
May 17 Python
pytest中文文档之编写断言
Sep 12 Python
python模块hashlib(加密服务)知识点讲解
Nov 25 Python
节日快乐! Python画一棵圣诞树送给你
Dec 24 Python
详解基于Facecognition+Opencv快速搭建人脸识别及跟踪应用
Jan 21 Python
Python 流媒体播放器的实现(基于VLC)
Apr 28 Python
python中mongodb包操作数据库
Apr 19 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中的字符串函数
2006/11/24 PHP
php中time()和mktime()方法的区别
2013/09/28 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(六)
2014/06/23 PHP
PHP中的Trait 特性及作用
2016/04/03 PHP
PHP使用XMLWriter读写xml文件操作详解
2018/07/31 PHP
javascript页面加载完执行事件代码
2014/02/11 Javascript
在JavaScript中构建ArrayList示例代码
2014/09/17 Javascript
JS实现网页表格自动变大缩小的方法
2015/03/09 Javascript
jquery选择器简述
2015/08/31 Javascript
Javascript中的Prototype到底是什么
2016/02/16 Javascript
jQuery EasyUI 右键菜单--关闭标签/选项卡的简单实例
2016/10/10 Javascript
angularjs select 赋值 ng-options配置方法
2018/02/28 Javascript
Angular 组件之间的交互的示例代码
2018/03/24 Javascript
Vue-cli@3.0 插件系统简析
2018/09/05 Javascript
vue、react等单页面项目部署到服务器的方法及vue和react的区别
2018/09/29 Javascript
Vue实现兄弟组件间的联动效果
2020/01/21 Javascript
Python栈类实例分析
2015/06/15 Python
Python 含参构造函数实例详解
2017/05/25 Python
Python编程实现的简单神经网络算法示例
2018/01/26 Python
对Python 2.7 pandas 中的read_excel详解
2018/05/04 Python
python分块读取大数据,避免内存不足的方法
2018/12/10 Python
Python 实现两个服务器之间文件的上传方法
2019/02/13 Python
Python 实现还原已撤回的微信消息
2019/06/18 Python
解决Django中调用keras的模型出现的问题
2019/08/07 Python
解决pandas展示数据输出时列名不能对齐的问题
2019/11/18 Python
使用python快速实现不同机器间文件夹共享方式
2019/12/22 Python
Python urlencode和unquote函数使用实例解析
2020/03/31 Python
python随机模块random的22种函数(小结)
2020/05/15 Python
sklearn线性逻辑回归和非线性逻辑回归的实现
2020/06/09 Python
HTML5中的websocket实现直播功能
2018/05/21 HTML / CSS
澳大利亚领先的在线美容商店:Facial Co
2017/10/22 全球购物
智能电子秤、手表和健康监测仪:Withings(之前为诺基亚健康)
2018/10/30 全球购物
奥地利票务门户网站:oeticket.com
2019/12/31 全球购物
绩效考核实施方案
2014/03/18 职场文书
2015届大学生就业推荐表自我评价
2014/09/27 职场文书
公司财务制度:成本管理控制制度模板
2019/11/19 职场文书