本文介绍了如何使用pil来将徽标上没有白色背景(透明?)的round_corner?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方形徽标,需要将其圆角化,搜索了一会儿,并得到以下代码正在工作":

I got a square logo and I need to round_corner it, searched for a while and got the follow code "working":

def round_corner_jpg(image, radius):
    """generate round corner for image"""
    mask = Image.new('RGB', image.size)
    #mask = Image.new('RGB', (image.size[0] - radius, image.size[1] - radius))
    #mask = Image.new('L', image.size, 255)
    draw = aggdraw.Draw(mask)
    brush = aggdraw.Brush('black')
    width, height = mask.size
    draw.rectangle((0,0,width,height), aggdraw.Brush('white'))
    #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()
    del draw
    return ImageChops.add(mask, image)

然后我保存了返回的图像对象,但是它的角落有白色背景 如何摆脱白色背景或使其不可见?提前谢谢〜

then I saved the returned image object,however it has white background in the corner like thisHow can i get rid of the white background or make it invisible?Thanks in advance~

这是fraxel的代码,谢谢〜

here is the code by fraxel,thanks~

def add_corners(im, rad):
    circle = Image.new('L', (rad * 2, rad * 2), 0)
    draw = ImageDraw.Draw(circle)
    draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
    alpha = Image.new('L', im.size, "white")
    w, h = im.size
    alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
    alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
    alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
    alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
    im.putalpha(alpha)
    return im


if __name__ == '__main__':
    im = Image.open('1.jpg')
    im = add_corners(im, 100)
    im.save('out.png')`

很抱歉.我需要将图像形状设为椭圆形而不是矩形,即在图片上写东西,以及@fraxel,我仍然可以看到您为我处理的图片中的白色角落

I am so sorry..I need the image shape to be ellipse not rectangle,I.E. the write stuff off the pic,and @fraxel, I still can see the white corner in the pic you processed for me

推荐答案

首先,请确保您以支持透明性的格式保存图像. PNG可以,JPG不能...下面是一些相当不错的代码,它们将添加透明角.它是这样的:

First off, make sure you are saving your image in a format that supports transparency. PNG does, JPG does not... Below is some pretty nice code that will add transparent corners. It works like this:

  1. 使用draw.ellipse()
  2. 绘制半径为rad的圆
  3. 为alpha通道创建与您的图片大小相同的图片
  4. 将我们的圈子切成四个部分(圆角),并将其放置在alpha图像的正确角上
  5. 使用putalpha()
  6. 将Alpha通道放入图像中
  7. 另存为png,从而保持透明性.
  1. Draws a circle with radius, rad, using draw.ellipse()
  2. Create an image for the alpha channel the same size as your image
  3. Chop our circle into four pieces (the rounded corners), and place them in the correct corners of the alpha image
  4. Put the alpha channel into your image using putalpha()
  5. Save as a png, thus preserving transparency.

这是代码:

import Image, ImageDraw

def add_corners(im, rad):
    circle = Image.new('L', (rad * 2, rad * 2), 0)
    draw = ImageDraw.Draw(circle)
    draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
    alpha = Image.new('L', im.size, 255)
    w, h = im.size
    alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
    alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
    alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
    alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
    im.putalpha(alpha)
    return im

im = Image.open('tiger.jpg')
im = add_corners(im, 100)
im.save('tiger.png')

弯曲老虎示例:

这是经过此代码处理的图像,具有透明的角:

here is your image, processed with this code, giving transparent corners:

这篇关于如何使用pil来将徽标上没有白色背景(透明?)的round_corner?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:51