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
Add this to the top of FaceAlign.py with the other imports: