1
$\begingroup$

I want to convert any .gif in HTML on Mathematica and keep the animation of the gif. For this I use this function:

Export["output.html", gif, "HTML"]

But after export, I lose all gif animation on HTML.

Have you some idea?

$\endgroup$
9
  • 3
    $\begingroup$ There are many questions about how to export to animated gif. Did you do any search? if you did, why nothing you found worked for you? what problems did you get? exporting-a-mathematica-animation-as-a-gif-and-and-controlling-the-animation-rat and manipulate-to-gif and exporting-a-dynamic-as-a-gif $\endgroup$
    – Nasser
    Jul 14, 2023 at 21:52
  • 1
    $\begingroup$ btw, I assume this is what you wanted. i.e. export from Mathematica to animated gif. But I had hard time parsing your question I want to export any .gif in HTLM on mathematica and keep the animation of the gif so you might want to rewrite this in a little bit more clear fashion. $\endgroup$
    – Nasser
    Jul 14, 2023 at 21:56
  • $\begingroup$ NO I know how to export from mathematica to animated gif. The problem is how to convert this .gif to html and keep the original animation of gif $\endgroup$
    – DuK
    Jul 14, 2023 at 23:02
  • 4
    $\begingroup$ convert this .gif to html I do not understand what you mean by converting a gif file to HTML. If you want to make a web page and put this gif file in it, then this is something you can do with a text editor. $\endgroup$
    – Nasser
    Jul 14, 2023 at 23:14
  • $\begingroup$ But why I Lose the animation of gif after convert it on html...??? Can I keep the original animation of gif? Use an other function than export? $\endgroup$
    – DuK
    Jul 14, 2023 at 23:17

1 Answer 1

2
$\begingroup$

It seems when you export AnimatedImage or a list of frames to HTML, Mathematica give you a notebook interface that does not animate, is slower (took 10 seconds to export on SSD), and generates more than one file.

If you want to just export a GIF into a single HTML file, we can use an HTML template like below:

ClearAll[ExportGIFToHTML];

ExportGIFToHTML[file_, frames_] := 
 Block[{dimensions = ImageDimensions @ First @ frames}, 
  Export[file, StringTemplate["<!DOCTYPE html>
<html lang=\"en\">
<head>
    <title>GIF</title>
</head>
<body>
    <img src='data:image/gif;base64,`image-data`' width='`width`' 
height='`height`'></img>
</body>
</html>"][<|
     "image-data" -> 
      ExportString[ExportString[frames, "GIF"], "Base64"], 
     "width" -> First @ dimensions, "height" -> Last @ dimensions|>], 
   "String"]
  ]

Let's generate a couple of frames:

frames = 
  Table[ListLinePlot[RandomInteger[5, 5], PerformanceGoal -> "Speed"],
    5];

Then call the function (Took less than a second):

ExportGIFToHTML["C:\\test.html", frames]

Output:

enter image description here

You can play with the Export options to change the animation setting.

$\endgroup$
1
  • 1
    $\begingroup$ (+1) Wonderful! $\endgroup$
    – cvgmt
    Jul 15, 2023 at 8:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.