Adding borders to an image using python

前端 未结 7 916
名媛妹妹
名媛妹妹 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

    You can create a new image with the desired new size, and paste the old image in the center, then saving it. If you want, you can overwrite the original image (are you sure? ;o)

    import Image
    
    old_im = Image.open('someimage.jpg')
    old_size = old_im.size
    
    new_size = (800, 800)
    new_im = Image.new("RGB", new_size)   ## luckily, this is already black!
    new_im.paste(old_im, ((new_size[0]-old_size[0])/2,
                          (new_size[1]-old_size[1])/2))
    
    new_im.show()
    # new_im.save('someimage.jpg')
    

提交回复
热议问题