python - _imaging C 模块未安装 Python 嵌入

标签 python opencv python-imaging-library embedding imaging

我有一个单独执行时运行良好的 python 脚本,它使用来自 PIL 的 Image 模块。
但是当我尝试通过使用 python 嵌入来使用相同的脚本时,我得到了

运行时错误 R6034
然后在命令窗口中

Traceback (most recent call last):
  File "FaceAlign.py", line 120, in <module>
    CropFace(image, eye_left, eye_right, offset_pct=(0.2,0.2), dest_sz=(100,100)
).save("Images/"+subjectname+"/"+str(count)+".jpg")
  File "FaceAlign.py", line 77, in CropFace
    image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
  File "FaceAlign.py", line 60, in ScaleRotateTranslate
    return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=res
ample)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1609, in transform
    im = new(self.mode, size, None)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1755, in new
    return Image()._new(core.new(mode, size))
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 37, in __getattr__
    raise ImportError("The _imaging C module is not installed")
ImportError: The _imaging C module is not installed

代码块是:
int argc=8;
char *argv[8];
char buffer[5][50];
argv[0] = "Face_Align";
argv[1] = "test.jpg";
sprintf_s(buffer[0], "%d", /*faces[0].x + eyes[0].x + eyes[0].width*0.5*/252);
argv[2] = buffer[0];
sprintf_s(buffer[1], "%d", /*faces[0].y + eyes[0].y + eyes[0].height*0.5*/364);
argv[3] = buffer[1];
sprintf_s(buffer[2], "%d", /*faces[0].x + eyes[1].x + eyes[1].width*0.5*/420);
argv[4] = buffer[2];
sprintf_s(buffer[3], "%d", /*faces[0].y + eyes[1].y + eyes[1].height*0.5*/366);
argv[5] = buffer[3];
sprintf_s(buffer[4], "%d", /*counter*/1);
argv[6] = buffer[4];
argv[7] = /*imagepath*/ "Abhishek";

printf("%s \n%s\n%s\n%s\n%s\n", argv[2], argv[3], argv[4], argv[5], argv[6]);

Py_SetProgramName(argv[0]);
Py_Initialize();
PyRun_SimpleString("import Image");
PySys_SetArgv(argc, argv);
PyObject *PyFileObject = PyFile_FromString("FaceAlign.py", "r");
PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "FaceAlign.py", 1);
Py_Finalize();

而python脚本是:
import sys
print sys.path
import math, os

def Distance(p1,p2):
    dx = p2[0] - p1[0]
    dy = p2[1] - p1[1]
    return math.sqrt(dx*dx+dy*dy)

def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC):
    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 CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
    # calculate offsets in original image
    offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
    offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
    # get the direction
    eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1])
    # calc rotation angle in radians
    rotation = -math.atan2(float(eye_direction[1]),float(eye_direction[0]))
    # distance between them
    dist = Distance(eye_left, eye_right)
    # calculate the reference eye-width
    reference = dest_sz[0] - 2.0*offset_h
    # scale factor
    scale = float(dist)/float(reference)
    # rotate original around the left eye
    image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
    # crop the rotated image
    crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v)
    crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)
    image = image.crop((int(crop_xy[0]), int(crop_xy[1]), int(crop_xy[0]+crop_size[0]), int(crop_xy[1]+crop_size[1])))
    # resize it
    image = image.resize(dest_sz, Image.ANTIALIAS)
    return image



if __name__ == "__main__":
    if len(sys.argv) != 8:
        print "usage: create_csv <base_path>"
        sys.exit(1)
    image = Image.open(sys.argv[1])
    eye_left = (int(sys.argv[2]), int(sys.argv[3]))
    eye_right = (int(sys.argv[4]), int(sys.argv[5]))
    count = int(sys.argv[6])
    subjectname = sys.argv[7]
    print image
    print eye_left
    print eye_right
    print count
    print subjectname

    CropFace(image, eye_left, eye_right, offset_pct=(0.2,0.2), dest_sz=(100,100)).save("Images/"+subjectname+"/"+str(count)+".jpg")

我检查了答案并验证了 PIL 与 python 的版本兼容。需要帮助
谢谢

最佳答案

将此与其他导入一起添加到 FaceAlign.py 的顶部:

from PIL import Image

关于python - _imaging C 模块未安装 Python 嵌入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26829383/

相关文章:

c++ - OpenCV VLFeat Slic 函数调用

python - 导入图像适用于 GAE,但不适用于 dev_appserver.py

python - S101 为 python 测试检测到断言的使用

python - 使用 Python 对 JSON 字符串进行插值替换

python - 如何在Python列表中包含反斜杠字符?

matlab - 将 Matlab 与 C++ 集成

python - 如何使用opencv的cv2.putTexttext显示文字2秒?

python - 给定多边形的边,判断两个区域是否相交

python - PIL简单渐变和TypeError : an integer is required

python - css first 和 last 在 selenium rc 中不起作用