Convert HTML and CSS to a Static GIF Image
Aspose.HTML for Python via .NET
converts an HTML file, string, document, or URL to GIF with
Converter.convert_html()
and
ImageSaveOptions
. Select ImageFormat.GIF explicitly because PNG is the default image format.
GIF is suitable for static graphics that work within a limited color palette. HTML to GIF conversion captures the current HTML and CSS in a raster image.
Test HTML to GIF and View the Python Code
Upload an HTML file or enter a webpage URL, then keep GIF selected to create a static image. The widget also provides Python code for the chosen output format.
{{#if_output 'PDF' 'DOCX' 'XPS' 'MHTML' 'MD' }}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
{{/if_output}}
{{#if_output 'XHTML'}}
import aspose.html as ah
import aspose.html.saving as sav
{{/if_output}}
with ah.HTMLDocument("{{input lower}}") as document:
{{#if_output 'PDF'}}
options = sav.PdfSaveOptions()
{{/if_output}}
{{#if_output 'DOCX'}}
options = sav.DocSaveOptions()
{{/if_output}}
{{#if_output 'XPS'}}
options = sav.XpsSaveOptions()
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP'}}
options = sav.ImageSaveOptions(rim.ImageFormat.{{output param2 upper}})
{{/if_output}}
{{#if_output 'MHTML'}}
options = sav.MHTMLSaveOptions()
{{/if_output}}
{{#if_output 'MD'}}
options = sav.MarkdownSaveOptions.default
{{/if_output}}
{{#if_output 'BMP' 'JPEG' 'GIF' 'PNG' 'TIFF' 'WEBP' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
conv.Converter.convert_html(document, options, "output.{{output lower}}")
{{/if_output}}
{{#if_output 'XHTML'}}
options = sav.HTMLSaveOptions()
options.document_type = sav.HTMLSaveOptions.XHTML
document.save("output.xhtml", options)
{{/if_output}}
For GIF output, the generated example opens the selected HTML source, passes ImageFormat.GIF to ImageSaveOptions, and writes output.gif. Keep linked stylesheets, images, and fonts accessible while the document is loaded.
Python Code to Convert HTML to GIF
import aspose.html as ah
import aspose.html.converters as conv
import aspose.html.rendering.image as rim
import aspose.html.saving as sav
with ah.HTMLDocument("document.html") as document:
options = sav.ImageSaveOptions(rim.ImageFormat.GIF)
conv.Converter.convert_html(document, options, "output.gif")
How to Convert an HTML Document to GIF
- Install the
aspose-html-netpackage. - Load an HTML file or webpage URL into
HTMLDocument. Supply a base URI when HTML text contains relative resource paths. - Create
ImageSaveOptionsand selectImageFormat.GIF; otherwise the image options default to PNG. - Set the page dimensions, resolution, and background for the intended GIF output.
- Pass the document, GIF options, and output path to
Converter.convert_html().
Configure GIF Page Size and Output
GIF does not use a separate GIF-specific save-options class. Configure the output through
ImageSaveOptions
:
- Output format: Pass
ImageFormat.GIFand use the.giffilename extension. The default image format is PNG. - Page dimensions: Set the output page size and margins. A document that spans several output pages can produce several numbered GIF files.
- Resolution: Horizontal and vertical resolution affect pages defined with physical units and internal raster images. Increasing the resolution does not enlarge a page whose dimensions are already specified in pixels.
- Background: Choose a background that works with the limited transparency available in GIF. Use PNG when partial transparency is required.
- Image processing: Configure CSS media, antialiasing, and text hinting when the defaults do not match the required output.
- Relative resources: Use the HTML file location, webpage URL, or a base URI so stylesheets, background images, and fonts can be resolved.
- Remote files: Keep external resources accessible until the document has been loaded and converted.
- Loaded page state: Content revealed only by later user interaction may not appear in the static GIF.
See Convert HTML to Image in Python for the complete image conversion workflow and shared settings.
Install Aspose.HTML for Python via .NET
Install the aspose-html-net package from
PyPI
.
Install Aspose.HTML for Python via .NET
pip install aspose-html-net
Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.
HTML to GIF FAQ
1. Does HTML to GIF conversion create an animated GIF?
No. The conversion captures the current HTML and CSS as static GIF output. It does not record a sequence of browser animation frames.
2. Can I set GIF compression or image quality during HTML to GIF conversion?
The ImageSaveOptions class does not provide a GIF quality setting, and its compression property applies to TIFF output. For GIF, configure page dimensions, resolution, background, CSS, antialiasing, and text rendering instead.
3. Does HTML to GIF conversion in Python require a web browser?
No. Aspose.HTML processes the HTML through its own APIs. The Python application needs access to the source and its linked resources, but it does not need browser automation.
4. Is text selectable in an HTML to GIF image?
No. HTML to GIF conversion produces a raster image. Text, links, and form controls become pixels and do not remain selectable or interactive.
Related GIF and Image Conversion Guides
Compare GIF with other image outputs or return to all Python conversion guides.