Skip to content

Commit fd30aaf

Browse files
committed
Add image resizing option
1 parent 0d68281 commit fd30aaf

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/jupyter_contrib_nbextensions/nbconvert_support/pre_embedimages.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Nbconvert preprocessor for the python-markdown nbextension."""
22

33
from nbconvert.preprocessors import Preprocessor
4-
from traitlets import Bool, Float, Unicode
4+
from traitlets import Bool, Unicode
55
import re
66
import os
77
import base64
@@ -15,7 +15,8 @@
1515

1616
class EmbedImagesPreprocessor(Preprocessor):
1717
"""
18-
:mod:`nbconvert` Preprocessor to embed images in a markdown cell as attachment inside the notebook itself.
18+
:mod:`nbconvert` Preprocessor to embed images in a markdown cell as
19+
attachment inside the notebook itself.
1920
2021
This :class:`~nbconvert.preprocessors.Preprocessor` replaces kernel code in
2122
markdown cells with the results stored in the cell metadata.
@@ -35,21 +36,26 @@ class EmbedImagesPreprocessor(Preprocessor):
3536
3637
EmbedImagesPreprocessor.embed_remote_images=True
3738
38-
to additionally embeds all images referenced by an url (e.g. http://jupyter.org/assets/nav_logo.svg) instead
39-
of a local file name. Also
39+
to additionally embeds all images referenced by an url
40+
(e.g. http://jupyter.org/assets/nav_logo.svg) instead of a local file name.
41+
Also
4042
4143
EmbedImagesPreprocessor.resize=small
4244
43-
Let's you scale-down the size of an image. This is useful if you want to save space by not embedding large
44-
images and instead use a smaller (scaled) version. Works only for raster images (i.e. png, jpg).
45-
Valid resize settings are: small = 500px, mid = 1000px, large = 2000px for maximum size in length or width
46-
No upscaling of small images will be performed.
45+
Let's you scale-down the size of an image. This is useful if you want to
46+
save space by not embedding large images and instead use a smaller (scaled)
47+
version. Works only for raster images (i.e. png, jpg).
48+
Valid resize settings are: small = 500px, mid = 1000px, large = 2000px
49+
for maximum size in length or width. No upscaling of small images will
50+
be performed.
4751
4852
Example::
4953
50-
$ jupyter nbconvert --to html --EmbedImagesPreprocessor.embed_images=True --EmbedImagesPreprocessor.scale=large mynotebook.ipynb
54+
$ jupyter nbconvert --to html --EmbedImagesPreprocessor.embed_images=True
55+
--EmbedImagesPreprocessor.resize=large mynotebook.ipynb
5156
52-
*Note:* To embed images after conversion to HTML you can also use the `html_embed` exporter
57+
*Note:* To embed images after conversion to HTML you can also use the
58+
`html_embed` exporter
5359
"""
5460

5561
embed_images = Bool(False, help="Embed images as attachment").tag(config=True)
@@ -80,7 +86,7 @@ def replfunc_md(self, match):
8086

8187
# resize settings: small -> 500px, mid -> 1000px, large -> 200px
8288
imgsizes = {'small': 500, 'mid': 1000, 'large': 2000}
83-
if self.resize in imgsizes.keys() and imgformat in ['png', 'jpg']:
89+
if self.resize in imgsizes.keys() and imgformat in ['png', 'jpg']:
8490
from io import BytesIO
8591
try:
8692
from PIL import Image
@@ -90,21 +96,21 @@ def replfunc_md(self, match):
9096
if Image:
9197
# Only make images smaller when rescaling
9298
im = Image.open(BytesIO(data))
93-
size = im.size
94-
factor = imgsizes[self.resize]/max(size)
99+
factor = imgsizes[self.resize] / max(im.size)
95100
if factor < 1.0:
96-
newsize = ( int(size[0]*factor), int(size[1]*factor))
101+
newsize = (int(im.size[0] * factor), int(im.size[1] * factor))
97102
newim = im.resize(newsize)
98103
fp = BytesIO()
99-
newim.save(fp, format=imgformat.replace('jpg', 'jpeg')) # PIL requires JPEG instead of JPG
104+
# PIL requires JPEG instead of JPG
105+
newim.save(fp, format=imgformat.replace('jpg', 'jpeg'))
100106
data = fp.getvalue()
101107
fp.close()
102108
self.log.debug("Resized %d x %d image %s to size %d x %d pixels" %
103-
(size[0], size[1], url, newsize[0], newsize[1]) )
109+
(im.size[0], im.size[1], url, newsize[0], newsize[1]))
104110

105111
self.log.debug("embedding url: %s, format: %s" % (url, imgformat))
106112
b64_data = base64.b64encode(data).decode("utf-8")
107-
self.attachments[url] = { 'image/'+imgformat : b64_data }
113+
self.attachments[url] = {'image/' + imgformat: b64_data}
108114

109115
newimg = '![' + match.group(1) + '](attachment:' + match.group(2) + ')'
110116
return newimg

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ deps =
6363
flake8
6464
isort
6565
commands =
66-
flake8 src tests setup.py
66+
flake8 --max-line-length=120 src tests setup.py
6767
isort --verbose --check-only --diff --recursive src tests setup.py
6868

6969
[testenv:docs]

0 commit comments

Comments
 (0)