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读取环境变量的方法和自定义类分享
Nov 22 Python
python使用itchat库实现微信机器人(好友聊天、群聊天)
Jan 04 Python
Python代码缩进和测试模块示例详解
May 07 Python
Python3.6基于正则实现的计算器示例【无优化简单注释版】
Jun 14 Python
Python类的继承、多态及获取对象信息操作详解
Feb 28 Python
python3.6编写的单元测试示例
Aug 17 Python
django 做 migrate 时 表已存在的处理方法
Aug 31 Python
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
Oct 24 Python
python实现大学人员管理系统
Oct 25 Python
自定义Django默认的sitemap站点地图样式
Mar 04 Python
基于python实现操作git过程代码解析
Jul 27 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
Nov 18 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
详细介绍:Apache+PHP+MySQL配置攻略
2006/09/05 PHP
PHP 和 MySQL 开发的 8 个技巧
2006/10/09 PHP
让PHP开发者事半功倍的十大技巧小结
2010/04/20 PHP
php中比较简单的导入phpmyadmin生成的sql文件的方法
2011/06/28 PHP
php在文件指定行中写入代码的方法
2012/05/23 PHP
PHP弹出对话框技巧详细解读
2015/09/26 PHP
Smarty保留变量用法分析
2016/05/23 PHP
HR vs CL BO3 第一场 2.13
2021/03/10 DOTA
Prototype Class对象学习
2009/07/19 Javascript
javascript 面向对象继承
2009/11/26 Javascript
jQuery 全选/反选以及单击行改变背景色实例
2013/07/02 Javascript
非常酷炫的Bootstrap图片轮播动画
2016/05/27 Javascript
封装的dialog插件 基于bootstrap模态对话框的简单扩展
2016/08/10 Javascript
JS实现密码框的显示密码和隐藏密码功能示例
2016/12/26 Javascript
AngularJS ng-repeat指令及Ajax的应用实例分析
2017/07/06 Javascript
ES6入门教程之let、const的使用方法
2019/04/13 Javascript
Centos7 安装Node.js10以上版本的方法步骤
2019/10/15 Javascript
Vue SPA 初次进入加载动画实现代码
2019/11/14 Javascript
微信小程序实现Swiper轮播图效果
2019/11/22 Javascript
vue项目中使用rem,在入口文件添加内容操作
2020/11/11 Javascript
Node.js中的异步生成器与异步迭代详解
2021/01/31 Javascript
17个Python小技巧分享
2015/01/23 Python
对python中for、if、while的区别与比较方法
2018/06/25 Python
Python计算时间间隔(精确到微妙)的代码实例
2019/02/26 Python
Python中将两个或多个list合成一个list的方法小结
2019/05/12 Python
python os.fork() 循环输出方法
2019/08/08 Python
python nmap实现端口扫描器教程
2020/05/28 Python
python实现XML解析的方法解析
2019/11/16 Python
Python3+Selenium+Chrome实现自动填写WPS表单
2020/02/12 Python
python实现图片横向和纵向拼接
2020/03/05 Python
HTML5 video 事件应用示例
2014/09/11 HTML / CSS
澳大利亚在线购买葡萄酒:The Wine Collective
2020/02/20 全球购物
办公室秘书岗位职责范本
2014/02/11 职场文书
农村党员一句话承诺
2014/05/30 职场文书
傲慢与偏见电影观后感
2015/06/10 职场文书
Java使用httpRequest+Jsoup爬取红蓝球号码
2021/07/02 Java/Android