bashcanvasalpha-transparencycompositingnetpbm

Netpbm: generating a transparent background image/file to put another one on to it at specific coordinates


As stated in the title, I need to know how to achieve this, which is ImageMagick, using the Netpbm programs suite (use of bash/CLI assumed):

convert -size 4800x3450 xc:transparent background.png; convert background.png overlay_image.pnm -geometry +469+499 -composite png:- | img2pdf - | pdftk - multibackground another_image.pdf output def_image.pdf

One liner code provided to give context. I only need the convert part.

Another way to state the problem having the Gimp in mind could be: how to put an image (import image as a layer) over a (transparent) canvas and save the whole thing without flattening it, that is, keeping the transparency of the background/canvas.

Feel free to elaborate thoroughly what's going on with channels, transparencies, etc. (and also even with more basic image processing concepts).

I'm a newbie on this and I'm speaking only from the point of view of what I want, not from what I should know. Thank you in advance.

(The Netpbm suite documentation is very stingy in examples for the newbie. After many unsuccessful tries, probably the best chance to know what's going on is to provide the ImageMagick example and see how it translates to the Netpbm suite.)


Solution

  • It seems this is what I was looking for (this is bash/CLI; I'm giving a one-liner again because this is what I'll be actually using):

    ppmmake white "${CANVAS_WIDTH}" "${CANVAS_HEIGHT}" | pamcomp -xoff="${x1}" -yoff="${y1}" "${OVERLAY_PNM_IMAGE}" - | pamtopng  -transparent=white | pngquant --speed 1 --posterize 4 --strip - | img2pdf - | pdftk - multibackground "${IMAGE%.pnm}_ocr.pdf" output "def.pdf"
    

    The following is a MWE to test on the fly, with ad hoc generated files. pngquant is removed here:

    CANVAS_W=4800; CANVAS_H=3450; W=1000; H=1000; x1=500; x2=500; \
    ppmmake blue "${W}" "${H}" > overlay_img.pnm; \
    ppmmake orange "${CANVAS_W}" "${CANVAS_H}" | ppmtojpeg | img2pdf - > pdf_file.pdf; ppmmake red "${CANVAS_W}" "${CANVAS_H}" | pamcomp -xoff="${x1}" -yoff="${y1}" "overlay_img.pnm" - | pamtopng -transparent=red | img2pdf - | pdftk - multibackground "pdf_file.pdf" output "def.pdf"