首页 > 编程语言 > 详细

python 直角图标生成圆角图标

时间:2019-10-26 12:36:39      阅读:310      评论:0      收藏:0      [点我收藏+]

参考链接:https://stackoverflow.com/questions/11287402/how-to-round-corner-a-logo-without-white-backgroundtransparent-on-it-using-pi

用了三种方法,第三种方法效果最好,效果如下图:

技术分享图片

 

 

需要用到两个库:

pip install Pillow
pip install aggdraw

源码如下:

from PIL import Image,ImageDraw,ImageChops
import aggdraw
import os

def round_corner_jpg(image, radius):
    """generate round corner for image"""
    mask = Image.new(L, image.size) # filled with black by default
    draw = aggdraw.Draw(mask)
    brush = aggdraw.Brush(white)
    width, height = mask.size
    #upper-left corner
    draw.pieslice((0,0,radius*2, radius*2), 90, 180, None, brush)
    #upper-right corner
    draw.pieslice((width - radius*2, 0, width, radius*2), 0, 90, None, brush)
    #bottom-left corner
    draw.pieslice((0, height - radius * 2, radius*2, height),180, 270, None, brush)
    #bottom-right corner
    draw.pieslice((width - radius * 2, height - radius * 2, width, height), 270, 360, None, brush)
    #center rectangle
    draw.rectangle((radius, radius, width - radius, height - radius), brush)
    #four edge rectangle
    draw.rectangle((radius, 0, width - radius, radius), brush)
    draw.rectangle((0, radius, radius, height-radius), brush)
    draw.rectangle((radius, height-radius, width-radius, height), brush)
    draw.rectangle((width-radius, radius, width, height-radius), brush)
    draw.flush()
    image = image.convert(RGBA)
    image.putalpha(mask)
    return image

def get_filePath_fileName_fileExt(fileUrl):
    """
    获取文件路径, 文件名, 后缀名
    :param fileUrl:
    :return:
    """
    filepath, tmpfilename = os.path.split(fileUrl)
    shotname, extension = os.path.splitext(tmpfilename)
    return filepath, shotname, extension

if __name__ == __main__:
    picLocation = Sina1.png
    im = Image.open(picLocation)
    im = round_corner_jpg(im, 20)
    im.save(get_filePath_fileName_fileExt(picLocation)[1]+_round.png)

顺便安利一个免费的开源的看图片的软件:https://imageglass.org/ 

 

前面两种方法效果都不好

第一种方法锯齿比较明显

技术分享图片

 

第二种方法有白色区域:

技术分享图片

 

python 直角图标生成圆角图标

原文:https://www.cnblogs.com/Summerio/p/11742186.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!