Python枕头-裁剪图像并将文件名保存在数据框中

时间:2020-06-29 20:16:01

标签: python python-imaging-library

我想基于坐标在多个缩略图中裁剪图像,将图像和文件名保存在数据框中,请参见下面的代码。我不知道如何将缩略图文件名保存在数据框中。拜托,我需要一些帮助。谢谢!

import pandas as pd
from PIL import Image

df = pd.DataFrame({'Time': [2586, 2836, 2986, 3269, 3702],
                   'X': [751, 673, 542, 762, 624],
                   'Y': [458, 316, 287, 303, 297],
                   'User': ['u1', 'u1', 'u2', 'u2', 'u2']})

imagefile = 'Buc.jpg'
coordinates = list(df[['X', 'Y', 'User']].itertuples(index=False, name=None))
psize = 100

img = Image.open(imagefile)
for x, y, u in coordinates:
      box = (x-psize/2, y-psize/2, x+psize/2, y+psize/2)
      thumbnails = img.crop(box).save('%s.x%03d.y%03d.%01s.jpg'% (imagefile.replace('.jpg',''), x, y, u))
      df['Image'] = thumbnails.filename #add a new column with thumbnail filename ....doesn't work

0 个答案:

没有答案