-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresize.py
53 lines (51 loc) · 1.26 KB
/
resize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
import sys, ntpath
from PIL import Image, ImageEnhance
fic_l = open(sys.argv[1],'rt')
liste=fic_l.readlines()
fic_l.close()
nbim = 0
nbimnr = 0
for fic in liste:
nbim += 1
fic = fic[:-1]
try:
im = Image.open(fic)
except:
nbimnr +=1
print ("Traitement de " + ntpath.basename(fic) + " -> ignoré (format non reconnu)")
continue
print ("Traitement de " + ntpath.basename(fic))
if im._getexif()!=None:
o = im._getexif().get(274)
else:
o = 0
if o in (1,2):
im_rotated = im
elif o in (3, 4):
im_rotated = im.transpose(Image.ROTATE_180)
elif o in (5, 6):
im_rotated = im.transpose(Image.ROTATE_270)
elif o in (7, 8):
im_rotated = im.transpose(Image.ROTATE_90)
else:
im_rotated = im
w = im_rotated.width
h = im_rotated.height
if w>=h:
h = round(150 / w * h)
w = 150
else:
w = round(150 / h * w)
h = 150
im_resized=im_rotated.resize((w,h),Image.LANCZOS)
im_final = ImageEnhance.Sharpness(im_resized).enhance(1.2)
im_final.save(sys.argv[2] + " - " + ntpath.splitext(ntpath.basename(fic))[0] + ".jpg","JPEG",quality=85)
im.close()
if nbimnr == 0:
print (str(nbim) + " image(s) traitée(s)")
exit(0)
else:
print (str(nbim) + " image(s) traitée(s) dont")
print (" " + str(nbimnr) + " image(s) non reconnue(s)")
exit(1)