1

I have a python script that runs well when executed separately, it uses Image module from PIL. But when I try to use the same script by using python embedding I am getting

Runtime Error R6034 and then in the command window

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

the code block is:

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();

and the python script is :

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")

I have checked answers and verified that the PIL is compatible with the version of python.Need help Thanks

Nilesh
  • 20,521
  • 16
  • 92
  • 148
user2580488
  • 73
  • 10
  • Why isn't the `import Image` in the `FaceAlign.py` file? – martineau Nov 09 '14 at 17:56
  • Oops it was there while debugging I moved the "import Image" line to CPP file to see if it is importing or not. The module is importing fine. But it still won't work. PyRun_SimpleString("import Image"); – user2580488 Nov 10 '14 at 14:05

1 Answers1

0

Add this to the top of FaceAlign.py with the other imports:

from PIL import Image
Hugo
  • 27,885
  • 8
  • 82
  • 98
  • It didn't worked! Could it be some path issue. I am using Visual Studio 2010. I printed the sys.path from the script and it has ././site-packages/PIL in it.I read somewhere that it could be due to some conflicting .dll tried that too but still no success – user2580488 Nov 10 '14 at 14:02
  • @user2580488: What version of Python are you using? On Windows the various versions were built with different versions of MS Visual Studio and you can run into C/C++ runtime dll issues trying to mix code compiled with one with a Python built with another. – martineau Nov 10 '14 at 18:08
  • @martineau I am using Python 2.7 – user2580488 Nov 11 '14 at 20:17
  • Have you installed PIL or Pillow or both? Where and how did you install them? I recommend just installing one of them, and first finding and uninstalling each, and then just installing Pillow with `pip install pillow`. – Hugo Nov 12 '14 at 09:12
  • I didn't install Pillow and I installed PIL from [link](http://www.pythonware.com/products/pil/) I am using 1.1.7 for python 2.7 – user2580488 Nov 12 '14 at 12:09
  • Have you tried uninstalling PIL and installing Pillow with `pip install pillow`? Pillow is an updated, maintained and bugfixed fork of PIL. – Hugo Nov 12 '14 at 12:24
  • @user2580488: If you're using Python 2.7 I think you need to use VS 2008 to compile your C code -- otherwise you will have a runtime dll issues. You can also use a compiler that Microsoft has released specifically for Python 2.7 -- see the EDIT at the end of [this answer](http://stackoverflow.com/a/8705722/355230) for where to get it. – martineau Nov 14 '14 at 20:49
  • There's no need to mess with fiddly C compilers on Windows because Pillow is released with prebuilt Windows binaries: `pip install pillow` will take care of everything in seconds. – Hugo Nov 14 '14 at 21:37