我在Python Imaging Library中使用image.transform
将仿射变换应用于脸部检测图像,以根据眼睛的位置校准脸部。结果是正确的,但图像的颜色如下所示。为什么?
代码如下。最后两行是转换函数的调用。
def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.NEAREST):
if (scale is None) and (center is None):
return image.rotate(angle=angle, resample=resample)
nx,ny = x,y = center
sx=sy=1.0
if new_center:
(nx,ny) = new_center
if scale:
(sx,sy) = (scale, scale)
cosine = math.cos(angle)
sine = math.sin(angle)
a = cosine/sx
b = sine/sx
c = x-nx*a-ny*b
d = -sine/sy
e = cosine/sy
f = y-nx*d-ny*e
return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
def Distance(p1,p2):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
return math.sqrt(dx*dx+dy*dy)
image_center = ((eye_left[0]+eye_right[0])/2,(eye_left[1]+eye_right[1])/2)
rotatedImg = ScaleRotateTranslate(pilIm,center=image_center,angle=rotation)
答案 0 :(得分:0)
试过这个:
import Image
import math
def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.NEAREST):
if (scale is None) and (center is None):
return image.rotate(angle=angle, resample=resample)
nx,ny = x,y = center
sx=sy=1.0
if new_center:
(nx,ny) = new_center
if scale:
(sx,sy) = (scale, scale)
cosine = math.cos(angle)
sine = math.sin(angle)
a = cosine/sx
b = sine/sx
c = x-nx*a-ny*b
d = -sine/sy
e = cosine/sy
f = y-nx*d-ny*e
return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
def Distance(p1,p2):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
return math.sqrt(dx*dx+dy*dy)
pilIm = Image.open('/Users/carlos/Downloads/image.jpg').convert('RGB');
eye_left = (10,10)
eye_right = (40,30)
rotation = 0.3;
image_center = ((eye_left[0]+eye_right[0])/2,(eye_left[1]+eye_right[1])/2)
rotatedImg = ScaleRotateTranslate(pilIm,center=image_center,angle=rotation)
rotatedImg.save('out.jpg')
得到了这个:
所以我想,答案是它对我有用。尝试检查您的PIL安装以及您正在使用RGB编码。