我正在做一个涉及视频帧处理的项目,其中一个要求是为帧添加平滑的圆角。我做了一些关于如何使用PIL或任何其他图像处理库为图像添加圆角的研究,但所有这些都产生了粗糙的角,看起来不专业。下面是我尝试的代码:
def add_corners(im, rad):
circle = Image.new('L', (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2 - 1, rad * 2 - 1), 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('sand.png')
im = round_corner_jpg(im, 50)
im.save('sand_corner.png')
字符串
x1c 0d1x的数据
这是它产生的结果,如上图所示,角落是粗糙的,并且有像素化的视图,它们并不平滑,就像我们可以在图中使用IOS角落平滑一样:-
我也看到了这个问题,也有相同的要求,但他们没有显示如何实现它的代码:-Any way to make nice antialiased round corners for images in python?
我想确保没有损失的框架质量时,增加圆角和角落应该是顺利的,更好的是看起来像在figma角落平滑
编辑:-
out = cv2.VideoWriter('assets/pathToSave.mp4', fourcc, fps, (1920, 1080))
def generate_frame():
while cap.isOpened():
code, frame = cap.read()
if code:
yield frame
else:
print("done")
break
for i, frame in enumerate(generate_frame()):
# Convert opencv frames into PIL Image
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
ss = 4
alpha = Image.new('L', (im.width*ss, im.height*ss))
# Draw rounded rectangle and unsupersample
d = ImageDraw.Draw(alpha)
d.rounded_rectangle([0,0,im.width*ss-1,im.height*ss-1],radius=120,fill='white')
alpha = alpha.resize((alpha.width//ss, alpha.height//ss),
resample=Image.Resampling.LANCZOS)
alpha.save('DEBUG-alpha.png')
# Push new alpha channel into original image and save
im_pil.putalpha(alpha)
im_np = np.array(im_pil.convert('RGB'))
open_cv_image = im_np[:, :, ::-1].copy()
outputVideo.write(open_cv_image)
型
我尝试了上面的代码,以便从传入的视频帧中获得圆角,但我仍然得到了粗糙的角落:
的
请看白色框架,透明的圆角是背景的一部分,在此视频上呈现
2条答案
按热度按时间5tmbdcev1#
也许这样更好:
字符串
的数据
2uluyalo2#
我认为一个很好的解决方案是使用GaussianBlur从PIL导入ImageFilter。它看起来像这样=>
字符串
这个代码给了我这个=>
型
A close up of the blurred border
这是从这段代码生成的图像:D.):不幸的是文件大小太大了。好吧,我只会把它应用到你的图像在这里=>
型
这是你的图像,你圆角与它有模糊应用:
的数据
Gl具有编码:D.