首页 > 解决方案 > 我想运行一个通过python中的数据框创建图像的函数

问题描述

这是我对 python 非常陌生的数据框的屏幕截图,如果我的问题有任何帮助或建议,我将不胜感激。所以我创建了几行非常简单的代码来合并两个相邻的图像,但现在我想对包含 80 对图像的列表执行该操作,我已经使用 pd.来自熊猫的 read_excel。任何建议,即使只是关于在哪里搜索以了解更多信息,都非常感谢,因为我只是不知道在哪里看。

 from PIL import Image
import os
os.chdir("/Users/someone/Documents/stimuli_final")

img = Image.open("/Users/me/Documents/StimuliOnly/1_5_1_n1.bmp")
img = img.resize((1280, 1280))
img1 = Image.open("/Users/me/Documents/StimuliOnly/1_10_1_n2.bmp")
img1 = img1.resize((1280, 1280))
bi = Image.new("RGBA", (2760, 1280), ("#808080"))
bi.paste(img, (0,0,1280,1280))
bi.paste(img1, (1480,0,2760,1280))
bi.save("stim5with14vs28congruent.png")

标签: pythonpandasimageloopsspyder

解决方案


如果您在 Excel 文件中添加了另一列名为“输出文件名”并填充了该列,则可以使用如下内容:

def helper(data):
    path1, path2, output_file_name = data["Bild 1"], data["Bild 2"], data["output file name"]
    img = Image.open(path1)
    img = img.resize((1280, 1280))
    img1 = Image.open(path2)
    img1 = img1.resize((1280, 1280))
    bi = Image.new("RGBA", (2760, 1280), ("#808080"))
    bi.paste(img, (0,0,1280,1280))
    bi.paste(img1, (1480,0,2760,1280))
    bi.save(output_file_name)

df.apply(helper, axis=1)

推荐阅读