I know this is quite an old question which already has a lot of answers, yet I still spent hours trying to actually do what I wanted:
Using Chrome headless (version 74.0.3729.157 as of this response), it is actually easy:
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless --screenshot --window-size=256,256 --default-background-color=0 button.html
Explanation of the command:
--headless
runs Chrome without opening it and exits after the command completes--screenshot
will capture a screenshot (note that it generates a file called screenshot.png
in the folder where the command is run)--window-size
allow to only capture a portion of the screen (format is --window-size=width,height
)--default-background-color=0
is the magic trick that tells Chrome to use a transparent background, not the default white color