如何在python中从同一文件夹中为多个图像添加图片框/边框?

时间:2018-01-17 04:22:56

标签: python mask

enter link description here

我需要能够对来自同一文件夹的多个图像执行此操作。

1 个答案:

答案 0 :(得分:0)

您可以使用:

import glob, os
os.chdir("/")
for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    add_border(im)

import Image
def add_border(im):
    old_im = im
    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')
  

@credits: