How can a square cover crop favor one side?
fit scales and crops to an exact target size. Its centering parameter positions the retained area.
Pixel experiment and interpretation
Anchor the square window at the right to keep the bright edge. Check text near the boundary before defaulting to a centered crop.
from PIL import Image, ImageOps
def row(values):
return Image.frombytes('L', (len(values), 1), bytes(values))
im=row([10,20,30,40])
ImageOps.fit(im,(1,1),centering=(1,0.5)).getpixel((0,0))Result 40
中文
如何决定方形封面从长图哪侧截取?
fit 同时缩放和裁剪以精确填满目标尺寸,centering 控制保留区域的偏向。
像素实验与适用边界
示例让方形窗口靠右,最右的亮像素得到保留。换成居中裁剪前应检查画面边缘的文字。
from PIL import Image, ImageOps
def row(values):
return Image.frombytes('L', (len(values), 1), bytes(values))
im=row([10,20,30,40])
ImageOps.fit(im,(1,1),centering=(1,0.5)).getpixel((0,0))结果 40