Resize rectangular image to square, keeping ratio and fill background with black

后端 未结 4 437
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 09:09

I\'m trying to resize a batch of grayscale images that are 256 x N pixels (N varies, but is always ≤256).

My intention is to downscale the images.

The resiz

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 09:51

    from PIL import Image
    
    def reshape(image):
        '''
        Reshapes the non-square image by pasting
        it to the centre of a black canvas of size
        n*n where n is the biggest dimension of
        the non-square image. 
        '''
        old_size = image.size
        max_dimension, min_dimension = max(old_size), min(old_size)
        desired_size = (max_dimension, max_dimension)
        position = int(max_dimension/2) - int(min_dimension/2) 
        blank_image = Image.new("RGB", desired_size, color='black')
        if image.height

提交回复
热议问题