Mercurial > hg > MPIWGWeb
comparison scaleTest.py @ 135:dafb1110bfbe
scaletest
author | dwinter |
---|---|
date | Fri, 31 May 2013 12:57:12 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
134:9f45ed6ffeab | 135:dafb1110bfbe |
---|---|
1 from PIL import Image; | |
2 from cStringIO import StringIO; | |
3 import ImageFilter | |
4 | |
5 import urllib | |
6 import logging | |
7 | |
8 | |
9 url="http://localhost:18080/www/projects/DEPT1_710Schemmel-Space/Gruppe_Schemmel.jpg" | |
10 ul = urllib.urlopen(url) | |
11 datStringIO = StringIO(ul.read()); | |
12 | |
13 | |
14 try: | |
15 pilImg = Image.open(datStringIO) | |
16 | |
17 | |
18 except: | |
19 logging.error("scale image input:"+self.getId()) | |
20 | |
21 | |
22 w,h = pilImg.size | |
23 logging.debug("oldsize: %s %s"%(w,h)) | |
24 targetW=120. | |
25 targetH=75. | |
26 | |
27 | |
28 facW=targetW/w | |
29 | |
30 h1 = int(h*facW) | |
31 | |
32 if h1<targetH: #bild zu klein in h | |
33 facH=targetH/h | |
34 | |
35 w1=int(w*facH) | |
36 | |
37 fs = min(max(int(1/facH)+1,3),10) | |
38 #fs = 5 | |
39 logging.debug(fs) | |
40 print facH | |
41 if (1/facH) > 2: | |
42 pilImg = pilImg.filter(ImageFilter.BLUR) | |
43 | |
44 try: | |
45 pilImg = pilImg.filter(ImageFilter.MaxFilter(fs)) | |
46 except: | |
47 pilImg = pilImg.filter(ImageFilter.MaxFilter(fs-1)) | |
48 | |
49 logging.debug("Xnew size: %s %s"%(w1,targetH)) | |
50 res = pilImg.resize((w1,int(targetH)),Image.ANTIALIAS); | |
51 | |
52 | |
53 else: | |
54 fs = min(max(int(1/facW)+1,3),10) | |
55 #fs=5 | |
56 logging.debug(fs) | |
57 | |
58 print facW | |
59 if (1/facW) > 2: | |
60 pilImg = pilImg.filter(ImageFilter.BLUR) | |
61 | |
62 try: | |
63 pilImg = pilImg.filter(ImageFilter.MaxFilter(fs)) | |
64 except: | |
65 | |
66 | |
67 try: | |
68 pilImg = pilImg.filter(ImageFilter.MaxFilter(fs-1)) | |
69 except: | |
70 pass | |
71 logging.debug("ynew size: %s %s"%(targetW,h1)) | |
72 res = pilImg.resize((int(targetW),h1)) | |
73 | |
74 nw,nh = res.size | |
75 | |
76 cutW1=int(nw/2-(targetW/2)) | |
77 cutW2=int(nw/2+(targetW/2)) | |
78 | |
79 cutH1=int(nh/2-(targetH/2)) | |
80 cutH2=int(nh/2+(targetH/2)) | |
81 | |
82 | |
83 | |
84 res2 = res.crop((cutW1,cutH1,cutW2,cutH2)) | |
85 | |
86 outS = StringIO() | |
87 | |
88 res2.save("/tmp/test.jpg") | |
89 |