当输入图像旋转给定的度数时,如何找出最终图像的宽度和高度,然后裁剪以避免任何非图像区域,同时保持原始图像宽高比。
例如:
答案 0 :(得分:6)
W/t
矩形(由绿色和黄色三角形包围)旋转
图像宽高比为extend=True
。以下是获取w
和h
。
image = Image.open(...)
rotated = image.rotate(degrees, Image.BICUBIC, True)
aspect_ratio = float(image.size[0]) / image.size[1]
rotated_aspect_ratio = float(rotated.size[0]) / rotated.size[1]
angle = math.fabs(degrees) * math.pi / 180
if aspect_ratio < 1:
total_height = float(image.size[0]) / rotated_aspect_ratio
else:
total_height = float(image.size[1])
h = total_height / (aspect_ratio * math.sin(angle) + math.cos(angle))
w = h * aspect_ratio
现在可以在w
x h
尺寸的中心裁剪旋转的图像
得到最终的图像。
答案 1 :(得分:1)
E是复杂而短暂的
您可能希望重复使用sin(A),cos(A)和tan(A)预先计算
它们由两个组成部分组成,即构成每个长度的两个三角形边
W0 = # original rectangle width
H0 = # original rectangle height
cosA = cos(A)
sinA = sin(A)
tanA = tan(A)
W1 = W0 * cosA + H0 * sinA
H1 = H0 * cosA + W0 * sinA
E = W0 / (1 + tanA / W0 * H0)
W2 = E / tanA # requested width
H2 = W2 / W0 * H0 # requested height
免责声明:这都是未经测试的
编辑:根据要求,我最好猜测我的意思是:
r0 = w2/h2
h2 = (w0-e1) / sin(A)
h2 = e1/ cos(A) / r0
(w0-e1) / sin(A) = e1/ cos(A) / r0
x = cos(A) / sin(A)
e1 = w0 * x / (1/r0 + x)
E是w2的水平分量,通过a + b线延伸。然后是关于计算a
vs b
的内容,使用纵横比和sin / cos来计算出来。条款被取消,声音被提出,这就是我最终的结果。
答案 2 :(得分:0)
此代码返回此块的坐标
returned box - is the coords of this block
from PIL import Image
def cropp_rotated(image, degrees):
x, y = image.size
cosA = abs(math.cos(math.radians(degrees)))
sinA = abs(math.sin(math.radians(degrees)))
a = x * cosA
b = x * sinA
relation = a / (a + b)
right_indent1 = a - x * relation * cosA
relation = b / (a + b)
bottom_ident1 = b - x * relation *sinA
c = y * cosA
d = y * sinA
relation = c / (c + d)
right_indent2 = c - y * relation * cosA
relation = d / (c + d)
bottom_ident2 = d - y * relation *sinA
right_indent = max(right_indent1, right_indent2)
top_indent = max(bottom_ident1, bottom_ident2)
#size of rotated image:
w_rotated = x * cosA + y * sinA
h_rotated = y * cosA + x * sinA
box = (
int(right_indent),
int(top_indent),
int(w_rotated - right_indent),
int(h_rotated - top_indent))
return box
我只导入PIL以获取实际尺寸的图像。 您无法导入PIL,而是可以通过任何其他方式获取图像大小。