【问题标题】:Saving output (multiple files) of PIL to directory将 PIL 的输出(多个文件)保存到目录
【发布时间】:2021-01-10 09:01:40
【问题描述】:

我正在做一个基本项目,该项目需要我拍摄一张图像并将人脸(仅)从该图像中分离出来,然后将它们输出到一个单独的文件夹中。到目前为止,我已经编写了下面的代码(编码和 Python 的新手)。这段代码的唯一问题是,当我知道有多个面孔时,它只保存一张图片。如果我将最后一行替换为注释部分 pil_image.show(),那么它会通过为每个人打开一个单独的文件来显示所有面孔,但不会将输出保存在任何地方。

我需要有关指向软件的帮助,以便它将所有输出保存在一个目录中。

from PIL import Image
import face_recognition

path = r"C:\Users\Julio\Desktop\Output\new"
image = face_recognition.load_image_file("MultiPeople/twopeople.jpeg")
count = 0

face_locations = face_recognition.face_locations(image)

print("I found {} face(s) in this photograph.".format(len(face_locations)))

for face_location in face_locations:
    top, right, bottom, left = face_location
    print("I found a face in image location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.save(str(path) + ".jpg")

    #pil_image.show()

【问题讨论】:

    标签: python-3.x python-imaging-library face-recognition


    【解决方案1】:

    错误在行中。pil_image.save(str(path) + ".jpg"),因为所有文件都在循环中写入同一文件路径。

    你可以试试这个。

    face_counter = 0
    for face_location in face_locations:
        top, right, bottom, left = face_location
        print("I found a face in image location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
    
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.save(str(path) + str(face_counter) + ".jpg")
        face_counter += 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多