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

yipeiwu_com6年前Python基础

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

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

具体代码如下:

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

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

相关文章

Python函数中定义参数的四种方式

Python中函数参数的定义主要有四种方式: 1. F(arg1,arg2,…) 这是最常见的定义方式,一个函数可以定义任意个参数,每个参数间用逗号分割,用这种方式定义的函数在调用的的时...

python实现七段数码管和倒计时效果

python实现七段数码管和倒计时效果

8是典型的七段数码管的例子,因为刚好七段都有经过,这里我写的代码是从1开始右转。 这是看Mooc视频写的一个关于用七段数码管显示当前时间 # -*-coding:utf-8 -*-...

Python脚本实现网卡流量监控

#/usr/bin/env/python #coding=utf-8 import sys,re,time,os maxdata = 50000 #单位KB memfilename...

Python实现自动为照片添加日期并分类的方法

Python实现自动为照片添加日期并分类的方法

本文实例讲述了Python实现自动为照片添加日期并分类的方法。分享给大家供大家参考,具体如下: 小时候没怎么照相,所以跟别人说小时候特别帅他们都不信。小外甥女出生了,我给买了个照相机,让...

python numpy和list查询其中某个数的个数及定位方法

1. list 查询个数: 调用list.count(obj)函数,返回obj在list中的个数。 输入: list_a = [2 for x in range(5)] print(...