python生成圆形图片的方法

yipeiwu_com6年前Python基础

本文实例为大家分享了python生成圆形图片的具体代码,供大家参考,具体内容如下

# -*- coding: utf-8 -*- 
""" 
__author__= 'Du' 
__creation_time__= '2018/1/5 9:08' 
""" 
 
import os, math 
from PIL import Image 
 
 
def circle(): 
 ima = Image.open("ball1.jpg").convert("RGBA") 
 # ima = ima.resize((600, 600), Image.ANTIALIAS) 
 size = ima.size 
 print(size) 
 
 # 因为是要圆形,所以需要正方形的图片 
 r2 = min(size[0], size[1]) 
 if size[0] != size[1]: 
 ima = ima.resize((r2, r2), Image.ANTIALIAS) 
 
 # 最后生成圆的半径 
 r3 = 60 
 imb = Image.new('RGBA', (r3*2, r3*2),(255,255,255,0)) 
 pima = ima.load() # 像素的访问对象 
 pimb = imb.load() 
 r = float(r2/2) #圆心横坐标 
 
 for i in range(r2): 
 for j in range(r2): 
  lx = abs(i-r) #到圆心距离的横坐标 
  ly = abs(j-r)#到圆心距离的纵坐标 
  l = (pow(lx,2) + pow(ly,2))** 0.5 # 三角函数 半径 
 
  if l < r3: 
  pimb[i-(r-r3),j-(r-r3)] = pima[i,j] 
 imb.save("test_circle.png") 
 
circle() 

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3.4 tkinter,PIL图片转换

Python3.4 tkinter,PIL图片转换

先给大家分享一下全部代码 import os from PIL import Image import tkinter import tkinter.filedialog impor...

Python实现在tkinter中使用matplotlib绘制图形的方法示例

Python实现在tkinter中使用matplotlib绘制图形的方法示例

本文实例讲述了Python实现在tkinter中使用matplotlib绘制图形的方法。分享给大家供大家参考,具体如下: 一. 代码: # coding=utf-8 import s...

python使用webbrowser浏览指定url的方法

本文实例讲述了python使用webbrowser浏览指定url的方法。分享给大家供大家参考。具体如下: 这段代码提示用户输入关键词,通过webbrowser打开浏览器浏览google...

Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

在python的Beautiful Soup 4 扩展库的使用过程中出现了 TypeError: list indices must be integers or slices, no...

Python OpenCV实现视频分帧

本文实例为大家分享了Python OpenCV实现视频分帧的具体代码,供大家参考,具体内容如下 # coding=utf-8 import os import cv2 video...