{"id":14645,"date":"2024-03-15T09:40:52","date_gmt":"2024-03-15T09:40:52","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/"},"modified":"2025-08-06T11:31:06","modified_gmt":"2025-08-06T11:31:06","slug":"how-to-create-dynamic-images-with-python","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/","title":{"rendered":"Python Dynamic Images: Create GIFs with Code"},"content":{"rendered":"<p>To create dynamic images, Python&#8217;s PIL (also known as Pillow) library and OpenCV library can be used to manipulate images.<\/p>\n<p>Here is an example code using PIL library to create dynamic images.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image, ImageSequence\r\n\r\n<span class=\"hljs-comment\"># \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u52a8\u6001\u56fe\u7247<\/span>\r\nnew_image = Image.new(<span class=\"hljs-string\">\"RGBA\"<\/span>, (<span class=\"hljs-number\">500<\/span>, <span class=\"hljs-number\">500<\/span>), (<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>))\r\n\r\n<span class=\"hljs-comment\"># \u6253\u5f00\u591a\u4e2a\u9759\u6001\u56fe\u7247\u6587\u4ef6<\/span>\r\nimage_files = [<span class=\"hljs-string\">\"image1.png\"<\/span>, <span class=\"hljs-string\">\"image2.png\"<\/span>, <span class=\"hljs-string\">\"image3.png\"<\/span>]\r\nframes = []\r\n<span class=\"hljs-keyword\">for<\/span> file <span class=\"hljs-keyword\">in<\/span> image_files:\r\n    image = Image.<span class=\"hljs-built_in\">open<\/span>(file)\r\n    frames.append(image)\r\n\r\n<span class=\"hljs-comment\"># \u5c06\u591a\u4e2a\u9759\u6001\u56fe\u7247\u4f9d\u6b21\u6dfb\u52a0\u5230\u52a8\u6001\u56fe\u7247\u4e2d<\/span>\r\nnew_image.save(<span class=\"hljs-string\">\"animated_image.gif\"<\/span>, save_all=<span class=\"hljs-literal\">True<\/span>, append_images=frames, loop=<span class=\"hljs-number\">0<\/span>, duration=<span class=\"hljs-number\">500<\/span>)\r\n<\/code><\/pre>\n<p>This code segment begins by using the Image.new() function to create a new dynamic image, specifying the dimensions and background color of the image.<\/p>\n<p>Then, multiple static image files are opened using the Image.open() function, creating a collection of image frames called frames.<\/p>\n<p>Finally, calling the new_image.save() function will sequentially add multiple static images to the dynamic image, and save it as a single dynamic image file. The parameters save_all=True indicates saving all frames, append_images=frames means adding all frames to the dynamic image, loop=0 represents looping the playback, and duration=500 indicates each frame will last for 500 milliseconds.<\/p>\n<p>It&#8217;s important to note that when saving as a dynamic image format, you can choose the GIF format (with the suffix .gif) or other formats that support dynamic images, such as APNG (with the suffix .png).<\/p>\n<p>Additionally, if you need to perform more complex operations on animated images, you can use the OpenCV library to read, process, and save animated images.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create dynamic images, Python&#8217;s PIL (also known as Pillow) library and OpenCV library can be used to manipulate images. Here is an example code using PIL library to create dynamic images. from PIL import Image, ImageSequence # \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u52a8\u6001\u56fe\u7247 new_image = Image.new(&#8220;RGBA&#8221;, (500, 500), (0, 0, 0, 0)) # \u6253\u5f00\u591a\u4e2a\u9759\u6001\u56fe\u7247\u6587\u4ef6 image_files = [&#8220;image1.png&#8221;, &#8220;image2.png&#8221;, &#8220;image3.png&#8221;] [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[19775,3158,8863,9130,72],"class_list":["post-14645","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-gif-creation","tag-image-manipulation","tag-opencv","tag-pillow","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Dynamic Images: Create GIFs with Code - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn to create dynamic images (GIFs) in Python using PIL\/Pillow. Step-by-step code tutorial included.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Dynamic Images: Create GIFs with Code\" \/>\n<meta property=\"og:description\" content=\"Learn to create dynamic images (GIFs) in Python using PIL\/Pillow. Step-by-step code tutorial included.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T09:40:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T11:31:06+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"Python Dynamic Images: Create GIFs with Code\",\"datePublished\":\"2024-03-15T09:40:52+00:00\",\"dateModified\":\"2025-08-06T11:31:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\"},\"wordCount\":207,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"gif creation\",\"Image manipulation\",\"OpenCV\",\"Pillow\",\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\",\"name\":\"Python Dynamic Images: Create GIFs with Code - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T09:40:52+00:00\",\"dateModified\":\"2025-08-06T11:31:06+00:00\",\"description\":\"Learn to create dynamic images (GIFs) in Python using PIL\/Pillow. Step-by-step code tutorial included.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Dynamic Images: Create GIFs with Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Dynamic Images: Create GIFs with Code - Blog - Silicon Cloud","description":"Learn to create dynamic images (GIFs) in Python using PIL\/Pillow. Step-by-step code tutorial included.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/","og_locale":"en_US","og_type":"article","og_title":"Python Dynamic Images: Create GIFs with Code","og_description":"Learn to create dynamic images (GIFs) in Python using PIL\/Pillow. Step-by-step code tutorial included.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T09:40:52+00:00","article_modified_time":"2025-08-06T11:31:06+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"Python Dynamic Images: Create GIFs with Code","datePublished":"2024-03-15T09:40:52+00:00","dateModified":"2025-08-06T11:31:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/"},"wordCount":207,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["gif creation","Image manipulation","OpenCV","Pillow","Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/","name":"Python Dynamic Images: Create GIFs with Code - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T09:40:52+00:00","dateModified":"2025-08-06T11:31:06+00:00","description":"Learn to create dynamic images (GIFs) in Python using PIL\/Pillow. Step-by-step code tutorial included.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-dynamic-images-with-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Dynamic Images: Create GIFs with Code"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=14645"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14645\/revisions"}],"predecessor-version":[{"id":158682,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14645\/revisions\/158682"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=14645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=14645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=14645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}