Jelajahi Sumber

docs: add bytes, dict & PIL to embed image docs
Also slightly reformatted the examples.

Mikael Koli 4 tahun lalu
induk
melakukan
b8d00ab6d9
1 mengubah file dengan 106 tambahan dan 10 penghapusan
  1. 106 10
      docs/tutorials/body_content.rst

+ 106 - 10
docs/tutorials/body_content.rst

@@ -24,8 +24,9 @@ of the email:
     email.send(
     email.send(
         subject='An image',
         subject='An image',
         receivers=['first.last@example.com'],
         receivers=['first.last@example.com'],
-        html="""<h1>This is an image:</h1> 
-                {{ my_image }}
+        html="""
+            <h1>This is an image:</h1> 
+            {{ my_image }}
         """,
         """,
         body_images={
         body_images={
             'my_image': 'path/to/image.png', 
             'my_image': 'path/to/image.png', 
@@ -46,7 +47,10 @@ you can also create the ``img`` tag yourself:
     email.send(
     email.send(
         subject='An image',
         subject='An image',
         receivers=['first.last@example.com'],
         receivers=['first.last@example.com'],
-        html='<h1>This is an image:</h1> <img src="{{ my_image.src }}" width=500 height=350>',
+        html="""
+            <h1>This is an image:</h1> 
+            <img src="{{ my_image.src }}" width=500 height=350>
+        """,
         body_images={
         body_images={
             'my_image': 'path/to/image.png', 
             'my_image': 'path/to/image.png', 
         }
         }
@@ -54,12 +58,78 @@ you can also create the ``img`` tag yourself:
 
 
 In addition to paths as strings, the following are supported:
 In addition to paths as strings, the following are supported:
 
 
-- ``pathlib.Path``
-- ``bytes`` (the image as raw bytes)
-- ``matplotlib.pyplot.Figure``
-- ``PIL.Image``
+- pathlib.Path
+- :ref:`bytes (the image as raw bytes) <embedding-images-bytes>`
+- :ref:`matplotlib.pyplot.Figure <embedding-images-plt>`
+- :ref:`PIL.Image (Pillow image) <embedding-images-pil>`
+- :ref:`dict (content as bytes and specify the type) <embedding-images-dict>`
+
+.. _embedding-images-bytes:
+
+Embedding Image from bytes
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+You may also pass the image as bytes:
+
+.. code-block:: python
+
+    import base64
+
+    # data of a simple PNG image 
+    data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
+    data_as_bytes = base64.b64decode(data_as_base64)
+
+    gmail.send(
+        subject='An image',
+        receivers=['first.last@example.com'],
+        html="""
+            <h1>This is an image:</h1> 
+            {{ myimage }}
+        """,
+        body_images={
+            'myimage': data_as_bytes
+        },
+    )
+
+.. note::
+
+    The bytes are expected to represent a PNG image. In case your image is in 
+    other format (ie. JPEG), you should specify the image using the 
+    :ref:`dict format <embedding-images-dict>`
+
+.. _embedding-images-dict:
+
+Embedding Image with dict format
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+You may also include images using the dict format:
+
+.. code-block:: python
+
+    import base64
 
 
-.. _embedding-plt:
+    # data of a simple PNG image 
+    data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
+    data_as_bytes = base64.b64decode(data_as_base64)
+
+    gmail.send(
+        subject='An image',
+        receivers=['first.last@example.com'],
+        html="""
+            <h1>This is an image:</h1> 
+            {{ myimage }}
+        """,
+        body_images={
+            'myimage': { 
+                'myimage': data_as_bytes,
+                'subtype': 'png'
+            }
+        }
+    )
+
+This enables more control than including bytes as you may specify the ``subtype`` of the image. 
+
+.. _embedding-images-plt:
 
 
 Embedding Figure
 Embedding Figure
 ^^^^^^^^^^^^^^^^
 ^^^^^^^^^^^^^^^^
@@ -80,8 +150,9 @@ A simple example to include a figure:
     email.send(
     email.send(
         subject='A plot',
         subject='A plot',
         receivers=['first.last@example.com'],
         receivers=['first.last@example.com'],
-        html="""<h1>This is a plot:</h1> 
-                {{ my_plot }}
+        html="""
+            <h1>This is a plot:</h1> 
+            {{ my_plot }}
         """,
         """,
         body_images={
         body_images={
             'my_plot': fig, 
             'my_plot': fig, 
@@ -93,6 +164,31 @@ The outcome looks like this:
 .. image:: /imgs/email_emb_plt.png
 .. image:: /imgs/email_emb_plt.png
     :align: center
     :align: center
 
 
+.. _embedding-images-pil:
+
+Embedding Pillow Image
+^^^^^^^^^^^^^^^^^^^^^^
+
+You may also include Pillow image:
+
+.. code-block:: python
+
+    # Create a simple image
+    from PIL.Image import Image
+    img = Image.new('RGB', (100, 30), color = (73, 109, 137))
+
+    # Send the plot
+    email.send(
+        subject='A PIL image',
+        receivers=['first.last@example.com'],
+        html="""
+            <h1>This is a Pillow image:</h1> 
+            {{ my_image }}
+        """,
+        body_images={
+            'my_image': img, 
+        }
+    )
 
 
 .. _embedding-tables:
 .. _embedding-tables: