IT Cooking

IT Stuff and Silly Shell

Spread the love

 

Meme Generator online are legion, and they do not offer the same options. Most will limit the size or shape of your meme, and most will apply their signature stamp on it. Plus it takes time to upload the image, refresh the page etc.

There is a solution for you though, and pretty easy believe me! It’s based on ImageMagick, with a simple drag&drop batch script for Windows. This solution will let you create classy memes even your mother would be proud of!

For the hasty, here is the Windows code for creating a classic meme with top and bottom text, font Impact white with blurred shadow:

Windows Batch

set input=file.jpg
set output=file-meme.jpg

FOR /F "tokens=* USEBACKQ" %%s IN (`magick identify -format "%%[fx:w]x%%[fx:h]" %input%`) DO SET SIZE=%%s
magick convert %input% ^
  -gravity north ^
  ( -size %SIZE% xc:none -font Impact -pointsize 50 -stroke black -strokewidth 7 -annotate 0 "top custom text" -blur 0x1  ) -composite ^
  -font Impact -pointsize 50 -fill white -stroke none      -annotate 0 "top custom text" ^
  -gravity south ^
  ( -size %SIZE% xc:none -font Impact -pointsize 50 -stroke black -strokewidth 7 -annotate 0 "bottom custom text" -blur 0x1  ) ^
  -font Impact -pointsize 50 -fill white -stroke none      -annotate 0 "bottom custom text" -composite ^
  %output%

Needless to say that the font size need to be adapted for the size of your picture. -pointsize 50 is ok for picture sizes between 600-800.

This is the batch version. To test this on command line, replace any double %% by %

 

Linux bash

file=file.jpg
output=file-meme.jpg

SIZE=$(magick identify -format "%[fx:w]x%[fx:h]" $file)
magick convert $file \
  -gravity north \
  ( -size $SIZE xc:none -font Impact -pointsize 50 -stroke black -strokewidth 7 -annotate 0 "top custom text" -blur 0x1  ) -composite \
  -font Impact -pointsize 50 -fill white -stroke none      -annotate 0 "top custom text" \
  -gravity south \
  ( -size $SIZE xc:none -font Impact -pointsize 50 -stroke black -strokewidth 7 -annotate 0 "bottom custom text" -blur 0x1  ) \
  -font Impact -pointsize 50 -fill white -stroke none      -annotate 0 "bottom custom text" -composite \
  $output

I didn’t test it but I’m 100% sure it works.

 

How to Automate the process in Windows Explorer

Let’s go further and create a real meme generator with a batch that you can use to drop files on. This totally automates the creation process. The batch script is there. Here is how it works:

First, drag the file you want over the batch (or create a right-click event):

Second, answer the questions in the script:

And voila, the generated image will open auto-magically before your very eyes.

History log

The script includes a history log so it will recall your last top and bottom text for your convenience :mrgreen:

Effects

This script also includes blur and sharpen filters for your background image. They are cumulative.

 

Tools needed

Download these portable tools and extract them in a folder that will be accessed by your scripts. Even better, add the path to your utilities in your %PATH% variable.

For ImageMagick you don’t need the full content (237MB). Extract only these files:

  • magick.exe
  • magic.xml
  • type.xml

 

ImageMagick is used to create, edit, compose, convert bitmap images, resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. . It can read and write images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, TIFF, DPX, EXR, WebP, Postscript, PDF, and SVG.

 

OptiPNG is a PNG optimizer that compress image files to a smaller size, without losing any information. This program also converts external lossless formats (BMP, GIF, PNM and TIFF) to optimized PNG, and also performs PNG integrity checks and corrections. It has to be called at the end.

 

GNU Utilities for Win32 are so important, it’s the missing part of the Windows command line that I hate. I don’t want to run a VM just to have the bash command line, and Cygwin is out of the question. I want native Windows commands that replicate the set of Unix utilities and this package is a good starter.

 

pngQuant is a command-line utility and a library for lossy compression of PNG images. The conversion reduces file sizes significantly (often as much as 70%) and preserves full alpha transparency. Generated images are compatible with all web browsers and operating systems.

pngQuant

I personally use pngQuant within a script on all my post images so they weight between 10k-30k each. It’s best suited for screenshots though, for the results can remind you of the GIF era when you try it on real pictures.

 

Windows Batch Script to Automate Meme Generation

This meme generator batch is for Windows, but it’s easily adaptable for Linux. Linux guys usually know how to translate this but I may answer in the comments if you really need it.

@echo OFF
::replace S:\wintools\multimedia by your path with magick, pngquant and optipng
set PATH=%PATH%;S:\wintools\multimedia
pushd %~dp1

::JPEG quality 75
set QUALITY=-quality 75
set output=jpg
set /p output=output [%output%]? 

::You can certainly combine blur with sharpen and any other filter you can think of for crazy results
set /p blurBackground=Blur Background [N/y]? 
:: set the blur level here: 0x1 to 0x6 heavy blur / single digit for soft blur http://www.imagemagick.org/script/command-line-options.php#blur
if "x%blurBackground%x" EQU "xyx" set BLUR=-blur 0x2

set /p sharpenBackground=Sharpen Background [N/y]? 
:: sharpen level 0x6 gives an old school, badass look to the image http://www.imagemagick.org/script/command-line-options.php#sharpen
if "x%sharpenBackground%x" EQU "xyx" set SHARPEN=-sharpen 6

if exist meme-history-top.log for /f "tokens=*" %%t in ('tail -1 meme-history-top.log') do set top=%%t
set /p top=top [%top%]? 
if "x%top%x" NEQ "xx" (echo %top%)>>meme-history-top.log

if exist meme-history-bottom.log for /f "tokens=*" %%b in ('tail -1 meme-history-bottom.log') do set bottom=%%b
set /p bottom=bottom [%bottom%]? 
if "x%bottom%x" NEQ "xx" (echo %bottom%)>>meme-history-bottom.log

FOR /F "tokens=* USEBACKQ" %%s IN (`magick identify -format "%%[fx:w]x%%[fx:h]" %1`) DO SET SIZE=%%s

magick convert %1 %BLUR% %SHARPEN% ^
  -gravity north ^
  ( -size %SIZE% xc:none -font Impact -pointsize 50 -stroke black -strokewidth 7 -annotate 0 "%top%" -blur 0x1  ) -composite ^
  -font Impact -pointsize 50 -fill white -stroke none      -annotate 0 "%top%" ^
  -gravity south ^
  ( -size %SIZE% xc:none -font Impact -pointsize 50 -stroke black -strokewidth 7 -annotate 0 "%bottom%" -blur 0x1  ) ^
  -font Impact -pointsize 50 -fill white -stroke none      -annotate 0 "%bottom%" -composite ^
  %QUALITY% %~n1-meme.%output%
::

:: insert pngquant reduction command here

::png optimization of the output
if "%output%" EQU "png" (
  optipng %~dpn1-meme.png
)

::launch the file in your default viewer
%~dpn1-meme.%output%

You can say thank you :mrgreen:


Spread the love
Posted In: scripting, Image Processing Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *