将 PIL 的输出(多个文件)保存到目录
Saving output (multiple files) of PIL to directory
我正在做一个基本项目,它需要我拍摄图像并将面部(仅)与该图像分开,并将它们输出到一个单独的文件夹。到目前为止,我已经编写了下面的代码(编码新手和 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()
错误在第 .. 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
我正在做一个基本项目,它需要我拍摄图像并将面部(仅)与该图像分开,并将它们输出到一个单独的文件夹。到目前为止,我已经编写了下面的代码(编码新手和 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()
错误在第 .. 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