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 相关文章推荐
tornado框架blog模块分析与使用
Nov 21 Python
python中sets模块的用法实例
Sep 30 Python
Python每天必学之bytes字节
Jan 28 Python
python实现本地图片转存并重命名的示例代码
Oct 27 Python
利用Python实现微信找房机器人实例教程
Mar 10 Python
python装饰器简介---这一篇也许就够了(推荐)
Apr 01 Python
Python实现朴素贝叶斯的学习与分类过程解析
Aug 24 Python
django实现将后台model对象转换成json对象并传递给前端jquery
Mar 16 Python
关于Python turtle库使用时坐标的确定方法
Mar 19 Python
Python实现JS解密并爬取某音漫客网站
Oct 23 Python
如何使用Django Admin管理后台导入CSV
Nov 06 Python
如何利用python 读取配置文件
Jan 06 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
收音机鉴频器对声音的影响和频偏分析
2021/03/02 无线电
怎样才能成为PHP高手?学会“懒惰”的编程
2006/12/05 PHP
PHP获得用户使用的代理服务器ip即真实ip
2006/12/31 PHP
php转换颜色为其反色的方法
2015/04/27 PHP
PHP+jQuery实现滚屏无刷新动态加载数据功能详解
2017/05/04 PHP
Smarty模板变量与调节器实例详解
2019/07/20 PHP
JS提交并解析后台返回的XML的代码
2008/11/03 Javascript
javascript+iframe 实现无刷新载入整页的代码
2010/03/17 Javascript
JavaScript常用对象的方法和属性小结
2012/01/24 Javascript
Javascript 键盘事件的组合使用实现代码
2012/05/04 Javascript
node.js中的querystring.unescape方法使用说明
2014/12/10 Javascript
js实现简单的左右两边固定广告效果实例
2015/04/10 Javascript
JS实现的5级联动Select下拉选择框实例
2015/08/17 Javascript
纯javascript代码实现计算器功能(三种方法)
2015/09/07 Javascript
详解js中构造流程图的核心技术JsPlumb
2015/12/08 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
javascript 判断一个对象为数组的方法
2017/05/03 Javascript
微信小程序-滚动消息通知的实例代码
2017/08/03 Javascript
JavaScript JMap类定义与使用方法示例
2019/01/22 Javascript
详解VScode编辑器vue环境搭建所遇问题解决方案
2019/04/26 Javascript
javascript设计模式 ? 组合模式原理与应用实例分析
2020/04/14 Javascript
在Python中使用模块的教程
2015/04/27 Python
python 地图经纬度转换、纠偏的实例代码
2018/08/06 Python
Python函数返回不定数量的值方法
2019/01/22 Python
Pandas统计重复的列里面的值方法
2019/01/30 Python
Python qqbot 实现qq机器人的示例代码
2019/07/11 Python
python中的函数递归和迭代原理解析
2019/11/14 Python
python opencv如何实现图片绘制
2020/01/19 Python
Gweniss格温妮丝女包官网:英国纯手工制造潮流包包品牌
2018/02/07 全球购物
有机婴儿毛毯和衣服:Monica + Andy
2020/03/01 全球购物
计算机专业自荐信范文
2014/05/28 职场文书
小学生关于梦想的演讲稿
2014/08/22 职场文书
实习证明格式范文
2015/06/16 职场文书
Python中的程序流程控制语句
2022/02/24 Python
winserver2019安装软件一直卡在应用程序正在为首次使用做准备
2022/06/10 Servers
java获取一个文本文件的编码(格式)信息
2022/09/23 Java/Android