Generate Post With Img for Hugo

Fundor333 | | Reading time 2 minutes | Word count 377

######


Card image cap

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

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! ๐Ÿ‘†


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.

This post is part of the Hugo Tricks series
  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



Comments

To reply to this post, you can send a Webmention or you can toot me at [email protected]

With an account on the Fediverse or Mastodon, you can respond to this post. Since Mastodon is decentralized, you can use your existing account hosted by another Mastodon server or compatible platform if you don't have an account on this one. Known non-private replies are displayed below.

Learn how this is implemented here.


See Also