建站学习网,专业提供各类建站教程,网页素材,SEO资讯等建站资源!

网站模板_网站源码_网站素材_建站教程_建站学习网

当前位置:建站学习网 > 网页设计 > 脚本HTML教程 >

python以环状形式组合排列图片并输出的方法

更新时间:2017-04-10整理编辑:建站学习网阅读:0

 这篇文章主要介绍了python以环状形式组合排列图片并输出的方法,涉及Python使用pil库操作图片的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

 
 

本文实例讲述了python以环状形式组合排列图片并输出的方法。分享给大家供大家参考。具体分析如下:

这段代码可以自定义一个空白画板,然后将指定的图片以圆环状的方式排列起来,用到了pil库,可以通过:
pip install pil 的方式安装。

具体代码如下:

代码如下:
# -*- coding: utf-8 -*-
__author__ = 'www.dedexuexi.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:/www.dedexuexi.com/images/1.png", "d:/www.dedexuexi.com/images/2.png", "d:/www.dedexuexi.com/images/3.png"] * 5
images = [Image.open(filename) for filename in imageFilenames]
arrangeImagesInCircle(img, images)
img.save("output.png")

 

希望本文所述对大家的Python程序设计有所帮助。


本文网址:https://www.dedexuexi.com/wysj/html/2645.html

本站部分文章搜集与网络,如有侵权请联系本站,转载请说明出处。

收藏此文 赞一下!() 打赏本站

如本文对您有帮助,就请建站学习网抽根烟吧!

支付宝打赏
微信打赏
用C#从IE浏览器中获取HTML文档
« 上一篇2017年04月10日
如何禁用HTML页面的上下文菜单
2017年04月10日下一篇 »
  • 实现导航栏固定在顶部(吸顶效果),滚动页面万能方式
    0阅读
    1)滚动条不动时,是静态相对定位状态relative2)当页面滚动超出设置距离时,js改变样式属性切换定位状态变成fixed,同时再给top设置一个值css部分// 滚动条不动时.box{position: relative;height: 80px;width: 100%;z-index: 999...
  • HTML锚点用class样式教程
    0阅读
    JS部分:$(function() { $(window).scroll(function() { //为页面添加页面滚动监听事件 var wst = $(window).scrollTop() //滚动条距离顶端值 for (i = 1; i < 6...
  • python标准算法实现数组全排列的方法
    0阅读
    这篇文章主要介绍了python标准算法实现数组全排列的方法,实例分析了全排列的原理与Python实现技巧,需要的朋友可以参考下 本文实例讲述了python标准算法实现数组全排列的方法,代码来自国外网
  • python将ip地址转换成整数的方法
    0阅读
    这篇文章主要介绍了python将ip地址转换成整数的方法,涉及Python针对IP地址的转换技巧,需要的朋友可以参考下 本文实例讲述了python将ip地址转换成整数的方法。分享给大家供大家参考。具体分析如
  • 一个生成html的新方法
    0阅读
    使用范例: set fso=CreateObject(Scripting.FileSystemObject) set f=fso.CreateTextFile( server.mappath( ), true ) f.WriteLine( asp2html(youpage.asp) ) f.clos...
 
QQ在线咨询