Organised wardrobe

That /img Folder Just Keeps Growing!

Recently, I found that I had over 120 images in my static /img directory (where blog post images live). It’s been bugging me for a while, so last night I thought I’d better deal with it. The reason it’s an issue having all those loose files is:

  1. The likelihood of duplicates grows i.e. /img/screenshot19.png
  2. There’s no easy way of knowing which images belong to which post1
  3. It looks (and feels) messy

The best solution would be to get Pelican to just handle it for me. That didn’t seem to be an option (not easily anyway). Possibly, I could modify my content/posts structure to have one directory per post and have the images in the same directory as the post (that seems to be what some people do). That would have involved modifiying every post which I didn’t want to do as not all my posts have images.

  • Some have zero
  • Some have one
  • A few have multiple

So, I decided I needed another method. The one I settled on is:

/img/YYYY-MM-DD/image1.jpg

Here’s how my content/img looks now:

Directory Listing

So the screenshot above is served from:

https://simonh.uk/img/2024-12-24/new_folders.webp

If on 1st January 2025, I decide to write another post involving changing some other directory structure, it won’t be /img/new_folders1.webp Instead it’ll be /img/2025-01-01/new_folders.webp Much better.

By the way, I did consider using the blog post title as the folder name:

/img/organising-blog-post-images-in-pelican/

but that’s a lot of typing. Plus I’d need some way of organising by year at the very least anyway. By just using the date, I know that if a folder exists, it must have at least one image in it (otherwise it wouldn’t exist). I can also quickly and easily find which image directory is linked with each post (as the date is part of the URL). That there may be more than post associated with an image directory won’t be a problum as if there may be confusion, I can simply name the image file slightly better to avoid that confusion.

For example, let’s say I’m being very productive today (2024-12-24) and write five posts about the following topics:

  1. A recent holiday I had
  2. Some Linux program I’ve been using
  3. A book review
  4. A cheesecake recipe
  5. Why women have smaller feet than men2

I can just have holiday1.jpg, holiday2.jpg, linux1.jpg, book5.jpg, cheese1.jpg etc. saved in /img/2024-12-24/ This method will make it easier than ever to know what belongs to what.3

I spent about an hour and a half checking each post from 2020 to now, seeing which ones had images, altering the path from /img/image.webp to /img/YYYY-MM-DD/image.webp. Doing a live reload for each altered post to check that I hadn’t made any silly mistakes. I think everything is fine, both locally and on the live site. It was a bit of unwanted work, but from now on, I’ve just got to get used to typing an extra 11 characters to the path. I’m a fast typist so I think it’s worth it for the better organisation (I’ll automate it if it becomes annoying).

I generally don’t write more than one or two posts in a given day, so each dated directory shouldn’t have too many images (16 images is the most for one day, so far). And I believe that organising by date in the YYYY-MM-DD format gives natural sorting (oldest to newest). As I don’t plan on ever changing the URL format:

simonh.uk/YYYY/MM/DD/blog-post-title

I’ll never need to worry about image paths again! If in the future I decide to group by year as well (which I probably won’t):

/img/YYYY/YYYY-MM-DD/image.jpg

That’ll only apply from that year onwards. But, I don’t think that’s necessary unless I get to the tens of millions of files and directories.

What strategies have other Pelican users come up with to deal with this problem?

Footnotes

1 I was trying to include the key post idea as the first word of the image filename to keep them together conceptually, but now that’s not necessary.

2 This was just a random idea that popped into my head. There’s trillions of webpages on the subject, if you’re interested!

3 Another option is:

  • /img/2024-12-24/hol/
  • /img/2024-12-24/cheese/
  • /img/2024-12-24/book/

I haven’t felt the need for subdirectories within the date folder yet, but you may want to consider it if you have multiple posts, with multiple images sharing the same post date.