加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python Django如何旋转图像并删除黑色?

发布时间:2020-12-20 13:29:31 所属栏目:Python 来源:网络整理
导读:我正在尝试旋转图像,但我想保持图像的大小.例如,在下面的示例图像中,我希望看到一个完整的矩形,并且在旋转后没有黑色背景颜色. 请帮帮我,我很感激. 现在我的代码是: src_im = Image.open("test.gif")im = src_im.rotate(30)im.save("result.gif") 解决方法
我正在尝试旋转图像,但我想保持图像的大小.例如,在下面的示例图像中,我希望看到一个完整的矩形,并且在旋转后没有黑色背景颜色.

请帮帮我,我很感激.

现在我的代码是:

src_im = Image.open("test.gif")
im = src_im.rotate(30)
im.save("result.gif")

解决方法

要自动调整大小,您需要展开kwarg:

src_im = Image.open("test.gif")
im = src_im.rotate(30,expand=True)
im.save("result.gif")

正如the PIL docs解释:

The expand argument,if true,indicates that the output image should be made large enough to hold the rotated image. If omitted or false,the output image has the same size as the input image.

在黑色背景上,保存GIF图像时需要specify the transparent colour:

transparency = im.info['transparency'] 
im.save('icon.gif',transparency=transparency)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读