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批量下载图片的三种方法
Apr 22 Python
Python的函数嵌套的使用方法
Jan 24 Python
python实现rest请求api示例
Apr 22 Python
使用Python的Flask框架表单插件Flask-WTF实现Web登录验证
Jul 12 Python
Python如何实现MySQL实例初始化详解
Nov 06 Python
python分块读取大数据,避免内存不足的方法
Dec 10 Python
PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上
Apr 01 Python
Python猜数字算法题详解
Mar 01 Python
Django调用百度AI接口实现人脸注册登录代码实例
Apr 23 Python
在pycharm中使用matplotlib.pyplot 绘图时报错的解决
Jun 01 Python
PyQt5的相对布局管理的实现
Aug 07 Python
python geopandas读取、创建shapefile文件的方法
Jun 29 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 tp验证表单与自动填充函数代码
2012/02/22 PHP
与文件上传有关的php配置参数总结
2013/06/14 PHP
dedecms集成财付通支付接口
2014/12/28 PHP
Symfony2在Nginx下的配置方法图文教程
2016/02/04 PHP
php操作access数据库的方法详解
2017/02/22 PHP
php关联数组与索引数组及其显示方法
2018/03/12 PHP
PHP实现的抓取小说网站内容功能示例
2019/06/27 PHP
jQuery开发者都需要知道的5个小技巧
2010/01/08 Javascript
两个listbox实现选项的添加删除和搜索
2013/03/01 Javascript
JQuery Highcharts 动态生成图表的方法
2013/11/15 Javascript
jquery如何通过name名称获取当前name的value值
2013/12/20 Javascript
JS遍历Json字符串中键值对先转成JSON对象再遍历
2014/08/15 Javascript
JavaScript和CSS交互的方法汇总
2014/12/02 Javascript
jQuery手动点击实现图片轮播特效
2020/04/20 Javascript
JavaScript正则表达式的分组匹配详解
2016/02/13 Javascript
jQuery Dialog 打开时自动聚焦的解决方法(两种方法)
2016/11/24 Javascript
微信小程序 UI布局常用技巧整理总结
2016/12/05 Javascript
详解从新建vue项目到引入组件Element的方法
2017/08/29 Javascript
vue2.0.js的多级联动选择器实现方法
2018/02/09 Javascript
微信小程序实现顶部下拉菜单栏
2018/11/04 Javascript
vue中的面包屑导航组件实例代码
2019/07/01 Javascript
Vue项目实现简单的权限控制管理功能
2019/07/17 Javascript
详解javascript void(0)
2020/07/13 Javascript
JS实现无限轮播无倒退效果
2020/09/21 Javascript
Python使用OpenCV进行标定
2018/05/08 Python
python学生管理系统
2019/01/30 Python
Python unittest单元测试openpyxl实现过程解析
2020/05/27 Python
matplotlib部件之矩形选区(RectangleSelector)的实现
2021/02/01 Python
.net C#面试题
2012/08/28 面试题
售后主管岗位职责
2013/12/08 职场文书
法律进企业活动方案
2014/03/04 职场文书
学习十八届四中全会精神思想汇报
2014/10/23 职场文书
2015年街道除四害工作总结
2015/05/15 职场文书
父母教会我观后感
2015/06/17 职场文书
2016应届毕业生实习心得体会
2015/10/09 职场文书
golang通过递归遍历生成树状结构的操作
2021/04/28 Golang