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实现根据IP地址和子网掩码算出网段的方法
Jul 30 Python
在Python中通过threading模块定义和调用线程的方法
Jul 12 Python
python tensorflow学习之识别单张图片的实现的示例
Feb 09 Python
python jieba分词并统计词频后输出结果到Excel和txt文档方法
Feb 11 Python
Python实现自定义函数的5种常见形式分析
Jun 16 Python
python 中文件输入输出及os模块对文件系统的操作方法
Aug 27 Python
对python判断是否回文数的实例详解
Feb 08 Python
python3 自动识别usb连接状态,即对usb重连的判断方法
Jul 03 Python
python3 webp转gif格式的实现示例
Dec 10 Python
Python3 xml.etree.ElementTree支持的XPath语法详解
Mar 06 Python
使用Python爬取弹出窗口信息的实例
Mar 14 Python
利用python制作拼图小游戏的全过程
Dec 04 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取进制余数函数代码
2012/01/19 PHP
PHP语言中global和$GLOBALS[]的分析 之二
2012/02/02 PHP
php目录操作实例代码
2014/02/21 PHP
PHP+JS三级菜单联动菜单实现方法
2016/02/24 PHP
TBCompressor js代码压缩
2011/01/05 Javascript
jQuery EasyUI API 中文文档 - TreeGrid 树表格使用介绍
2011/11/21 Javascript
ASP.NET jQuery 实例2 (表单中使用回车在TextBox之间向下移动)
2012/01/13 Javascript
js控制淡入淡出示例代码
2013/11/12 Javascript
几种设置表单元素中文本输入框不可编辑的方法总结
2013/11/25 Javascript
基于Jquery实现表单验证
2020/07/20 Javascript
jQuery+CSS实现一个侧滑导航菜单代码
2016/05/09 Javascript
微信小程序 支付功能开发错误总结
2017/02/21 Javascript
JavaScript中数组Array方法详解
2017/02/27 Javascript
JS实现的简单拖拽功能示例
2017/03/13 Javascript
Bootstrap table使用方法总结
2017/05/10 Javascript
Cropper.js 实现裁剪图片并上传(PC端)
2017/08/20 Javascript
用p5.js制作烟花特效的示例代码
2018/03/21 Javascript
ES6 Promise对象概念及用法实例详解
2019/10/15 Javascript
Vue中fragment.js使用方法小结
2020/02/17 Javascript
JavaScript实现公告栏上下滚动效果
2020/03/13 Javascript
Javascript基于OOP实实现探测器功能代码实例
2020/08/26 Javascript
vue 图片裁剪上传组件的实现
2020/11/12 Javascript
[56:17]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
Tensorflow卷积神经网络实例
2018/05/24 Python
Python实现识别图片内容的方法分析
2018/07/11 Python
Python使用pyshp库读取shapefile信息的方法
2018/12/29 Python
python制作抽奖程序代码详解
2021/01/15 Python
纯CSS3代码实现文字描边
2016/04/25 HTML / CSS
利用纯html5绘制出来的一款非常漂亮的时钟
2015/01/04 HTML / CSS
有影响力的品牌之家:Our Social Collective
2019/06/08 全球购物
FC-Moto丹麦:欧洲最大的摩托车服装和头盔商店之一
2019/08/20 全球购物
俄罗斯玩具、儿童用品、儿童服装和鞋子网上商店:MyToys.ru
2019/10/14 全球购物
夫妻忠诚协议书范本
2014/11/17 职场文书
员工试用期转正自我评价
2015/03/10 职场文书
长江七号观后感
2015/06/11 职场文书
详解PHP设计模式之依赖注入模式
2021/05/25 PHP