对于PIL中的线条和椭圆,图像很粗糙.
我只在resize和缩略图中找到了抗锯齿.
绘制直线或椭圆时有没有办法做抗锯齿?
我有一个方形徽标,我需要round_corner它,搜索了一会儿,并得到以下代码"工作":
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 …Run Code Online (Sandbox Code Playgroud) 我正在使用pgmagick生成一个圆形缩略图.我正在使用类似于此处讨论的过程,这确实为我生成了一个漂亮的圆形缩略图.但是,我需要在圆的半径周围有一个白色边框.
我最初的方法是创建一个具有透明背景的稍大的白色圆圈的新图像,并将缩略图复合在其上,让白色圆圈从缩略图下方"峰顶"并创建边框效果.这是我用来实现的pgmagick代码:
border_background = Image(Geometry(220, 220), Color('transparent'))
drawer = Draw()
drawer.circle(110, 110, 33.75, 33.75)
drawer.fill_color(Color('white'))
drawer.stroke_antialias(False)
border_background.draw(drawer.drawer)
border_background.composite(original_thumbnail, 0, 0, CompositeOperator.OverCompositeOp)
Run Code Online (Sandbox Code Playgroud)
这"有效",但周围的白色边框相当扭曲,边缘不连贯 - 没有生产就绪.如果我拿出drawer.stroke_antialias(False),那就更糟了.
有关使用pgmagick使这个边框更平滑的任何想法?
avatar.jpg

back.jpg

如何合成两个图像如下?
我效果:

我目前正在使用此方法为我的用户圆化图像的边缘:
def _add_corners(self, im, rad=100):
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
Run Code Online (Sandbox Code Playgroud)
舍入效果非常好,我对此很满意。但是,我还想在边缘的限制内在图像周围绘制边框。我在网上阅读的大部分内容都展示了如何在图像本身上绘制边框(而不是我正在做的圆形边框)。有没有办法做到这一点?我已阅读以下内容: …