首页 > 解决方案 > 如何在图像周围添加圆形边框?

问题描述

我有一个矩形图像,我想把它的角弄圆,然后给它添加一个黑色边框(所以边框也是圆形的)。

有没有简单的方法来实现它?

那将是所需的输出:

在此处输入图像描述

类似未回答的问题

标签: pythonimage-processingpython-imaging-library

解决方案


我喜欢用 SVG 绘制圆角矩形来进行更改——尤其是因为有人认为我总是使用 ImageMagick ;-)

#!/usr/bin/env python3

from PIL import ImageOps, Image
from cairosvg import svg2png
from io import BytesIO

def frame(im, thickness=5):
    # Get input image width and height, and calculate output width and height
    iw, ih = im.size
    ow, oh = iw+2*thickness, ih+2*thickness

    # Draw outer black rounded rect into memory as PNG
    outer = f'<svg width="{ow}" height="{oh}" style="background-color:none"><rect rx="20" ry="20" width="{ow}" height="{oh}" fill="black"/></svg>'
    png   = svg2png(bytestring=outer)
    outer = Image.open(BytesIO(png))

    # Draw inner white rounded rect, offset by thickness into memory as PNG
    inner = f'<svg width="{ow}" height="{oh}"><rect x="{thickness}" y="{thickness}" rx="20" ry="20" width="{iw}" height="{ih}" fill="white"/></svg>'
    png   = svg2png(bytestring=inner)
    inner = Image.open(BytesIO(png)).convert('L')

    # Expand original canvas with black to match output size
    expanded = ImageOps.expand(im, border=thickness, fill=(0,0,0)).convert('RGB')

    # Paste expanded image onto outer black border using inner white rectangle as mask
    outer.paste(expanded, None, inner)
    return outer

# Open image, frame it and save
im = Image.open('monsters.jpg')
result = frame(im, thickness=10)
result.save('result.png')

输出图像

在此处输入图像描述

输入图像

在此处输入图像描述

您可以使用rxry更改拐角的半径。

这里是outer,innerexpanded- 正如你所看到的,它们的大小都相同,以便于在彼此之上组合。

在此处输入图像描述

其他想法:

  • 您还可以通过在黑盒子中绘制一个白色矩形并在其上运行中值滤波器或一些形态腐蚀来创建圆角。如果你过滤这个:

在此处输入图像描述

使用 15x15 中值滤波器,你会得到:

在此处输入图像描述


以防万一有人想要ImageMagick解决方案:

#!/bin/bash

# Get width and height of input image
read iw ih < <(identify -format "%w %h" monsters.jpg)

# Calculate size of output image, assumes thickness=10
((ow=iw+20))
((oh=ih+20))

magick -size ${ow}x${oh} xc:none  -fill black -draw "roundrectangle 0,0 $ow,$oh 20,20" \
    \( -size ${iw}x${ih} xc:black -fill white -draw "roundrectangle 0,0,$iw,$ih 20,20" monsters.jpg -compose darken -composite \) \
       -gravity center -compose over -composite result.png

在此处输入图像描述

关键词:Python、图像处理、圆角、圆角、边框、SVG、cairo、cairosvg、SVG 到 PNG、SVG 作为 PNG、SVG 到 PIL、PIL、Pillow。


推荐阅读