{"id":20764,"date":"2025-07-28T08:20:01","date_gmt":"2025-07-28T08:20:01","guid":{"rendered":"https:\/\/thedatascientist.com\/?p=54427"},"modified":"2025-07-28T08:20:01","modified_gmt":"2025-07-28T08:20:01","slug":"automating-meme-creation-from-news-headlines-using-python","status":"publish","type":"post","link":"https:\/\/python-bloggers.com\/2025\/07\/automating-meme-creation-from-news-headlines-using-python\/","title":{"rendered":"Automating Meme Creation from News Headlines Using Python"},"content":{"rendered":"<div style=\\\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\\\">\r\n<i>This article was first published on  <strong>\r\n<a href=\"https:\/\/thedatascientist.com\/automating-meme-creation-from-news-headlines-using-python\/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=automating-meme-creation-from-news-headlines-using-python\"> Technical Posts \u2013 The Data Scientist <\/a><\/strong>, and kindly contributed to <a href=\/about\/>python-bloggers<\/a>.  (You can report issue about the content on this page <a href=\/contact-us\/>here<\/a>)\r\n<br\/>Want to share your content on python-bloggers?<a href=\/add-your-blog\/> click here<\/a>.<\/i>\r\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h2>\n<p>In today&#8217;s digital world, memes have become a powerful tool for sharing information, humor, and social commentary. This blog post explores how we can automate meme creation using Python by fetching news headlines, downloading relevant images, generating witty captions, and overlaying the text on the images. We will use various libraries and APIs, including Google News, Pixabay, and Google&#8217;s Gemini AI, to make this process seamless and efficient.<\/p>\n<h2 class=\"wp-block-heading\"><strong>Overview&nbsp;<\/strong><\/h2>\n<p>The script follows a structured approach:<\/p>\n<ol class=\"wp-block-list\">\n<li><strong>Fetch news headlines<\/strong> using Google&#8217;s RSS feed.<\/li>\n<li><strong>Download images<\/strong> from Pixabay based on random categories.<\/li>\n<li><strong>Generate witty meme text<\/strong> using Gemini AI, taking into <a href=\"https:\/\/thedatascientist.com\/los-angeles-nonprofit-accounting-trusted-accountant\/\"  rel=\"noopener\" data-wpil-monitor-id=\"6026\">account<\/a> both the headline and the downloaded image.<\/li>\n<li><strong>Overlay text on the image<\/strong> to create a meme.<\/li>\n<li><strong>Save and display the final meme.<\/strong><\/li>\n<\/ol>\n<h2 class=\"wp-block-heading\"><strong>Required Libraries<\/strong><\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"578\" height=\"385\" src=\"https:\/\/i0.wp.com\/thedatascientist.com\/wp-content\/uploads\/2025\/07\/image-376.png?resize=578%2C385&#038;ssl=1\" alt=\"Required Libraries\" class=\"wp-image-54460\" srcset=\"https:\/\/thedatascientist.com\/wp-content\/uploads\/2025\/07\/image-376.png 800w, https:\/\/thedatascientist.com\/wp-content\/uploads\/2025\/07\/image-376-300x200.png 300w, https:\/\/thedatascientist.com\/wp-content\/uploads\/2025\/07\/image-376-768x512.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n<\/div>\n<p>Before running the script, ensure you have installed the necessary libraries:<\/p>\n<pre>pip install requests Pillow beautifulsoup4 google-generativeai<\/pre>\n<h2 class=\"wp-block-heading\"><strong>Step-by-Step Breakdown<\/strong><\/h2>\n<h3 class=\"wp-block-heading\"><strong>1. Fetching News Headlines<\/strong><\/h3>\n<p>We use Google&#8217;s RSS feed to fetch the latest news headlines:<\/p>\n<pre>import requests\n\nfrom bs4 import BeautifulSoup\n\nURL = 'https:\/\/news.google.com\/rss\/search?q=today%27s+news'\n\ndef fetch_headlines():\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"Fetch headlines from Google News and save them to a text file.\"\"\"\n\n&nbsp;&nbsp;&nbsp;&nbsp;response = requests.get(URL)\n\n&nbsp;&nbsp;&nbsp;&nbsp;if response.status_code == 200:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;soup = BeautifulSoup(response.content, 'xml')\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;items = soup.find_all('item', limit=5)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;headlines = &#91;item.title.text for item in items]\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with open('notepad.txt', 'w', encoding='utf-8') as file:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for headline in headlines:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.write(headline + 'n')\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return headlines\n\n&nbsp;&nbsp;&nbsp;&nbsp;else:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f'Failed to retrieve news. Status code: {response.status_code}')\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#91;]<\/pre>\n<p>This function fetches the top 5 headlines and saves them in a text file.<\/p>\n<h3 class=\"wp-block-heading\"><strong>2. Downloading Images from Pixabay<\/strong><\/h3>\n<p>We use the Pixabay API to fetch random images for meme creation:<\/p>\n<p>This function selects a random search category, downloads images from Pixabay, and extracts metadata tags as image descriptions.<\/p>\n<pre>import os\n\nimport random\n\nPIXABAY_API_KEY = ''&nbsp; # Add your API key here\n\nSEARCH_TERMS = &#91;'nature', 'technology', 'food', 'cities']\n\nDOWNLOAD_DIR = r' \u2019 #ADD PATH\n\nIMAGE_COUNT = 5\n\ndef download_pixabay_images():\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"Download random images from Pixabay and extract descriptions.\"\"\"\n\n&nbsp;&nbsp;&nbsp;&nbsp;search_term = random.choice(SEARCH_TERMS)\n\n&nbsp;&nbsp;&nbsp;&nbsp;url = f'https:\/\/pixabay.com\/api\/?key={PIXABAY_API_KEY}&amp;q={search_term}&amp;image_type=photo&amp;per_page={IMAGE_COUNT}'\n\n&nbsp;&nbsp;&nbsp;&nbsp;response = requests.get(url)\n\n&nbsp;&nbsp;&nbsp;&nbsp;if response.status_code == 200:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data = response.json()\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images = data&#91;'hits']\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;downloaded_images = &#91;]\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image_descriptions = &#91;]\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for img in images:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img_url = img&#91;'largeImageURL']\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img_tags = img&#91;'tags']&nbsp; # Use Pixabay's tags as an image description\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img_data = requests.get(img_url).content\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img_name = img_url.split('\/')&#91;-1]\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with open(os.path.join(DOWNLOAD_DIR, img_name), 'wb') as handler:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.write(img_data)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;downloaded_images.append(os.path.join(DOWNLOAD_DIR, img_name))\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image_descriptions.append(img_tags)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return downloaded_images, image_descriptions\n\n&nbsp;&nbsp;&nbsp;&nbsp;else:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f'Failed to retrieve images. Status code: {response.status_code}')\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#91;], &#91;]<\/pre>\n<h3 class=\"wp-block-heading\"><strong>3. Generating Meme Text with Gemini AI<\/strong><\/h3>\n<p>Google\u2019s Gemini API helps generate witty meme captions from news headlines while considering the image context:<\/p>\n<pre>import google.generativeai as genai\n\ngenai.configure(api_key='')&nbsp; # Add your Gemini API key here\n\ndef generate_content_with_gemini(headline, image_description):\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"Generate content using Gemini AI based on the headline and image description.\"\"\"\n\n&nbsp;&nbsp;&nbsp;&nbsp;model = genai.GenerativeModel(\"gemini-1.5-flash\")\n\n&nbsp;&nbsp;&nbsp;&nbsp;prompt = f\"Create a funny meme caption based on this news headline: '{headline}' and this image description: '{image_description}'\"\n\n&nbsp;&nbsp;&nbsp;&nbsp;response = model.generate_content(prompt)\n\n&nbsp;&nbsp;&nbsp;&nbsp;if response and hasattr(response, 'text'):\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return response.text.strip()\n\n&nbsp;&nbsp;&nbsp;&nbsp;else:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return \"Breaking News: AI refuses to joke!\"<\/pre>\n<p>This function ensures the meme text is customized using both the news headline and the associated image context.<\/p>\n<h3 class=\"wp-block-heading\"><strong>4. Overlaying Text on an Image<\/strong><\/h3>\n<p>We use the Pillow library to add text to the downloaded images:<\/p>\n<pre>from PIL import Image, ImageDraw, ImageFont\n\ndef create_meme(image_path, text):\n\n&nbsp;&nbsp;&nbsp;&nbsp;\"\"\"Overlay text on an image to create a meme.\"\"\"\n\n&nbsp;&nbsp;&nbsp;&nbsp;img = Image.open(image_path)\n\n&nbsp;&nbsp;&nbsp;&nbsp;draw = ImageDraw.Draw(img)\n\n&nbsp;&nbsp;&nbsp;&nbsp;try:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font = ImageFont.truetype(\"arial.ttf\", 40)\n\n&nbsp;&nbsp;&nbsp;&nbsp;except IOError:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font = ImageFont.load_default()\n\n&nbsp;&nbsp;&nbsp;&nbsp;text_position = (10, img.height - 70)\n\n&nbsp;&nbsp;&nbsp;&nbsp;draw.text(text_position, text, fill=\"white\", font=font)\n\n&nbsp;&nbsp;&nbsp;&nbsp;meme_image_path = os.path.join(DOWNLOAD_DIR, f'meme_{os.path.basename(image_path)}')\n\n&nbsp;&nbsp;&nbsp;&nbsp;img.save(meme_image_path)\n\n&nbsp;&nbsp;&nbsp;&nbsp;return meme_image_path<\/pre>\n<p>This function uses a readable font and ensures the text is properly placed on the image.<\/p>\n<h3 class=\"wp-block-heading\"><strong>5. Running the Script<\/strong><\/h3>\n<p>We now bring everything together in the main function:<\/p>\n<pre>def main():\n\nheadlines = fetch_headlines()\n\n&nbsp;&nbsp;&nbsp;&nbsp;if headlines:\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;downloaded_images, image_descriptions = download_pixabay_images()\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for headline, image_path, img_desc in zip(headlines, downloaded_images, image_descriptions):\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meme_text = generate_content_with_gemini(headline, img_desc)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create_meme(image_path, meme_text)\n\nif __name__ == '__main__':\n\n&nbsp;&nbsp;&nbsp;&nbsp;main()<\/pre>\n<p>The script now analyzes both the news headline and the image metadata to create a more relevant and engaging meme.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n<p>This Python project automates the process of creating memes from current news headlines, incorporating image context for more relevant and humorous results. It showcases how different APIs and libraries can be integrated to fetch data, process images, and generate text dynamically. This is a fun yet practical application of AI in <a href=\"https:\/\/thedatascientist.com\/ai-avatar-generator-future-of-video-content-creation-and-marketing-2\/\"  rel=\"noopener\" data-wpil-monitor-id=\"6028\">content creation<\/a>. It can be implemented to generate content for various <a href=\"https:\/\/thedatascientist.com\/how-social-media-affects-teenagers-what-parents-should-know\/\"  rel=\"noopener\" data-wpil-monitor-id=\"6027\">social media<\/a> platforms- like Instagram, YouTube etc.<\/p>\n<figure class=\"wp-block-table\">\n<table class=\"has-fixed-layout\">\n<tbody>\n<tr>\n<td colspan=\"2\"><strong>Hrishitva Patel <\/strong><a href=\"https:\/\/www.linkedin.com\/in\/hrishitva-patel\/\"  rel=\"noopener\"><strong>https:\/\/www.linkedin.com\/in\/hrishitva-patel\/<\/strong><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n<div style=\\\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\\\">\r\n<div style=\\\"text-align: center;\\\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/thedatascientist.com\/automating-meme-creation-from-news-headlines-using-python\/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=automating-meme-creation-from-news-headlines-using-python\"> Technical Posts \u2013 The Data Scientist <\/a><\/strong>.<\/div>\r\n<hr \/>\r\nWant to share your content on python-bloggers?<a href=\/add-your-blog\/ rel=\\\"nofollow\\\"> click here<\/a>.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width: 60%; display: inline-block; float:left; \"> Introduction In today\u2019s digital world, memes have become a powerful tool for sharing information, humor, and social commentary. This blog post explores how we can automate meme creation using Python by fetching news headlines, downloading relevant images, generating witty captions, and overlaying the text on the images. We will &#8230;<\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><img id=\"excerpts_images\" src=' https:\/\/thedatascientist.com\/wp-content\/uploads\/2025\/07\/image-376.png' width = \"200\"  style = \"padding: 10px;\" \/><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":105,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-20764","post","type-post","status-publish","format-standard","hentry","category-data-science"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/posts\/20764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/users\/105"}],"replies":[{"embeddable":true,"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/comments?post=20764"}],"version-history":[{"count":3,"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/posts\/20764\/revisions"}],"predecessor-version":[{"id":20779,"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/posts\/20764\/revisions\/20779"}],"wp:attachment":[{"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/media?parent=20764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/categories?post=20764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-bloggers.com\/wp-json\/wp\/v2\/tags?post=20764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}