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调用SQLPlus来操作和解析Oracle数据库的方法
Apr 09 Python
Python中的descriptor描述器简明使用指南
Jun 02 Python
基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解
Oct 13 Python
完美解决Python matplotlib绘图时汉字显示不正常的问题
Jan 29 Python
Python模块、包(Package)概念与用法分析
May 31 Python
对python中的float除法和整除法的实例详解
Jul 20 Python
关于windows下Tensorflow和pytorch安装教程
Feb 04 Python
Django ModelForm操作及验证方式
Mar 30 Python
Keras 数据增强ImageDataGenerator多输入多输出实例
Jul 03 Python
序列化Python对象的方法
Aug 01 Python
k-means & DBSCAN 总结
Apr 27 Python
Pandas 数据编码的十种方法
Apr 20 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桌面中心(三) 修改数据库
2007/03/11 PHP
php array_intersect比array_diff快(附详细的使用说明)
2011/07/03 PHP
使用php-timeit估计php函数的执行时间
2015/09/06 PHP
PHP大文件切割上传功能实例分析
2019/07/01 PHP
jQuery插件PageSlide实现左右侧栏导航菜单
2015/04/12 Javascript
jquery判断单选按钮radio是否选中的方法
2015/05/05 Javascript
JavaScript动态创建form表单并提交的实现方法
2015/12/10 Javascript
JS获取url参数、主域名的方法实例分析
2016/08/03 Javascript
jQuery通过改变input的type属性实现密码显示隐藏切换功能
2017/02/08 Javascript
Vue中保存用户登录状态实例代码
2017/06/07 Javascript
JavaScript实现图片拖曳效果
2017/09/08 Javascript
快速将Vue项目升级到webpack3的方法步骤
2017/09/14 Javascript
实例分析Array.from(arr)与[...arr]到底有何不同
2019/04/09 Javascript
js实现点击生成随机div
2020/01/16 Javascript
vue中是怎样监听数组变化的
2020/10/24 Javascript
解决Element中el-date-picker组件不回填的情况
2020/11/07 Javascript
Python获取系统默认字符编码的方法
2015/06/04 Python
详解Python3中的Sequence type的使用
2015/08/01 Python
以视频爬取实例讲解Python爬虫神器Beautiful Soup用法
2016/01/20 Python
Python之列表的插入&替换修改方法
2018/06/28 Python
浅谈Python线程的同步互斥与死锁
2020/03/22 Python
Python如何实现Paramiko的二次封装
2021/01/30 Python
基于注解实现 SpringBoot 接口防刷的方法
2021/03/02 Python
超30万乐谱下载:Musicnotes.com
2016/09/24 全球购物
土木工程应届生求职信
2013/10/31 职场文书
自主招生自荐信范文
2013/12/04 职场文书
中学自我评价
2014/01/31 职场文书
厨师长岗位职责
2014/03/02 职场文书
数学系毕业生求职信
2014/05/29 职场文书
企业总经理任命书
2014/06/05 职场文书
诚实守信道德模范事迹材料
2014/08/15 职场文书
优秀党员申报材料
2014/12/18 职场文书
报案材料怎么写
2015/05/25 职场文书
大学体育课感想
2015/08/10 职场文书
安全生产标语口号
2015/12/26 职场文书
Win11 Build 25179预览版发布(附更新内容+ISO官方镜像下载)
2022/08/14 数码科技