【问题标题】:Why am I getting a NoneType?为什么我得到一个 NoneType?
【发布时间】:2020-11-08 21:12:04
【问题描述】:

谁能看出我哪里出错了?我已经逐行浏览了它,直到 new_face 都产生了预期的结果,当 new_face 开始产生 NoneType 时。

import numpy, cv2
from PIL import Image

face_cascade = cv2.CascadeClassifier("..data/haarcascade_frontalface_default.xml") #absolute path cut down for privacy

def find_faces(image_for_faces):
    image = image_for_faces.resize((1800,3150))
    image_np = numpy.asarray(image)
    image_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(image_gray) #correct numpy array, prints correct coordinates per face
    face_list = []
    image.show()
    for face in faces:
        print(face)
        box = (face[0], face[1], face[0]+face[2], face[1]+face[3]) #correctly produces numpy coordinates
        copy_image = image.copy()
        cropped = copy_image.crop(box = (box))
        new_face = cropped.thumbnail((128,128))
        face_list.append(new_face)
    return face_list

y = Image.open('famphoto.jpg')
z = y.convert('RGB')
x = find_faces(z)

【问题讨论】:

    标签: python python-imaging-library cv2


    【解决方案1】:

    Image.thumbnail() 修改了Image 对象,在docs 中了解更多信息。它提到它返回一个None 类型。

    图片.缩略图:

    将此图像制作为缩略图。此方法将图像修改为 包含自身的缩略图版本,不大于给定大小。 此方法计算适当的缩略图大小以保留 图片方面,调用draft()方法配置文件 阅读器(如果适用),最后调整图像大小。

    【讨论】:

    • 就是这样!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2021-03-29
    • 1970-01-01
    相关资源
    最近更新 更多