Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
ots-blog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ots
ots-blog
Commits
82c1c1b1
Commit
82c1c1b1
authored
1 year ago
by
James Vasile
Browse files
Options
Downloads
Patches
Plain Diff
Add helper for making image sets
parent
cd086452
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bin/img-srcset
+61
-0
61 additions, 0 deletions
bin/img-srcset
requirements-dev.txt
+1
-0
1 addition, 0 deletions
requirements-dev.txt
requirements.txt
+1
-0
1 addition, 0 deletions
requirements.txt
with
63 additions
and
0 deletions
bin/img-srcset
0 → 100755
+
61
−
0
View file @
82c1c1b1
#!/usr/bin/env python3
"""
Generate srcset images so we can do something like:
<img src=
"
img.png
"
width=
"
4000
"
height=
"
3000
"
alt=
""
srcset=
"
img.png 4000w,
img-2000x1500.png 2000w,
img-1024x768.png 1024w,
img-768x576.png 768w,
img-300.png 225w
"
sizes =
"
100vw
"
>
"""
import
os
from
PIL
import
Image
import
sys
def
get_html_dir
(
curr
:
str
=
""
,
token
:
str
=
"
content
"
):
curr
=
curr
or
os
.
getcwd
()
ret
=
[]
while
curr
!=
'
/
'
:
curr
,
child
=
os
.
path
.
split
(
curr
)
if
child
==
token
:
break
ret
.
append
(
child
)
ret
.
reverse
()
return
'
/
'
+
'
/
'
.
join
(
ret
)
while
current_dir
!=
'
/
'
:
dir_name
=
os
.
path
.
basename
(
current_dir
)
if
target_string
in
dir_name
:
break
current_dir
=
os
.
path
.
dirname
(
current_dir
)
in_fname
=
sys
.
argv
[
1
]
html_dir
=
get_html_dir
()
fname_parts
=
os
.
path
.
splitext
(
in_fname
)
image
=
Image
.
open
(
in_fname
)
widths
=
(
4000
,
2000
,
1024
,
768
,
300
)
## Write files of different widths
print
(
f
'
<img src=
"
{
html_dir
}
/
{
in_fname
}
"'
)
print
(
f
'
width=
"
{
image
.
width
}
"
height=
"
{
image
.
height
}
"
alt=
""'
)
print
(
f
'
srcset=
"
{
html_dir
}
/
{
in_fname
}
{
image
.
width
}
w,
'
)
for
width
in
widths
:
if
width
>=
image
.
width
:
continue
height
=
int
((
width
*
image
.
height
)
/
image
.
width
)
resized_image
=
image
.
resize
((
width
,
height
))
## Write resized image
out_fname
=
f
"
{
fname_parts
[
0
]
}
-
{
width
}
x
{
height
}{
fname_parts
[
1
]
}
"
resized_image
.
save
(
out_fname
)
## Print html snippet
sys
.
stdout
.
write
(
f
'
{
html_dir
}
/
{
out_fname
}
{
width
}
w
'
)
if
width
==
widths
[
-
1
]:
print
(
'"
'
)
else
:
print
(
'
,
'
)
print
(
'
sizes =
"
100vw
"
>
'
)
This diff is collapsed.
Click to expand it.
requirements-dev.txt
+
1
−
0
View file @
82c1c1b1
...
...
@@ -3,3 +3,4 @@ psutil
bs4
pytest-xprocess
feedparser
types-Pillow
This diff is collapsed.
Click to expand it.
requirements.txt
+
1
−
0
View file @
82c1c1b1
pelican
[markdown]
pelican-search
Pillow
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment