python中的IOS角点平滑

bfnvny8b  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(149)

我正在做一个涉及视频帧处理的项目,其中一个要求是为帧添加平滑的圆角。我做了一些关于如何使用PIL或任何其他图像处理库为图像添加圆角的研究,但所有这些都产生了粗糙的角,看起来不专业。下面是我尝试的代码:

  1. def add_corners(im, rad):
  2. circle = Image.new('L', (rad * 2, rad * 2), 0)
  3. draw = ImageDraw.Draw(circle)
  4. draw.ellipse((0, 0, rad * 2 - 1, rad * 2 - 1), fill=255)
  5. alpha = Image.new('L', im.size, 255)
  6. w, h = im.size
  7. alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
  8. alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
  9. alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
  10. alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
  11. im.putalpha(alpha)
  12. return im
  13. im = Image.open('sand.png')
  14. im = round_corner_jpg(im, 50)
  15. im.save('sand_corner.png')

字符串
x1c 0d1x的数据
这是它产生的结果,如上图所示,角落是粗糙的,并且有像素化的视图,它们并不平滑,就像我们可以在图中使用IOS角落平滑一样:-

我也看到了这个问题,也有相同的要求,但他们没有显示如何实现它的代码:-Any way to make nice antialiased round corners for images in python?
我想确保没有损失的框架质量时,增加圆角和角落应该是顺利的,更好的是看起来像在figma角落平滑
编辑:-

  1. out = cv2.VideoWriter('assets/pathToSave.mp4', fourcc, fps, (1920, 1080))
  2. def generate_frame():
  3. while cap.isOpened():
  4. code, frame = cap.read()
  5. if code:
  6. yield frame
  7. else:
  8. print("done")
  9. break
  10. for i, frame in enumerate(generate_frame()):
  11. # Convert opencv frames into PIL Image
  12. img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  13. im_pil = Image.fromarray(img)
  14. ss = 4
  15. alpha = Image.new('L', (im.width*ss, im.height*ss))
  16. # Draw rounded rectangle and unsupersample
  17. d = ImageDraw.Draw(alpha)
  18. d.rounded_rectangle([0,0,im.width*ss-1,im.height*ss-1],radius=120,fill='white')
  19. alpha = alpha.resize((alpha.width//ss, alpha.height//ss),
  20. resample=Image.Resampling.LANCZOS)
  21. alpha.save('DEBUG-alpha.png')
  22. # Push new alpha channel into original image and save
  23. im_pil.putalpha(alpha)
  24. im_np = np.array(im_pil.convert('RGB'))
  25. open_cv_image = im_np[:, :, ::-1].copy()
  26. outputVideo.write(open_cv_image)


我尝试了上面的代码,以便从传入的视频帧中获得圆角,但我仍然得到了粗糙的角落:



请看白色框架,透明的圆角是背景的一部分,在此视频上呈现

5tmbdcev

5tmbdcev1#

也许这样更好:

  1. #!/usr/bin/env python3
  2. from PIL import Image, ImageDraw
  3. # Open image
  4. im = Image.open('rock.jpg')
  5. # Make alpha channel, supersampled by factor 4
  6. ss = 4
  7. alpha = Image.new('L', (im.width*ss, im.height*ss))
  8. # Draw rounded rectangle and unsupersample
  9. d = ImageDraw.Draw(alpha)
  10. d.rounded_rectangle([0,0,im.width*ss-1,im.height*ss-1],radius=120,fill='white')
  11. alpha = alpha.resize((alpha.width//ss, alpha.height//ss), resample=Image.Resampling.LANCZOS)
  12. alpha.save('DEBUG-alpha.png')
  13. # Push new alpha channel into original image and save
  14. im.putalpha(alpha)
  15. im.save('result.png')

字符串


的数据

展开查看全部
2uluyalo

2uluyalo2#

我认为一个很好的解决方案是使用GaussianBlur从PIL导入ImageFilter。它看起来像这样=>

  1. from PIL import Image
  2. from PIL import ImageDraw
  3. from PIL import ImageFilter
  4. def convert_jpg_to_png(jpg_path, png_path):
  5. img = Image.open(jpg_path)
  6. img.save(png_path)
  7. def add_corners(im, rad,blur_radius):
  8. circle = Image.new('L', (rad * 2, rad * 2), 0)
  9. draw = ImageDraw.Draw(circle)
  10. draw.ellipse((0, 0, rad * 2 - 1, rad * 2 - 1), fill=255)
  11. alpha = Image.new('L', im.size, 255)
  12. w, h = im.size
  13. #cirlce quadrents
  14. alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
  15. alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
  16. alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
  17. alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad))
  18. #add blur to just cornors
  19. for i in range(0, w, w - rad):
  20. for j in range(0, h, h - rad):
  21. corner = alpha.crop((i, j, i + rad, j + rad))
  22. blurred_corner = corner.filter(ImageFilter.GaussianBlur(blur_radius))
  23. alpha.paste(blurred_corner, (i, j))
  24. im.putalpha(alpha)
  25. return im

字符串
这个代码给了我这个=>

  1. convert_jpg_to_png('mountain-landscape-2031539_1280.jpg', 'mountain-landscape-2031539_1280.png')
  2. im = Image.open('mountain-landscape-2031539_1280.png')
  3. im = add_corners(im, 500,5)
  4. im.save('mountain-landscape-2031539_1280_corner.png')


A close up of the blurred border
这是从这段代码生成的图像:D.):不幸的是文件大小太大了。好吧,我只会把它应用到你的图像在这里=>

  1. im = Image.open('Beirp.png')
  2. im = add_corners(im, 50,2)
  3. im.save('new_idk_image.png')


这是你的图像,你圆角与它有模糊应用:


的数据
Gl具有编码:D.

展开查看全部

相关问题