title: Resize rectangular image to square, keeping ratio and fill background with black
body: <p>I'm trying to resize a batch of grayscale images that are 256 x N pixels (N varies, but is always ≤256). </p>
  
  <p>My intention is to downscale the images.</p>
  
  <p>The resize would have to output a square (1:1) image, with:</p>
  
  <ul>
  <li>resized image centered vertically</li>
  <li>aspect ratio maintained</li>
  <li>remaining pixels rendered black</li>
  </ul>
  
  <p>Visually this would be the desired result:</p>
  
  <p><a href="https://i.stack.imgur.com/hLARP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hLARP.png" alt="enter image description here"></a></p>
  
  <p>I have tried creating a numpy zeroes matrix with the target size (e.g. 200 x 200) but have not been able to paste the resized image into its vertical center.</p>
  
  <p>Any suggestions using cv2, PIL or numpy are welcome.</p>