pycharm人脸五官替换
如何将下面图片第6行代码中的"face1.png", "face2.png"两个参数更改,更改为graphicsView上的图像img1和img2

pycharm人脸五官替换
如何将下面图片第6行代码中的"face1.png", "face2.png"两个参数更改,更改为graphicsView上的图像img1和img2

关注引自免费微信小程序:皆我百晓生
1. 确定graphicsView上图像的获取方式 在pycharm的代码中,可以使用如下代码来获取graphicsView上显示的图像:
img1 = graphicsView.scene().items()[0].pixmap().toImage()
img2 = graphicsView.scene().items()[1].pixmap().toImage()
这里假设图像是通过QGraphicsView显示,并且图像是通过QGraphicsPixmapItem添加到scene中的。
2. 修改代码中的参数 将代码中的"face1.png"和"face2.png"替换为img1和img2,可以使用如下代码:
img1_path = "path_to_image1.png" # 保存img1为文件
img2_path = "path_to_image2.png" # 保存img2为文件
img1.save(img1_path)
img2.save(img2_path)
# 将代码中的"face1.png"和"face2.png"替换为img1_path和img2_path
replace_string = "'face1.png', 'face2.png'"
replace_string_new = f"'{img1_path}', '{img2_path}'"
line_number = 6 # 代码所在行号
with open("your_script.py", "r") as f:
lines = f.readlines()
lines[line_number-1] = lines[line_number-1].replace(replace_string, replace_string_new)
with open("your_script.py", "w") as f:
f.writelines(lines)
这段代码将img1和img2保存为文件,并将文件路径替换到代码的第6行中。
注意事项: