During the first weekend of the semester, I got inspired to do some Python development. Out of the inspiration came the latest feature of the Image Titler took: logo insertion. In this article, we’ll take a look at what that means and how it was implemented.
Table of Contents
What Is Logo Insertion?
As far as names are concerned, logo insertion isn’t a formal term. It’s just what I’ve been calling the latest feature of the Image Titler script. Now, in addition to being able to add titles and borders to image, you can also insert your logo:
Notice how there’s now an image of my logo in the lower left corner. How cool is that?
Personally, I haven’t tried other logos, but I imagine you’ll get similar performance as long as it’s square. Otherwise, it’ll shrink one way or the other.
In any case, you can add a logo of your own with the following code:
pip install image-titler # in case you haven't already image_titler -l "path/to/logo"
That will open up a prompt for you to select an image. Then, the tool will attempt to parse the file name (i.e. assumes
- separators) and place it as a title on the image. Finally, the logo will be positioned in the lower left corner, and the image will be saved wherever you wrote this command.
How Does Logo Insertion Work?
Under the hood, we take advantage of the PIL library in Python. Essentially, we load two images: the background and the foreground. Then, we paste one onto the other:
def draw_logo(img: Image, logo_path: str): """ Adds a logo to the image if a path is provided. :param img: an image to be modified :param logo_path: the path to a logo :return: nothing """ if logo_path: logo = Image.open(logo_path, "r") logo.thumbnail(LOGO_SIZE) width, height = img.size img.paste(logo, (LOGO_PADDING, height - LOGO_SIZE - LOGO_PADDING), logo)
As you can see, I wrote a
draw_logo() function which takes a background image and a path to a logo. Then, if the logo path is present, we load the logo, convert it to a thumbnail of a particular size, and paste it onto the background image.
Here, the real magic is in the last line. In other words, we can the
paste() function which takes a few inputs: the image file, its location, and a mask. In this case, we use the logo, and we calculate where to paste it using the height of the logo and the height of the background. Since we don’t want it perfectly in the corner, we add some padding.
If you’re interested in seeing the entire script, you can check it out here. Keep in mind that it’s a little rough around the edges, but it gets the job done. Feel free to fork it and modify it to your needs.
Any Other Changes?
While I was putting this feature together, I realized that there was this strange artifact showing up in my output images. Let me show you what I mean:
Since I put the Image Titler tool together, I’d been generating images that look exactly like this one. If you don’t look to hard, there’s nothing to see. It’s just a regular featured image.
However, if you zoom in on the text, you’ll notice some strange artifacts. For instance, the red isn’t exactly solid—there are some darker shades mixed in. Overall, the image looks rather poor.
For awhile, I chalked this up to WordPress. After all, the previews that Image Titler generates are always extremely clean and clear. Unfortunately, that’s not the same quality you get when you save the image. Apparently, the save function does some compression which can actually be toggled off:
edited_image.save(storage_path, subsampling=0, quality=100)
Here, I set the quality to max and subsampling to zero. Now, images in v1.5.1 look exactly how I expect them to:
Of course, WordPress still does some minor distortion. If you want to compare the two raw images, try right clicking and selecting “open image in new tab.”
Outside of this change, I also updated the dimensions of the image in v1.5.2, so they conform more to my thumbnails:
Now, you’ll be able to see both the title and the logo on the archive pages of the website. How cool is that?!
At this point, I’m fairly satisfied with where the Image Titler tool is at. That said, there are a few outstanding issues I’d like to deal with at some point. In addition, I’m open to suggestions. Is there anything you’d like to see?
Otherwise, thanks for stopping by! If you’re interested in getting updates like this in your inbox, hop on my mailing list. I publish twice a week, and I always send both posts out in a Friday newsletter. In addition, you can support this project and others like it by becoming a patron or sponsor me directly on GitHub.
Here are a few related posts for your perusal:
Thanks for taking some time to check out this update! I appreciate the support.
Chiropractors get a bad rap, but I find their YouTube content somewhat enjoyable. That said, they absolutely destroyed my YouTube recommendations.
Python is a fun language to learn on its own, but what if we could learn it by doing something even more fun like making Discord bots?