Adding borders to an image using python

前端 未结 7 967
名媛妹妹
名媛妹妹 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:37

    Alternatively, if you are using OpenCV, they have a function called copyMakeBorder that allows you to add padding to any of the sides of an image. Beyond solid colors, they've also got some cool options for fancy borders like reflecting or extending the image.

    import cv2
    
    img = cv2.imread('image.jpg')
    
    color = [101, 52, 152] # 'cause purple!
    
    # border widths; I set them all to 150
    top, bottom, left, right = [150]*4
    
    img_with_border = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
    

    Sources: OpenCV border tutorial and OpenCV 3.1.0 Docs for copyMakeBorder

提交回复
热议问题