Adding borders to an image using python

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

    It is important to consider old dimension, new dimension and their difference here. If the difference is odd (not even), you will need to specify slightly different values for left, top, right and bottom borders.

    Assume the old dimension is ow,oh and new one is nw,nh. So, this would be the answer:

    import Image, ImageOps
    img = Image.open('original-image.png')
    deltaw=nw-ow
    deltah=nh-oh
    ltrb_border=(deltaw/2,deltah/2,deltaw-(deltaw/2),deltah-(deltah/2))
    img_with_border = ImageOps.expand(img,border=ltrb_border,fill='black')
    img_with_border.save('imaged-with-border.png')
    
    0 讨论(0)
提交回复
热议问题