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 获取et和excel的版本号
Apr 09 Python
Centos5.x下升级python到python2.7版本教程
Feb 14 Python
python中安装模块包版本冲突问题的解决
May 02 Python
Python实现购物车功能的方法分析
Nov 10 Python
Python之Scrapy爬虫框架安装及简单使用详解
Dec 22 Python
Python产生Gnuplot绘图数据的方法
Nov 09 Python
Python使用while循环花式打印乘法表
Jan 28 Python
python 猴子补丁(monkey patch)
Jun 26 Python
简单了解Python生成器是什么
Jul 02 Python
浅谈Python中的模块
Jun 10 Python
python 实现弹球游戏的示例代码
Nov 17 Python
python实现控制台输出颜色
Mar 02 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
Terran剧情介绍
2020/03/14 星际争霸
Apache中php.ini的设置方法
2013/02/28 PHP
PHP 使用MySQL管理Session的回调函数详解
2013/06/21 PHP
CodeIgniter框架中_remap()使用方法2例
2014/03/10 PHP
PHP查找数值数组中不重复最大和最小的10个数的方法
2015/04/20 PHP
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
2015/05/12 PHP
在openSUSE42.1下编译安装PHP7 的方法
2015/12/24 PHP
js获取单选框或复选框值及操作
2012/12/18 Javascript
img onload事件绑定各浏览器均可执行
2012/12/19 Javascript
jquery实现浮动在网页右下角的彩票开奖公告窗口代码
2015/09/04 Javascript
js表单验证实例讲解
2016/03/31 Javascript
详解nodejs与javascript中的aes加密
2016/05/22 NodeJs
js实现图片缓慢放大缩小效果
2016/08/02 Javascript
AngularJs Modules详解及示例代码
2016/09/01 Javascript
js实现简单的二级联动效果
2017/03/09 Javascript
vue+swiper实现组件化开发的实例代码
2017/10/26 Javascript
JS实现非首屏图片延迟加载的示例
2018/01/06 Javascript
让Vue也可以使用Redux的方法
2018/05/23 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
2019/04/29 Javascript
JS脚本实现定时到网站上签到/签退功能
2020/04/22 Javascript
详解Vue中的自定义指令
2020/12/07 Vue.js
详解template标签用法(含vue中的用法总结)
2021/01/12 Vue.js
linux系统使用python获取cpu信息脚本分享
2014/01/15 Python
Python 专题三 字符串的基础知识
2017/03/19 Python
Python使用PDFMiner解析PDF代码实例
2017/03/27 Python
Python对象类型及其运算方法(详解)
2017/07/05 Python
Python使用三种方法实现PCA算法
2017/12/12 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
2018/01/08 Python
python实现批量修改图片格式和尺寸
2018/06/07 Python
Python彻底删除文件夹及其子文件方式
2019/12/23 Python
Python接口自动化判断元素原理解析
2020/02/24 Python
python 实现一个图形界面的汇率计算器
2020/11/09 Python
大三在校生电子商务求职信
2013/10/29 职场文书
标准的毕业生自荐信
2014/04/20 职场文书
甜品店创业计划书
2014/08/14 职场文书
Python实现批量自动整理文件
2022/03/16 Python