[Python] python以环状形式组合排列图片并输出 →→→→→进入此内容的聊天室

来自 , 2020-02-24, 写在 Python, 查看 145 次.
URL http://www.code666.cn/view/fdb2c3ba
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'www.sharejs.com'
  3. import math
  4. from PIL import Image
  5.  
  6. def arrangeImagesInCircle(masterImage, imagesToArrange):
  7.     imgWidth, imgHeight = masterImage.size
  8.  
  9.     #we want the circle to be as large as possible.
  10.     #but the circle shouldn't extend all the way to the edge of the image.
  11.     #If we do that, then when we paste images onto the circle, those images will partially fall over the edge.
  12.     #so we reduce the diameter of the circle by the width/height of the widest/tallest image.
  13.     diameter = min(
  14.         imgWidth  - max(img.size[0] for img in imagesToArrange),
  15.         imgHeight - max(img.size[1] for img in imagesToArrange)
  16.     )
  17.     radius = diameter / 2
  18.  
  19.     circleCenterX = imgWidth  / 2
  20.     circleCenterY = imgHeight / 2
  21.     theta = 2*math.pi / len(imagesToArrange)
  22.     for i in range(len(imagesToArrange)):
  23.         curImg = imagesToArrange[i]
  24.         angle = i * theta
  25.         dx = int(radius * math.cos(angle))
  26.         dy = int(radius * math.sin(angle))
  27.  
  28.         #dx and dy give the coordinates of where the center of our images would go.
  29.         #so we must subtract half the height/width of the image to find where their top-left corners should be.
  30.         pos = (
  31.             circleCenterX + dx - curImg.size[0]/2,
  32.             circleCenterY + dy - curImg.size[1]/2
  33.         )
  34.         masterImage.paste(curImg, pos)
  35.  
  36. img = Image.new("RGB", (500,500), (255,255,255))
  37.  
  38. #下面的三个图片是3个 50x50 的pngs 图片,使用了绝对路径,需要自己进行替换成你的图片路径
  39. imageFilenames = ["d:/www.sharejs.com/images/1.png", "d:/www.sharejs.com/images/2.png", "d:/www.sharejs.com/images/3.png"] * 5
  40. images = [Image.open(filename) for filename in imageFilenames]
  41.  
  42. arrangeImagesInCircle(img, images)
  43.  
  44. img.save("output.png")
  45. #//python/8673

回复 "python以环状形式组合排列图片并输出"

这儿你可以回复上面这条便签

captcha