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 相关文章推荐
详解python3百度指数抓取实例
Dec 12 Python
django实现登录时候输入密码错误5次锁定用户十分钟
Nov 05 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
Dec 07 Python
Django如何开发简单的查询接口详解
May 17 Python
通过python实现弹窗广告拦截过程详解
Jul 10 Python
OpenCV 边缘检测
Jul 10 Python
python实现动态数组的示例代码
Jul 15 Python
Django 简单实现分页与搜索功能的示例代码
Nov 07 Python
pytorch程序异常后删除占用的显存操作
Jan 13 Python
python实时监控logstash日志代码
Apr 27 Python
PyCharm常用配置和常用插件(小结)
Feb 06 Python
OpenCV-Python模板匹配人眼的实例
Jun 08 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
CodeIgniter php mvc框架 中国网站
2008/05/26 PHP
浅析使用Turck-mmcache编译来加速、优化PHP代码
2013/06/20 PHP
PHP中让curl支持sock5的代码实例
2015/01/21 PHP
PHP版本升级到7.x后wordpress的一些修改及wordpress技巧
2015/12/25 PHP
PHP基于进程控制函数实现多线程
2020/12/09 PHP
让getElementsByName适应IE和firefox的方法
2007/09/24 Javascript
jQuery + Flex 通过拖拽方式动态改变图片的代码
2011/08/03 Javascript
JS中setInterval、setTimeout不能传递带参数的函数的解决方案
2013/04/28 Javascript
JS实现一个列表中包含上移下移删除等功能
2014/09/24 Javascript
详解JavaScript的while循环的使用
2015/06/03 Javascript
jquery实现的动态回到顶部特效代码
2015/10/28 Javascript
window.onerror()的用法与实例分析
2016/01/27 Javascript
js点击返回跳转到指定页面实现过程
2020/08/20 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
全面了解js中的script标签
2016/07/04 Javascript
JS实现拖拽的方法分析
2016/12/20 Javascript
AngularJS实现的获取焦点及失去焦点时的表单验证功能示例
2017/10/25 Javascript
VUEJS 2.0 子组件访问/调用父组件的实例
2018/02/10 Javascript
vue2.0使用swiper组件实现轮播的示例代码
2018/03/03 Javascript
vue axios登录请求拦截器
2018/04/02 Javascript
跟老齐学Python之用Python计算
2014/09/12 Python
使用Pyrex来扩展和加速Python程序的教程
2015/04/13 Python
Python 字符串大小写转换的简单实例
2017/01/21 Python
Python删除n行后的其他行方法
2019/01/28 Python
Python查找数组中数值和下标相等的元素示例【二分查找】
2019/02/13 Python
python 伯努利分布详解
2020/02/25 Python
Django Session和Cookie分别实现记住用户登录状态操作
2020/07/02 Python
Pyqt助手安装PyQt5帮助文档过程图解
2020/11/20 Python
英国大码女性时装零售商:Evans
2018/08/29 全球购物
澳大利亚婴儿礼品公司:The Baby Gift Company
2018/11/04 全球购物
英国高街奥特莱斯:Highstreet Outlet
2019/11/21 全球购物
电子商务专业实习生自我鉴定
2013/09/24 职场文书
自我鉴定书范文
2013/10/02 职场文书
报关专员求职信范文
2014/02/22 职场文书
地理科学专业自荐信
2014/09/01 职场文书
关于python爬虫应用urllib库作用分析
2021/09/04 Python