node.jsimagemagicktiffgm

How to convert multiple tiff file to png files using gmmagick?


I have to convert multipple tiff to separate png files.For isntance i have a tiff file which have a 3 pages and i'd like to convert it 3 separate png's.In below code i could only convert first page of tiff file into png.

gm(__dirname+'/Tiff/Recorded.tiff').write(__dirname+'/Png/cpng.png',(err)=>{
if(!err){
    console.log('Done');
}
else {
    console.log(err);
}

});

How could i convert for example a second page of this tiff file?

And second question regarding adding a tiff file into another tiff.For example i have tiff file with one page and have second tiff file with three pages? I investigate i have found the append method but it was not working.The code below!

gm(__dirname+'/Tiff/Recorded.tiff')
.append(__dirname+'/Tiff/another.tiff').append(false);

could i add the tiff with one page into the tiiff with three pages and get a one tiff file with four pages?


Solution

  • Your question is quite unclear, so I will show you some examples and you can extract the bits you need.

    Let's say we start with a single TIFF file, called 6page.tif, that has 6 pages in it. Let's see how many pages there are:

    gm identify 6page.tif
    6page.tif[0] TIFF 595x842+0+0 DirectClass 16-bit 17.2Mi 0.000u 0m:0.000000s
    6page.tif[1] TIFF 595x842+0+0 DirectClass 16-bit 17.2Mi 0.000u 0m:0.000000s
    6page.tif[2] TIFF 595x842+0+0 DirectClass 16-bit 17.2Mi 0.000u 0m:0.000000s
    6page.tif[3] TIFF 595x842+0+0 DirectClass 16-bit 17.2Mi 0.000u 0m:0.000000s
    6page.tif[4] TIFF 595x842+0+0 DirectClass 16-bit 17.2Mi 0.000u 0m:0.000000s
    6page.tif[5] TIFF 595x842+0+0 DirectClass 16-bit 17.2Mi 0.000u 0m:0.000000s
    

    Ok, 6 pages, numbered 0-5.

    Let's extract page 3 to a PNG:

    gm convert 6page.tif[3] JustPage3.png
    

    Let's append the extracted page 3 to the end of the original tif:

    gm convert 6page.tif JustPage3.png BigBoy.tif
    

    Now check what we have got - yes, 7 pages:

    gm identify BigBoy.tif
    BigBoy.tif[0] TIFF 595x842+0+0 DirectClass 16-bit 17.7Mi 0.000u 0m:0.000000s
    BigBoy.tif[1] TIFF 595x842+0+0 DirectClass 16-bit 17.7Mi 0.000u 0m:0.000000s
    BigBoy.tif[2] TIFF 595x842+0+0 DirectClass 16-bit 17.7Mi 0.000u 0m:0.000000s
    BigBoy.tif[3] TIFF 595x842+0+0 DirectClass 16-bit 17.7Mi 0.000u 0m:0.000000s
    BigBoy.tif[4] TIFF 595x842+0+0 DirectClass 16-bit 17.7Mi 0.000u 0m:0.000000s
    BigBoy.tif[5] TIFF 595x842+0+0 DirectClass 16-bit 17.7Mi 0.000u 0m:0.000000s
    BigBoy.tif[6] TIFF 595x842+0+0 PseudoClass 65536c 16-bit 17.7Mi 0.000u 0m:0.000000s