Skip to content

Commit be9eff3

Browse files
authored
Update README.md
1 parent 7755298 commit be9eff3

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Specify the folder path that have all the image (recursive) and breakpoint width
1515

1616
```python
1717
import os, sys
18-
from PIL import Image
18+
from PIL import Image, ImageFilter
1919
import math
2020
import glob
2121
```
@@ -35,17 +35,33 @@ def resize_maintain_aspect_by_width(PIL_Image,new_width):
3535

3636
new_height = math.floor(new_width * height / width )
3737
return PIL_Image.resize((new_width, new_height), Image.ANTIALIAS)
38-
```
3938

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))
4044

41-
```python
4245
def savebreakpoint(filename,PIL_Image,breakpoint,formatter="%s_%s"):
4346
'''
4447
This function save PIL image by add postfix from specify breakpoint
4548
'''
4649
path = filename.rsplit('.', 1)
4750
path[0] = formatter % (path[0] , breakpoint)
4851
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)
4965
```
5066

5167
## Config
@@ -71,9 +87,8 @@ formatter="%s_%s"
7187
```python
7288
for file in files:
7389
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)
7792
```
7893

7994
### 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
89104

90105
def _plot(_type,fp):
91106
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 ))
93108
plt.imshow(Image.open(fp))
94109
plt.grid(False)
95110
plt.xticks([])
@@ -101,12 +116,14 @@ def show_preview():
101116
_plot("Original",'./testbatch\\IMG_20200101_125629_1.jpg')
102117
plt.figure(figsize=(20,20))
103118
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')
105120
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')
107122
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')
109124
plt.subplot(2,3,4)
125+
_plot("Width 1132",'./testbatch\\IMG_20200101_125629_1_1132.jpg')
126+
plt.subplot(2,3,5)
110127
_plot("Width 1400",'./testbatch\\IMG_20200101_125629_1_1400.jpg')
111128

112129
```
@@ -119,11 +136,11 @@ show_preview()
119136
```
120137

121138

122-
![png](output_17_0.png)
139+
![png](output_16_0.png)
123140

124141

125142

126-
![png](output_17_1.png)
143+
![png](output_16_1.png)
127144

128145

129146

output_16_0.png

249 KB
Loading

output_16_1.png

514 KB
Loading

0 commit comments

Comments
 (0)