Search code examples
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"
    
    • If I'm not wrong, the key point here is to make sure the colour of the canvas made with the ppmmake command is the same as the one specified in the pamtopng command.

      I thought there was some 'geometrical' info (i.e. coordinates to define areas and related stuff) to be stored/embedded in the .png file to tell the software not to modify the specified areas/pixels, but it seems the whole business amounts to tell the utility converting from .pnm to .png to interpret as the 'transparent' colour the one formerly specified when generating the image that will be used as the 'canvas'.

      NOTE: in my working scenario (grayscale), I choose white because picking an actual colour to absolutely avoid inteference with grayscale images (i.e., using an extraneous colour, red for instance, makes you sure you won't be messing up with existing colours and then can behave as 'transparent', as a reference for transparency, so to say), produced an undesired effect when the final .pdf file was viewed in okular: a very thin red-coloured frame (or grey, if piped through pngquant) could be clearly perceived. Thus, as the background of my images are white, I chose white.

    • pngquant is here (grayscale context) used for fine-tuning the output .png file, basically these settings reduce its size compared to the output of the convertcommand in the original post.