avatarShinichi Okada

Summary

The website provides a Bash script called cliptext for creating header images with text clipped from an image, aimed at enhancing programming articles.

Abstract

The cliptext Bash script is designed to generate distinctive header images for programming articles by clipping text onto a background image. The script utilizes wkhtmltopdf/wkhtmltoimage to render the HTML and CSS into an image format. It offers various customization options such as text, font, size, output image name, background image, width, height, and font weight. The script is available for installation via the Awesome package manager or direct download from GitHub. It is intended to help authors create unique header images for their articles, differentiating them from others that often use the same images.

Opinions

  • The author is inspired by a text effect seen in a YouTube video and prefers using wkhtmltopdf/wkhtmltoimage over other methods for its reasonable performance.
  • The author has experienced limitations with other tools, such as Firefox's screenshot feature not supporting CSS color: transparent.
  • wkhtmltopdf/wkhtmltoimage is noted to use an older version of WebKit, which may not support advanced CSS and JavaScript.
  • The script uses the getoptions library for its option parser, emphasizing its portability and compatibility with all POSIX shells.
  • The author encourages the use of the script to create stand-out header images and suggests that using unique images can help articles stand out.
  • A membership option is promoted for full access to stories on Medium, indicating the author's support for the platform and its content creators.

Bash Script to Clip an Image to Text

Create a stand-out article header image for your programming article

The output from the command, cliptext -t “Bash script to clip an image to text” -w 1400 -e 900

Introduction

I like the text effect you see in this video. It creates a header image like the above and decided to make a Bash script for it. The video shows you how to clip an image to text using HTML and CSS. With my Bash script, you can create it from the terminal. I am going to use these images for my future articles with different background images and text.

Requirement

You need to install wkhtmltopdf/wkhtmltoimage. wkhtmltopdf/wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF/image formats using the Qt WebKit rendering engine.

I tried different methods, but I found wkhtmltopdf/wkhtmltoimage works reasonably well comparing to other options. Please note that wkhtmltopdf/wkhtmltoimageuse an old version of WebKit for its rendering engine, and it simply doesn’t support advanced CSS and Javascript.

I gave up using Firefox screenshot since it can’t handle CSS color: transparent.

Installation

Using Awesome package manager:

$ awesome install shinokada/cliptext

Or you can DOWNLOAD IT.

Options

I used Getoptions for the Shell script option parser. It supports all POSIX shells, no limitations, no bashisms. It has high portability and supports all platforms.

OPTIONS                          DESCRIPTION
  -t,     --text TEXT              -t "Your Text to print"
  -f,     --font FONT              -f "Font Name"
  -s,     --size SIZE              -s 240  will set CSS font-size: 240px
  -i,     --image BKIMG            -i background-image.jpg
  -o,     --output OUTPUT          -o output-file-name
  -w,     --width WIDTH            -w 1240. The default width is 1920px.
  -h,     --height HEIGHT          -h 800. The default height is 1080px.
  -b,     --bold WEIGHT            -b 900. This sets the font weight. Choose from 500|700|900.
  -h,     --help                   
          --version

How to use cliptext

  • The default text is “It’s Friday” and you can set your text using the -t option.
  • The default font is Luckiest Guy and you can change it by using the -f option. Select a font from Google font.
  • The default font-size is 240px and you can change it by using the -s option.
  • The default output image name is myimg.jpg and you can change it by using the -o option.
  • The default background image is flower.jpg and you can change it by using the -i option.
  • The default width is 1920px and you can change it by using the -w option.
  • The default height is 1080 and you can change it by using the -h option.
  • Some fonts have different weights. You can set it by using the -b option and choose from 500, 700, or 900.
$ cliptext -t Cliptext -i ./forest.jpeg -s 140 -w 800 -h 300

The above command results in the image below.

Bash script

Conclusion

Feel free to use it for your articles. Nowadays many articles use the same header images. If you want to stand out from the crowd, this script may help you.

Get full access to every story on Medium by becoming a member.

https://blog.codewithshin.com/subscribe

References

Bash Script
Command Line
Clip
Programming
Terminal Commands
Recommended from ReadMedium