@@ -15,7 +15,7 @@ Specify the folder path that have all the image (recursive) and breakpoint width
15
15
16
16
``` python
17
17
import os, sys
18
- from PIL import Image
18
+ from PIL import Image, ImageFilter
19
19
import math
20
20
import glob
21
21
```
@@ -35,17 +35,33 @@ def resize_maintain_aspect_by_width(PIL_Image,new_width):
35
35
36
36
new_height = math.floor(new_width * height / width )
37
37
return PIL_Image .resize((new_width, new_height), Image.ANTIALIAS )
38
- ```
39
38
39
+ def apply_GaussianBlur (image ,radius = 20 ):
40
+ '''
41
+ Apply GaussianBlur with default rad = 10 to image
42
+ '''
43
+ return image.filter(ImageFilter.GaussianBlur(radius = radius))
40
44
41
- ``` python
42
45
def savebreakpoint (filename ,PIL_Image ,breakpoint ,formatter = " %s _%s " ):
43
46
'''
44
47
This function save PIL image by add postfix from specify breakpoint
45
48
'''
46
49
path = filename.rsplit(' .' , 1 )
47
50
path[0 ] = formatter % (path[0 ] , breakpoint )
48
51
PIL_Image .save(r ' . ' .join(path))
52
+
53
+ def generate_image_breakpoints (index ,file_path ,image ,formatter ,breakpoint ,blur = True ):
54
+ '''
55
+ This is function helper for generate breakpoints.
56
+ '''
57
+ # if it's a lowest-resolution image and generate a lazyload blurred image
58
+ if (index== 0 and blur== True ):
59
+ tmp = resize_maintain_aspect_by_width(image,breakpoint )
60
+ tmp = apply_GaussianBlur(tmp)
61
+ savebreakpoint(file_path,tmp," blur" ,formatter)
62
+
63
+ tmp = resize_maintain_aspect_by_width(image,breakpoint )
64
+ savebreakpoint(file_path,tmp,breakpoint ,formatter)
49
65
```
50
66
51
67
## Config
@@ -71,9 +87,8 @@ formatter="%s_%s"
71
87
``` python
72
88
for file in files:
73
89
im = Image.open(file )
74
- for breakpoint in breakpoints:
75
- tmp = resize_maintain_aspect_by_width(im,breakpoint )
76
- savebreakpoint(file ,tmp,breakpoint ,formatter)
90
+ for index,breakpoint in enumerate (breakpoints):
91
+ generate_image_breakpoints(index,file ,im,formatter,breakpoint ,blur = True )
77
92
```
78
93
79
94
### Output: Sample.jpg to Sample_200.jpg, Sample_763.jpg, Sample_1132.jpg, Sample_1400.jpg
@@ -89,7 +104,7 @@ from matplotlib import pyplot as plt
89
104
90
105
def _plot (_type ,fp ):
91
106
tmp = Image.open(fp)
92
- plt.xlabel( " {0} : Size In Bytes:- {1:20, } kB" .format( _type,len (tmp.fp.read()) ))
107
+ plt.xlabel( " {0} : Size {1:0,.2f } kB" .format( _type,len (tmp.fp.read())/ 1000 ))
93
108
plt.imshow(Image.open(fp))
94
109
plt.grid(False )
95
110
plt.xticks([])
@@ -101,12 +116,14 @@ def show_preview():
101
116
_plot(" Original" ,' ./testbatch\\ IMG_20200101_125629_1.jpg' )
102
117
plt.figure(figsize = (20 ,20 ))
103
118
plt.subplot(2 ,3 ,1 )
104
- _plot(" Width 200" ,' ./testbatch\\ IMG_20200101_125629_1_200 .jpg' )
119
+ _plot(" Generated a lazyload blurred image, Width 200" ,' ./testbatch\\ IMG_20200101_125629_1_blur .jpg' )
105
120
plt.subplot(2 ,3 ,2 )
106
- _plot(" Width 763 " ,' ./testbatch\\ IMG_20200101_125629_1_763 .jpg' )
121
+ _plot(" Width 200 " ,' ./testbatch\\ IMG_20200101_125629_1_200 .jpg' )
107
122
plt.subplot(2 ,3 ,3 )
108
- _plot(" Width 1132 " ,' ./testbatch\\ IMG_20200101_125629_1_1132 .jpg' )
123
+ _plot(" Width 763 " ,' ./testbatch\\ IMG_20200101_125629_1_763 .jpg' )
109
124
plt.subplot(2 ,3 ,4 )
125
+ _plot(" Width 1132" ,' ./testbatch\\ IMG_20200101_125629_1_1132.jpg' )
126
+ plt.subplot(2 ,3 ,5 )
110
127
_plot(" Width 1400" ,' ./testbatch\\ IMG_20200101_125629_1_1400.jpg' )
111
128
112
129
```
@@ -119,11 +136,11 @@ show_preview()
119
136
```
120
137
121
138
122
- ![ png] ( output_17_0 .png)
139
+ ![ png] ( output_16_0 .png)
123
140
124
141
125
142
126
- ![ png] ( output_17_1 .png)
143
+ ![ png] ( output_16_1 .png)
127
144
128
145
129
146
0 commit comments