HTML import to Word -> inline image dimensions ignored

Viewed 168

When importing an HTML file to Microsoft Word, inline image dimensions are ignored. For example, the following HTML code:

<html>
<body>
<div>
here's an image 50px in height: 
<img height=50 src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT4AAACfCAMAAABX0UX9AAAAMFBMVEUMETYmKD8AADFlZnCQkZcAACwkJj6Xl5wWGTU5O0oFDDUAACUeIDqpqaxfYGtvcHd51QkgAAABV0lEQVR4nO3QwQGCMAAAsaIIKqj7bysz9L7JCBn7c2HS9hrrYzDpvekrFn2FvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvkRfoi/Rl+hL9CX6En2JvuTqO27MOsfne2fW7w+KdA3sU/NzBAAAAABJRU5ErkJggg==">
</div>
</body>
</html>

looks like this in a browser:

enter image description here

but, looks like this in Word (note the huge size relative to the 12-pt font):

enter image description here

How do I get the image in Word to scale to the correct size? Please note:

  • Adding style="height:50px;width:100px;max-width:100px;max-height:50px;" to the img tag has no effect on the Word result
  • The source image must be inline (src="data:... and cannot be saved in a separate file)
  • Reducing the dimensions of the encoded data is not and option, because the resolution becomes unacceptably low (it won't matter for the image provided in this example, but it will matter a lot for the actual images this is to be used for)
1 Answers

What finally got me a fix was to set the image's spatial resolution (DPI, dots per inch). This was done directly inside the PNG file, before base64-encoding it.

When generating the image output in Python with Pillow, I changed

img.save(buf, format="PNG")

to

img.save(buf, format="PNG", dpi=(200,200))

Word respected that, at least for PNG images, which is also your case. It may either be ignoring other attributes or just prioritizing the file's DPI setting.

Related