Skip to content

Commit 897bbd5

Browse files
committed
Init simple script, Ready to use
1 parent 98dca26 commit 897bbd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+163
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.DS_Store
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Simple Batch Breakpoint generator python"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"This notebook script will easy generate responsive image for Frontend website, maintain aspect ratio, no stretch.\n",
15+
"Normally I have to generate it via https://www.responsivebreakpoints.com/ which quite slow for file by file.\n",
16+
"So I create some Vue component with fixed Breakpoint size [200,763,1132,1400] in other site production (not in this project) feel free to modify the code "
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"## what it do ?"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"Specify the folder path that have all the image (right now is all .jpg) and breakpoint width and run script and done.\n"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"## Import Library"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 18,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"import os, sys\n",
47+
"from PIL import Image\n",
48+
"import math \n",
49+
"import glob "
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"## Function"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 25,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"def resize_maintain_aspect_by_width(PIL_Image,new_width):\n",
66+
" '''\n",
67+
" This function get image and resize by width (maintain aspect ratio) without strech\n",
68+
" '''\n",
69+
" width, height = PIL_Image.size\n",
70+
" # if new width is greater than original width, return the image\n",
71+
" if new_width > width:\n",
72+
" return PIL_Image\n",
73+
" \n",
74+
" new_height = math.floor(new_width * height / width )\n",
75+
" return PIL_Image.resize((new_width, new_height), Image.ANTIALIAS)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 26,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"def savebreakpoint(filename,PIL_Image,breakpoint):\n",
85+
" '''\n",
86+
" This function save PIL image by add postfix from specify breakpoint\n",
87+
" '''\n",
88+
" path = filename.rsplit('.', 1)\n",
89+
" path[0] = path[0]+'_'+str(breakpoint)\n",
90+
" PIL_Image.save(r'.'.join(path))"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"metadata": {},
96+
"source": [
97+
"## Edit here"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 27,
103+
"metadata": {},
104+
"outputs": [],
105+
"source": [
106+
"# set breakpoints width\n",
107+
"breakpoints = [200,763,1132,1400]\n",
108+
"# set the folder path\n",
109+
"files = glob.glob('./testbatch/**/*.jpg',recursive = True) "
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"## Run"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 28,
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"for file in files:\n",
126+
" im = Image.open(file) \n",
127+
" for breakpoint in breakpoints:\n",
128+
" tmp = resize_maintain_aspect_by_width(im,breakpoint)\n",
129+
" savebreakpoint(file,tmp,breakpoint) "
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"### Output: Sample.jpg to Sample_200.jpg, Sample_763.jpg, Sample_1132.jpg, Sample_1400.jpg"
137+
]
138+
}
139+
],
140+
"metadata": {
141+
"kernelspec": {
142+
"display_name": "Python 3",
143+
"language": "python",
144+
"name": "python3"
145+
},
146+
"language_info": {
147+
"codemirror_mode": {
148+
"name": "ipython",
149+
"version": 3
150+
},
151+
"file_extension": ".py",
152+
"mimetype": "text/x-python",
153+
"name": "python",
154+
"nbconvert_exporter": "python",
155+
"pygments_lexer": "ipython3",
156+
"version": "3.8.3"
157+
}
158+
},
159+
"nbformat": 4,
160+
"nbformat_minor": 4
161+
}

testbatch/IMG_20200101_125629_1.jpg

129 KB
96.1 KB
96.1 KB
8.15 KB
62.4 KB

testbatch/IMG_20200501_164051.jpg

408 KB
110 KB
161 KB

testbatch/IMG_20200501_164051_200.jpg

5.84 KB

testbatch/IMG_20200501_164051_763.jpg

54.2 KB

testbatch/IMG_20200501_164214.jpg

331 KB
81.7 KB
122 KB

testbatch/IMG_20200501_164214_200.jpg

3.74 KB

testbatch/IMG_20200501_164214_763.jpg

37.9 KB

testbatch/IMG_20200501_165954.jpg

28.2 KB
16.1 KB
16.1 KB

testbatch/IMG_20200501_165954_200.jpg

7.11 KB

testbatch/IMG_20200501_165954_763.jpg

16.1 KB

testbatch/IMG_20200501_175122.jpg

19.6 KB
11.2 KB
11.2 KB

testbatch/IMG_20200501_175122_200.jpg

5.47 KB

testbatch/IMG_20200501_175122_763.jpg

11.2 KB

testbatch/IMG_20200502_140241.jpg

465 KB
131 KB
188 KB

testbatch/IMG_20200502_140241_200.jpg

7.02 KB

testbatch/IMG_20200502_140241_763.jpg

65.9 KB

testbatch/IMG_20200507_150129.jpg

121 KB
84.7 KB
84.7 KB

testbatch/IMG_20200507_150129_200.jpg

6 KB

testbatch/IMG_20200507_150129_763.jpg

48.1 KB

testbatch/IMG_20200627_164006.jpg

278 KB
85.4 KB
122 KB

testbatch/IMG_20200627_164006_200.jpg

5.43 KB

testbatch/IMG_20200627_164006_763.jpg

43.4 KB

testbatch/IMG_20200708_204821.jpg

235 KB
81.8 KB
112 KB

testbatch/IMG_20200708_204821_200.jpg

6.28 KB

testbatch/IMG_20200708_204821_763.jpg

45.3 KB

testbatch/IMG_20200710_193428.jpg

287 KB
101 KB
140 KB

testbatch/IMG_20200710_193428_200.jpg

7.13 KB

testbatch/IMG_20200710_193428_763.jpg

54.6 KB

0 commit comments

Comments
 (0)