本文實例為大家分享了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() 效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答