我正在尝试使用 Pillow (python-imaging-library) Python 库在我的 .png 图像周围创建轮廓/描边/边框(选择任何颜色和宽度)。您可以在这里看到原始图像和我想要的结果(由手机应用程序创建):

您可以在这里下载原始图像的png文件:https://pixabay.com/illustrations/brain-character-organ-smart-eyes-1773885/
我已经用中等尺寸(1280x1138)完成了它,但也许用最小尺寸(640x569)来完成它更好。
我尝试用两种方法解决这个问题。
方法一
第一种方法是创建 Brain.png 图像的全黑图像,将其放大,然后将原始彩色大脑图像粘贴在其上。这是我的代码:
brain_black = Image.open("brain.png") #load brain image
width = brain_black.width #in order not to type a lot
height = brain_black.height #in order not to type a lot
rectangle = Image.new("RGBA", (width, height), "black") #creating a black rectangle in the size of the brain image
brain_black.paste(rectangle, mask=brain_black) #pasting on the brain image the black rectangle, and masking it with the brain picture
#now brain_black is the brain.png …Run Code Online (Sandbox Code Playgroud)