how to save a nonetype as a function in python

Solutions on MaxInterview for how to save a nonetype as a function in python by the best coders in the world

showing results for - "how to save a nonetype as a function in python"
Juan Martín
18 Feb 2018
1import sys, math, Image
2import os
3
4def Distance(p1,p2):
5  dx = p2[0] - p1[0]
6  dy = p2[1] - p1[1]
7  return math.sqrt(dx*dx+dy*dy)
8
9def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC):
10  if (scale is None) and (center is None):
11    return image.rotate(angle=angle, resample=resample)
12  nx,ny = x,y = center
13  sx=sy=1.0
14  if new_center:
15    (nx,ny) = new_center
16  if scale:
17    (sx,sy) = (scale, scale)
18  cosine = math.cos(angle)
19  sine = math.sin(angle)
20  a = cosine/sx
21  b = sine/sx
22  c = x-nx*a-ny*b
23  d = -sine/sy
24  e = cosine/sy
25  f = y-nx*d-ny*e
26  return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
27
28def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
29  # calculate offsets in original image
30  offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
31  offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
32  # get the direction
33  eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1])
34  # calc rotation angle in radians
35  rotation = -math.atan2(float(eye_direction[1]),float(eye_direction[0]))
36  # distance between them
37  dist = Distance(eye_left, eye_right)
38  # calculate the reference eye-width
39  reference = dest_sz[0] - 2.0*offset_h
40  # scale factor
41  scale = float(dist)/float(reference)
42  # rotate original around the left eye
43  image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
44  # crop the rotated image
45  crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v)
46  crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)
47  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])))
48  # resize it
49  image = image.resize(dest_sz, Image.ANTIALIAS)
50  return image
51
52def readFileNames():
53    try:
54        inFile = open('E:\\Project_Face\\Projects\\image_data.csv')
55    except:
56        raise IOError('There is no file named path_to_created_csv_file.csv in current directory.')
57        return False
58
59    picPath = []
60    picIndex = []
61    for line in inFile.readlines():
62        if line != '':
63
64            fields = line.rstrip().split(';')
65            picPath.append(fields[0])
66            picIndex.append(int(fields[1]))
67
68    return (picPath, picIndex)
69
70
71if __name__ == "__main__":
72  [images, indexes]=readFileNames()
73if not os.path.exists("modified"):
74    os.makedirs("modified")
75for img in images:
76    image =  Image.open(img)
77    CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.1,0.1), dest_sz=(200,200)).save(".\\modified\\"+img.rstrip().split('\\')[5]+"_10_10_200_200.jpg")
78    CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.2,0.2), dest_sz=(200,200)).save(".\\modified\\"+img.rstrip().split('\\')[5]+"_20_20_200_200.jpg")
79    CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.3,0.3), dest_sz=(200,200)).save(".\\modified\\"+img.rstrip().split('\\')[5]+"_30_30_200_200.jpg")
80    CropFace(image, eye_left=(182,264), eye_right=(304,265), offset_pct=(0.2,0.2)).save("_20_20_70_70.jpg").save(".\\modified\\"+img.rstrip().split('\\')[5]+"_20_20_70_70.jpg")
81