Adding borders to an image using python

前端 未结 7 912
名媛妹妹
名媛妹妹 2020-11-29 05:10

I have a large number of images of a fixed size (say 500*500). I want to write a python script which will resize them to a fixed size (say 800*800) but will keep the origina

7条回答
  •  [愿得一人]
    2020-11-29 05:36

    You can load the image with scipy.misc.imread as a numpy array. Then create an array with the desired background with numpy.zeros((height, width, channels)) and paste the image at the desired location:

    import numpy as np
    import scipy.misc
    
    im = scipy.misc.imread('foo.jpg', mode='RGB')
    height, width, channels = im.shape
    
    # make canvas
    im_bg = np.zeros((height, width, channels))
    im_bg = (im_bg + 1) * 255  # e.g., make it white
    
    # Your work: Compute where it should be
    pad_left = ...
    pad_top = ...
    
    im_bg[pad_top:pad_top + height,
          pad_left:pad_left + width,
          :] = im
    # im_bg is now the image with the background.
    

提交回复
热议问题