Python-调整图片大小 [英] Python - resize image

查看:650
本文介绍了Python-调整图片大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来检测人脸:

I'm using the code below to detect faces:

import io
import picamera
import cv2
import numpy
import PIL
from PIL import Image
from resizeimage import resizeimage

#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')

#Create a memory stream so photos doesn't need to be saved in a file
stream = io.BytesIO()

#Get the picture (low resolution, so it should be quite fast)
#Here you can also specify other parameters (e.g.:rotate the image)
with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.vflip = False
    camera.hflip = False
    camera.brightness = 60
    camera.capture(stream, format='jpeg')

#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)

#Now creates an OpenCV image
image = cv2.imdecode(buff, 1)

#Load a cascade file for detecting faces
#face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')

#Convert to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.1, 5)

print "Found "+str(len(faces))+" face(s)"

#Draw a rectangle around every found face
#Crop faces and save to separate files
id = 1
for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
    cropped = image[ y : y+h, x : x+w ]
    #RESIZE IMAGE to 92x112
    cropped = cv2.resize(cropped,None,92,112)
    cv2.imwrite("../reco/test_faces/cropped_face" + str(id) + ".png", cropped)
    id = id + 1

最后,我想将人脸裁剪为图像文件,并将其大小调整为92x112.这就是我尝试的方法

At the end I want to crop the faces into image files and resize them to 92x112. This is what I try with

cropped = cv2.resize(cropped,None,92,112)

当我运行它时,我得到

OpenCV错误:断言调整大小(file/build/opencv-ISmtkH/opencv-2.4.9.1 + dfsg/modules/imgproc/src/imgwarp.cpp,第1835行 追溯(最近一次通话): 在第48行的文件"1track.py"中 裁剪= cv2.resize(裁剪,无,92,112) cv2.error:/build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp:1835:错误:(-215)dsize.area()||函数调整大小中的(inv_scale_x> 0&& inv_scale_y> 0)

OpenCV Error: Assertion failed (dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0)) in resize, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp, line 1835 Traceback (most recent call last): File "1track.py", line 48, in cropped = cv2.resize(cropped,None,92,112) cv2.error: /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp:1835: error: (-215) dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0) in function resize

任何帮助都会很棒. 对不起,如果马术很愚蠢,我是编程的绝对初学者,上面的代码是谷歌搜索的结果. 谢谢!

Any help would be great. Sorry if th equestion is stupid, I'm an absolute beginner in programming, the code above is a googling result.. Thanks!

推荐答案

要将图像调整为新尺寸,您需要知道新尺寸和当前尺寸之间的比例.因此,如果要将(例如)640x480的图像设置为92x112的图像:

To resize the image to new dimensions, you need to know the ratio between the new dimensions and the current ones. So if you want to set (for example) a 640x480 image into a 92x112 image:

92/640 = 0.143 112/480 = 0.233

92/640=0.143 112/480=0.233

您可以在cv2.resize函数中使用以下比率:

You use these ratios in the cv2.resize function:

cropped = cv2.resize(cropped, (0,0), fx=0.143, fy=0.233) 

这篇关于Python-调整图片大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆