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的正则表达式re模块的常用方法
Mar 09 Python
Python制作CSDN免积分下载器
Mar 10 Python
Python使用minidom读写xml的方法
Jun 03 Python
浅析Python装饰器以及装饰器模式
May 28 Python
详解Python3的TFTP文件传输
Jun 26 Python
python爬虫之验证码篇3-滑动验证码识别技术
Apr 11 Python
Python3 全自动更新已安装的模块实现
Jan 06 Python
Python实现i人事自动打卡的示例代码
Jan 09 Python
python 一维二维插值实例
Apr 22 Python
python 8种必备的gui库
Aug 27 Python
Pytho爬虫中Requests设置请求头Headers的方法
Sep 22 Python
django 认证类配置实现
Nov 11 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来实现网络服务
2009/09/15 PHP
PHP中SimpleXML函数用法分析
2014/11/26 PHP
ImageFlow可鼠标控制图片滚动
2008/01/30 Javascript
firefox和IE系列的相关区别整理 以备后用
2009/12/28 Javascript
用apply让javascript函数仅执行一次的代码
2010/06/27 Javascript
jQuery当鼠标悬停时放大图片的效果实例
2013/07/03 Javascript
查找iframe里元素的方法可传参
2013/09/11 Javascript
IE6下javasc#ipt:void(0) 无效的解决方法
2013/12/23 Javascript
js获得当前系统日期时间的方法
2015/05/06 Javascript
.NET微信公众号开发之创建自定义菜单
2015/07/16 Javascript
JavaScript中split与join函数的进阶使用技巧
2016/05/03 Javascript
浅析Javascript ES6新增值比较函数Object.is
2016/08/24 Javascript
原生js实现鼠标跟随效果
2017/02/28 Javascript
Angular2关于@angular/cli默认端口号配置的问题
2017/07/15 Javascript
vuejs数据超出单行显示更多,点击展开剩余数据实例
2019/05/05 Javascript
微信小程序实现一个简单swiper代码实例
2019/12/30 Javascript
JavaScript canvas绘制渐变颜色的矩形
2020/02/18 Javascript
vue+iview框架实现左侧动态菜单功能的示例代码
2020/07/23 Javascript
vue 递归组件的简单使用示例
2021/01/14 Vue.js
python模拟登陆Tom邮箱示例分享
2014/01/13 Python
Python的Flask框架的简介和安装方法
2015/11/13 Python
CentOS安装pillow报错的解决方法
2016/01/27 Python
浅谈PYTHON 关于文件的操作
2019/03/19 Python
Python基于BeautifulSoup和requests实现的爬虫功能示例
2019/08/02 Python
Python中Unittest框架的具体使用
2019/08/27 Python
详解Python中打乱列表顺序random.shuffle()的使用方法
2019/11/11 Python
详解Python在使用JSON时需要注意的编码问题
2019/12/06 Python
Notino匈牙利:购买香水和化妆品
2019/04/12 全球购物
中国跨镜手机配件批发在线商店:TVC-Mall
2019/08/20 全球购物
公司新员工的演讲稿注意事项
2014/01/01 职场文书
推荐信模板
2014/05/09 职场文书
爱护公物标语
2014/06/24 职场文书
幼儿园清明节活动总结
2014/07/04 职场文书
人事专员岗位说明书
2014/07/29 职场文书
公司行政助理岗位职责
2015/04/11 职场文书
公司联欢会主持词
2015/07/04 职场文书