Transparency checkerboard with convert

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Swoops
Posts: 11
Joined: 2017-01-19T05:13:30-07:00
Authentication code: 1151

Transparency checkerboard with convert

Post by Swoops »

Hello, how do I get a checkerboard pattern instead of transparency when converting PNG to JPG?
I did manage to get at least something working with 'composite' instead of 'convert'. This results in grey background:

Code: Select all

composite -compose Dst_Over -tile pattern:checkerboard ./input.png ./output.jpg 
However I am really struggling to get the patttern to show in tiles, like in Photoshop, and to make it work with 'convert'. I know the docs says:
composite {overlay} {background} [{mask}] [-compose {method}] {result}
convert {background} {overlay} [{mask}] [-compose {method}] -composite {result}
So I came up with this:

Code: Select all

convert -tile pattern:checkerboard ./input.png -compose Dst_Over ./output.jpg 
This gives an error because it cannot find file '-tile', so the order must be wrong or something. Can anyone help me with this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparency checkerboard with convert

Post by fmw42 »

With convert -tile or tile: needs to know the size of the image.

Please always provide your IM version and OS/platform since syntax differs.

# create transparent image from IM internal logo: image

Code: Select all

convert logo: -transparent white logo_t.png
In Unix syntx:

IM 6
# get size of image

Code: Select all

wxh=`convert logo_t.png -format "%wx%h" info:`
# create checkerboard and overlay transparent image

Code: Select all

convert \
\( -size $wxh tile:pattern:checkerboard \) \
logo_t.png \
-compose over -composite result.jpg
IM 7
# do all in one convert command

Code: Select all

magick \
logo_t.png -set option:wxh "%wx%h" \
\( -size "%[wxh]" tile:pattern:checkerboard \) \
+swap -compose over -composite result.jpg
For Windows,
1) change \( ... \) to simply ( ... ) i.e., remove the \
2) change end of line \ to ^

See
http://www.imagemagick.org/Usage/canvas/#tile
http://www.imagemagick.org/Usage/windows/
Swoops
Posts: 11
Joined: 2017-01-19T05:13:30-07:00
Authentication code: 1151

Re: Transparency checkerboard with convert

Post by Swoops »

Hi Fred, thanks a lot for the examples and it works wonderful! Next time I will add the version and platform.
DenSEO
Posts: 2
Joined: 2019-07-25T04:28:18-07:00
Authentication code: 1152

Re: Transparency checkerboard with convert

Post by DenSEO »

Hello,

I trying to get checkerboard pattern instead of transparency when converting PNG to JPG. I need to convert and resize a lot of PNG files (900 directories with 1,000 files in each directory).
I am using Windows 7 with IM version 7

I have solution for single file:

Code: Select all

magick ^
	example.png -set option:wxh "%wx%h" ^
	( -size "%[wxh]" tile:pattern:checkerboard -brightness-contrast 40,10 ) ^
	+swap -compose over -composite ^
	-thumbnail 340x -quality 80 -filter Lanczos example_tn.jpg
it works perfect, but for single file.

When I trying to create a Batch file:

Code: Select all

@Echo Off
Setlocal
color 0a
set "Source=%~dp0"
cd /d "%~dp0"
if not exist ".\*.png" (
echo.
echo ======================== FAILED! Files *.png not found. ========================
echo.
 pause
 endlocal & exit
) else (
echo.
echo: Lossy compress all PNG in a Directory:
echo: %Source%
if not exist Compressed mkdir Compressed
for %%i in (*.png) do (
	magick ^
	"%%i" -set option:wxh "%wx%h" ^
	( -size "%[wxh]" tile:pattern:checkerboard -brightness-contrast 40,10 ) ^
	+swap -compose over -composite ^
	-thumbnail 340x -quality 80 -filter Lanczos ^
	".\Compressed\%%~ni_Compressed.jpg"
)
)&& cls
echo.
echo   -----------------------------------------------------
echo                       Process done!
echo   -----------------------------------------------------
echo.
pause
endlocal & exit
and run it, this gives an error:

Code: Select all

magick: MissingArgument `-size`at CLI arg 6 @ fatal/magick-cli.c/ProcessCommandOptions/447.
"+swap" is not recognized as an internal or external command... 
Thanks in advance for any help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Transparency checkerboard with convert

Post by snibgo »

magick: MissingArgument `-size`at CLI arg 6 @ fatal/magick-cli.c/ProcessCommandOptions/447
Because you forgot to double the % in "%[wxh]".
"+swap" is not recognized as an internal or external command...
Because you didn't escape the parentheses within the magick command. As this is in a "for... do ( ...)" loop, Windows sees the close-parenthesis as the end of the loop, and tries to interpret "+swap" as the next command.

You can escape them thus: ^( and ^).
snibgo's IM pages: im.snibgo.com
DenSEO
Posts: 2
Joined: 2019-07-25T04:28:18-07:00
Authentication code: 1152

Re: Transparency checkerboard with convert

Post by DenSEO »

Snibgo, thanks for the quick and complete answer. I appreciate it!
Now it works perfect!
Post Reply