Page 1 of 1

Transparency checkerboard with convert

Posted: 2017-01-20T11:29:52-07:00
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?

Re: Transparency checkerboard with convert

Posted: 2017-01-20T11:54:32-07:00
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/

Re: Transparency checkerboard with convert

Posted: 2017-01-20T12:15:45-07:00
by Swoops
Hi Fred, thanks a lot for the examples and it works wonderful! Next time I will add the version and platform.

Re: Transparency checkerboard with convert

Posted: 2019-07-25T05:50:29-07:00
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.

Re: Transparency checkerboard with convert

Posted: 2019-07-25T06:06:06-07:00
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 ^).

Re: Transparency checkerboard with convert

Posted: 2019-07-25T06:22:41-07:00
by DenSEO
Snibgo, thanks for the quick and complete answer. I appreciate it!
Now it works perfect!