有没有办法在python中为图像制作漂亮的抗锯齿圆角? [英] Any way to make nice antialiased round corners for images in python?

查看:976
本文介绍了有没有办法在python中为图像制作漂亮的抗锯齿圆角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用python制作漂亮的圆角?目前我的项目中使用了PIL和GD2。它们都有一个 arc()方法,它允许你绘制一个四分之一圆,但是四分之一圈没有抗锯齿,所以图像看起来很脆。

Is there any way to make nice round corners with python? Currently PIL and GD2 are used in my project. Both of them have an arc() method, that allows you to draw a quater-circle, but the quater-circle is not antialiased, so the image looks crispy.

是否有任何简洁的方法来制作抗锯齿/平滑圆角?

Is there any neat way to make antialiased/smooth round corners?

推荐答案

所以,好吧,我找到了解决方案。

So, okay, i found the solution.

我之前使用的是超级采样方法 bobince ,但我发现图像变得模糊。所以,我不会提供完整的代码,但我会解释我的解决方案。

I used the supersampling method bobince proposed before, but i found that the images become blurry. So, I would not provide the full code, but i'll explain my solution.

它可能看起来有点像独白,但我还没有找到任何其他方法在不涉及任何其他库的情况下进行。

It may seem kind of idiotistic, but i haven't found any other way to do it without involving any other libraries.

使用PIL,您可以在图像上绘制白色(或任何需要的颜色,包括透明的颜色)角。然后你复制它。你现在应该有两个图像,带有非抗锯齿的白色圆角。

With PIL you draw white (or whatever color you need, involving transparent ones) corners on the image. Then you copy it. You should now have two images with crispy non-antialiased white round corners.

现在你应该使用向上扩展 - >向下缩小的超级采样(参见上面的 bobince的答案中的详细信息)方法来制作其中一个图像平滑(我使用8倍缩放)。

Now you should use scale-up -> scale-down supersampling (see bobince's answer above for details) method to make one of the images smooth (i used 8x scaling).

现在你有两张图片 - 一张带有清脆的角落,另一张带有光滑的角落,但图像模糊。

Now you have two images - one with crispy corners and another one with smooth corners, but with blurry picture.

现在你必须在脆弱的图像上制作角落透明并将整个图像粘贴在光滑的图像上。我在PIL库中使用了Image.composite(crispy_image,smooth_image,crispy_image)方法来执行此操作。

You have now to make corners on crispy image transparent and paste the whole image on the smooth one. I used the Image.composite(crispy_image, smooth_image, crispy_image) method in PIL library to do this.

Voila!

这篇关于有没有办法在python中为图像制作漂亮的抗锯齿圆角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆