1
1
"""Nbconvert preprocessor for the python-markdown nbextension."""
2
2
3
3
from nbconvert .preprocessors import Preprocessor
4
- from traitlets import Bool , Float , Unicode
4
+ from traitlets import Bool , Unicode
5
5
import re
6
6
import os
7
7
import base64
15
15
16
16
class EmbedImagesPreprocessor (Preprocessor ):
17
17
"""
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.
19
20
20
21
This :class:`~nbconvert.preprocessors.Preprocessor` replaces kernel code in
21
22
markdown cells with the results stored in the cell metadata.
@@ -35,21 +36,26 @@ class EmbedImagesPreprocessor(Preprocessor):
35
36
36
37
EmbedImagesPreprocessor.embed_remote_images=True
37
38
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
40
42
41
43
EmbedImagesPreprocessor.resize=small
42
44
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.
47
51
48
52
Example::
49
53
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
51
56
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
53
59
"""
54
60
55
61
embed_images = Bool (False , help = "Embed images as attachment" ).tag (config = True )
@@ -80,7 +86,7 @@ def replfunc_md(self, match):
80
86
81
87
# resize settings: small -> 500px, mid -> 1000px, large -> 200px
82
88
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' ]:
84
90
from io import BytesIO
85
91
try :
86
92
from PIL import Image
@@ -90,21 +96,21 @@ def replfunc_md(self, match):
90
96
if Image :
91
97
# Only make images smaller when rescaling
92
98
im = Image .open (BytesIO (data ))
93
- size = im .size
94
- factor = imgsizes [self .resize ]/ max (size )
99
+ factor = imgsizes [self .resize ] / max (im .size )
95
100
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 ))
97
102
newim = im .resize (newsize )
98
103
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' ))
100
106
data = fp .getvalue ()
101
107
fp .close ()
102
108
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 ]))
104
110
105
111
self .log .debug ("embedding url: %s, format: %s" % (url , imgformat ))
106
112
b64_data = base64 .b64encode (data ).decode ("utf-8" )
107
- self .attachments [url ] = { 'image/' + imgformat : b64_data }
113
+ self .attachments [url ] = {'image/' + imgformat : b64_data }
108
114
109
115
newimg = ' + ')'
110
116
return newimg
0 commit comments