Skip to content

Commit 34bd7fa

Browse files
committed
fixed video_io transpose & rotate options
1 parent fa356b3 commit 34bd7fa

3 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/ffmpegio/configure.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -846,23 +846,25 @@ def video_stream(
846846
vfilters.append("vflip")
847847

848848
transpose = stream_opts.get("transpose", None)
849-
if transpose:
850-
vfilters.append(("transpose", transpose % 8))
851-
852-
rot = stream_opts.get("rotate", None)
853-
if rot:
854-
w, h = utils.get_rotated_shape(w, h, rot)
849+
if transpose is not None:
850+
vfilters.append(("transpose", transpose))
851+
if h > w or (isinstance(transpose, int) and transpose < 4):
852+
w, h = h, w
853+
# 4-7, the transposition is only done if the input video geometry is portrait and not landscape
854+
855+
rotdeg = stream_opts.get("rotate", None)
856+
if rotdeg:
857+
w, h, rot = utils.get_rotated_shape(w, h, rotdeg)
855858
vfilters.append(("rotate", rot, w, h, {"c": bg_color}))
856859

857860
size = stream_opts.get("size", None)
858861
scale = stream_opts.get("scale", None)
859862
if size or scale:
860863
if size:
861-
if size[0] <= 0:
862-
size[0] = size[1] * w / h
863-
elif size[1] <= 0:
864-
size[1] = size[0] * h / w
865-
w, h = size
864+
w, h = (
865+
size[1] * w / h if size[0] <= 0 else size[0],
866+
size[0] * h / w if size[1] <= 0 else size[1],
867+
)
866868
else:
867869
sep = isinstance(scale, Sequence)
868870
w *= scale[0] if sep else scale

src/ffmpegio/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def get_rotated_shape(w, h, deg):
172172
C = np.cos(theta)
173173
S = np.sin(theta)
174174
X = np.matmul([[C, -S], [S, C]], [[w, w, 0.0], [0.0, h, h]])
175-
return int(round(abs(X[0, 0] - X[0, 2]))), int(round(abs(X[1, 1])))
175+
return int(round(abs(X[0, 0] - X[0, 2]))), int(round(abs(X[1, 1]))), theta
176176

177177

178178
def get_audio_format(fmt):

tests/test_image.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,28 @@ def test_read_write():
7272
# plt.show()
7373

7474

75-
# test_read_write()
75+
# test_read_write()
76+
77+
if __name__ == "__main__":
78+
79+
from matplotlib import pyplot as plt
80+
import logging
81+
82+
# logging.basicConfig(level=logging.DEBUG)
83+
84+
IM = image.read(
85+
"tests/assets/ffmpeg-logo.png",
86+
# crop=(0, 0, 200, 0),
87+
# rotate=30,
88+
pix_fmt='rgb24',
89+
fill_color="blue",
90+
transpose=0,
91+
size=(200,-1)
92+
)
93+
print(IM.shape)
94+
95+
plt.figure(figsize=(IM.shape[1] / 96, IM.shape[0] / 96))
96+
plt.imshow(IM)
97+
plt.gca().set_position((0, 0, 1, 1))
98+
# plt.axis('off')
99+
plt.show()

0 commit comments

Comments
 (0)