1

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?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
DuK
  • 111
  • 4
  • 3
    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 – Nasser Jul 14 '23 at 21:52
  • 1
    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. – Nasser Jul 14 '23 at 21:56
  • 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 – DuK Jul 14 '23 at 23:02
  • 4
    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. – Nasser Jul 14 '23 at 23:14
  • 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? – DuK Jul 14 '23 at 23:17
  • it sounds like you did not export from Mathematica the gif file as animated gif file, but as a normal gif file. Loading an animated gif file into browser should automatically plays it unless you added some specific option not to do that. sorry, that is all I can help with for now since I have no idea what you did and did not do. – Nasser Jul 14 '23 at 23:22
  • try to add some options and see it – DuK Jul 14 '23 at 23:28
  • Ummm... make a copy, source for your conversion, so you preserve the original? – David G. Stork Jul 14 '23 at 23:29
  • the original dont change but the new change – DuK Jul 14 '23 at 23:48

1 Answers1

2

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=&quot;en&quot;> <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.

Ben Izd
  • 9,229
  • 1
  • 14
  • 45