How do you keep a rotated rectangular image on the canvas?

With the default rotation center, expand grows the output canvas to accommodate the turned image.

Pixel experiment and interpretation

Turn a banner through 90 degrees. A fixed canvas keeps its original shape, while expansion makes it tall. Recompute annotations in the new coordinates.

from PIL import Image

im=Image.new('L',(6,2))
(im.rotate(90).size,im.rotate(90,expand=True).size)

Result ((6, 2), (2, 6))

中文

旋转非方形图像怎样保留完整画布?

rotate 的 expand 选项在默认中心旋转时扩展输出画布以容纳图像。

像素实验与适用边界

把一条横幅旋转 90 度:固定画布仍是横幅,展开后则是竖幅。相应的标注坐标也要随新画布重算。

from PIL import Image

im=Image.new('L',(6,2))
(im.rotate(90).size,im.rotate(90,expand=True).size)

结果 ((6, 2), (2, 6))

References / 参考来源

Keep reading / 继续阅读