gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:32 26 4
gpt4 key购买 nike

我正在尝试旋转图像,但我想保持图像的大小。例如在下面的示例图像中,我想看到一个完整的矩形,旋转后没有黑色背景。

请帮助我,我将不胜感激。

现在我的代码是:

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

enter image description here enter image description here

最佳答案

要自动调整大小,您需要 expand 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.

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

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

关于Python Django 如何旋转图像并去除黑色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11902626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com