Sphinx doc: Process image directive to create HTML with base64 image data URI - python-sphinx

When Sphinx compiles an RST file with image directive, it results in a HTML that has tag where the "src" attribute points to the image file.
Sample RST file
..image:: path/image.jpg
Resulting HTML
<img src='path/image.jpg'>
This means that the image is not contained within the HTML. The image file is a separate document from the HTML.
How can image directive be processed such that the resulting HTML has the tag with 'src' attribute containing base64 encoded image data as shown below
<img width="64" height="69" alt="Treehouse Logo" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABF...">
In the above example, the image data is embedded into the HTML. How can this be achieved?

Related

src tag for pdf in Thymeleaf

I'm working in Thymeleaf, I uploaded image using base64, but can't upload pdf documents using base64.
I'm wondering what will be the src value of pdf like images.
I already used image in Thymeleaf with base64 like this...
<img th:src="${'data:image/jpg;base64,' + image.profileImg}" alt="">
But I don't understand how to accept pdf src in base64. I tried like this
<iframe th:src="${'data:file/pdf;base64,' + myfile.profilePdf}" height="200" width="300"></iframe>
But it doesn't work...
Try this:
<iframe src="data:file/pdf;base64,myfile.profilePdf" height="200%" width="300%"></iframe>

thymeleaf html tag with img file extension wild card

I have a thymeleaf html tag that displays an image but the image could be a jpg or it could be a png.
Example
<img th:src="'http://localhost:8081/'+#{'/images/'}+${image}+'.jpg'"/>
I need to substitute the '.jpg' for a wild card extension so as to display either jpg or png
does anyone know what that piece of code at the end is?
Thanks
I don't think it is possible. Thymeleaf has no way to detect what is the type of the file and the browser need the full path of the file to show it.
I would advice to detect/add the file extension at the same place your set the ${image} value.
For example by converting the "image" variable to an object with "name" and "extension" fields
<img th:src="'http://localhost:8081/'+#{'/images/'}+${image.name}+${image.extension}"/>

Insert inline images in README.md

Is it possible to insert an inline image (base64 encoded) via HTML or Markdown in README.md (GitHub)?
For example, this doesn't work:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
Such a possibility exists:
![image](data:image/png;base64,<BASE64_HERE>)
But I would rather add an image to the project, similar to the README.MD itself, and only refer to the image that is part of the project.
More info: https://github.com/YourUserAccount/YourProject/blob/master/DirectoryPath/ReadMe.md

Converts html to docx with pandoc

I would like to convert my html report to docx. I put 2 images side by side on my docx page.
For my first try, I just use this html below and I can achieve what I need:
<div>
<img width="256px" height="256px" src="imga1.png"/>
<!-- I use this to simulate margin-right -->
<span> </span><span> </span><span> </span><span> 
</span>
<img width="256px" height="256px" src="image2.png"/>
<div>
Now I would like to add a header for each image and that creates complication.
As an assumption, my first text header will not exceed 256px (because it is the image's size). And 256px corresponds to 16 "&emsp".
Then I compute my text header image by using a canvas 2d context method in javascript (text metrics).
After that I substract 256px by my computed text width and compute the number of "&emsp" that I need to fit the first image.
With this method, I can put my header for my 2 images but it is not efficient because my text second header image is not always aligned to the second image.
I am looking methods to put my image header all aligned to images.

How can I insert images in lektor markdown sections?

How can I insert an image in a markdown section in lektor. In particular, does the url filter work inside markdown, or who else to I reference the image location inside assets/static/?
Most of the time in the admin site, you want to embed a file listed on the "Attachments" list on the left. To do that, simply use the attachment filename, as in :
![alt text](myimage.png)
Use the standard markdown for the inserting images. For the image named my-image.jpg stored in the assets/static/ use the following markdown code:
<!-- 1) Inserts an image with an empty alt parameter, but better to use the 2nd or the 3rd example -->
![](/static/my-image.jpg)
<!-- 2) Inserts an image also with the text "fancy picture" as the alt parameter value -->
![fancy picture](/static/my-image.jpg)
<!-- 3) Image also has the title attribute -->
![fancy picture](/static/my-image.jpg "Title of the fancy picture")
The snippet above will generate resulting HTML:
<img src="../static/my-image.jpg" alt="">
<img src="../static/my-image.jpg" alt="fancy picture">
<img src="../static/my-image.jpg" alt="fancy picture" title="Title of the fancy picture">
I tested this snippet on my example website and it works perfectly.
Please note the / before static in the markdown code.

Resources