How to resize frame's from video with aspect ratio

GGz*_*zet 0 python video opencv resize image

I am using Python 2.7, OpenCV. I have written this code.

import cv2
vidcap = cv2.VideoCapture('myvid2.mp4')
success,image = vidcap.read()
count = 0;
print "I am in success"
while success:
  success,image = vidcap.read()
  resize = cv2.resize(image, (640, 480)) 
  cv2.imwrite("%03d.jpg" % count, resize)     
  if cv2.waitKey(10) == 27:                     
      break
  count += 1
Run Code Online (Sandbox Code Playgroud)

I am working with video and am dividing the video into individual frames, as a .jpg images. I am also at the same time resizing the frames to dimension 640x480. The order of the frames is also being preserved. The only issue with the code is that it does not save the previous image-ratio.

For example how it look's like, resize from 1920x1080: 通过纯代码调整图像大小

There is a problem in ratio, as you can see. 1920x1080 16:9, but 640:480 4:3

How I ideally want it to be: 用理想代码调整图像大小

Thank you for your taking the time for reading the question. I will be very glad if you can help me solve this issue~ Have a good day, my friend.

Sar*_*wal 6

可以不使用硬编码的值640和480,而可以将原始帧的高度和宽度除以一个值并将其作为参数提供,如下所示:

while success:
  success,image = vidcap.read()
  height , width , layers =  img.shape
  new_h=height/2
  new_w=width/2
  resize = cv2.resize(image, (new_w, new_h)) 
  cv2.imwrite("%03d.jpg" % count, resize) 
Run Code Online (Sandbox Code Playgroud)