Generate Post With Img for Hugo

Generate Post With Img for Hugo
https://www.midjourney.com/home/

Some days ago I find a new post from a blog I follow about “Open Graph Meta Tags on Hugo and WordPress Blogs ” as a toot

Naty S
@[email protected]

New

➡️ burgeonlab.com/blog/hugo-and-w

I often see bloggers on Mastodon with no images for their blog links or sometimes no link card at all (this was me not too long ago).

I've been sorting out Open Graph meta tags for my three blogs (Hugo and WordPress) and I'm happy to say it's all working great now! Read my post to get some tips! 👆


3:18 PM · 8 Jul 2025

and I find I do the same thing and some more. I generate a default cover because I prefer to have a “standard” generated image or a specific image as the feature image. The image set in the settings is a default, a “if all other image fall use this” kind of stuff.

How I generate the post?

When I write a post I launch a makefile command and it launch two command for me:

  1. Generate a new post with hugo new
  2. Generate a cover with my python script

Some code

For this project I use a font (Futura Book font ) and a image where put the title and other thing of the post (for now only the title). This is the image “clean”, without the text.

cover-blank.jpg
from PIL import Image, ImageDraw, ImageFont  # 👉️ Import modules from PIL


def generate_img(message: str, path: str):
    font_path = "Futura Book font.ttf"  # 👉️ Font .ttf Path
    font_size = 100  # 👉️ Font Size
    img = Image.open("cover-blank.jpg")  # 👉️ Open Image
    dr = ImageDraw.Draw(img)  # 👉️ Create New Image
    my_font = ImageFont.truetype(font_path, font_size)  # 👉️ Initialize Font
    text_x = (img.width) // 2
    text_y = (img.height) // 2
    dr.text((text_x, text_y), message, font=my_font, fill=(255, 255, 255), anchor="mm")
    print("Generated content/" + path + "/cover.png")
    img.save("content/" + path + "/cover.png")

This script use Python (the library is PIL) and I set the fill parameters for a standard size OpenGraph image.

If you use a python script for generate new post you can implement this code inside of the script or you can add some code to make a command line to launch after your “new post” command.

Written by a human

Part of the series: Hugo Tricks
  1. Hugo With Lazy Loading and Webp
  2. WebP and Avif With Hugo's Static Site Generator
  3. How I implement Indieweb, Webmention and H Entry in My Blog
  4. More Stuff I Do With Webmention, Micropub and Brid.gy
  5. New Render Image For Hugo
  6. Add Photo Page in your Hugo Site
  7. Github Action for Syndication Links
  8. Generate Post With Img for Hugo
  9. Hugo Blog With Jupyter Notebook
Reference this post

Please reference this post with a link to this page. I prefer to be called Fundor333 (he/him) or Fundor333' Blog.

Learn more

Comments

Reply with a Webmention or a reply on the Fediverse.

With an account on the Fediverse or Mastodon you can reply to the original post.

See also