You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pillow rotate函数异常:仅旋转图像内容未旋转视口求助

解决方法

问题出在Pillow的rotate()方法默认行为上——它只会旋转图像内容,但保持原始图像的尺寸不变,旋转后的内容会被适配进原尺寸的画布,导致看起来像是“内容转了,画布没转”。

只需要给rotate()方法加上expand=True参数,就能让画布自动调整尺寸,完整容纳旋转后的内容:

block = scene.read_block()
image = PILImage.fromarray(block)
# 添加expand=True参数,让画布适配旋转后的内容
image_rotate = image.rotate(-90, expand=True)
buf = BytesIO()
image_rotate.save(buf, "JPEG", quality=90)        
return buf.getvalue()  

expand=True的作用是自动计算旋转后所需的画布宽高,让旋转后的图像内容完整显示,画布尺寸也会跟着旋转后的内容变化。

内容的提问来源于stack exchange,提问作者Gina Marano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 08:56:07