Saving multipage TIF

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
sheier
Posts: 3
Joined: 2015-07-08T13:56:41-07:00
Authentication code: 1151

Saving multipage TIF

Post by sheier » 2015-07-08T14:00:22-07:00

Hello all, I am trying to take in a multi-page TIF, overlay some text on the first page, and then save it again. I'm able to load the file as a MagickImageCollection and loop through the pages, but when I write it back to disk it is always only the first page of the TIF. I am using VS.NET. Does anyone have experience with this type of operation? Thanks in advance!

User avatar
fmw42
Posts: 22079
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Saving multipage TIF

Post by fmw42 » 2015-07-08T14:06:31-07:00

Are you writing the same information to each page or different information on each page.


If the same information, then see gif animations (http://www.imagemagick.org/Usage/anim_mods/#composite). I believe you can do the same with multilayer tiff files.

If separate information to each page, then you probably need to clone each page, write to it, delete the originals and then combine the modified pages into a new image.

sheier
Posts: 3
Joined: 2015-07-08T13:56:41-07:00
Authentication code: 1151

Re: Saving multipage TIF

Post by sheier » 2015-07-08T14:09:19-07:00

I really only have to write the information on the top page, all the other pages can stay as they are.

User avatar
fmw42
Posts: 22079
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Saving multipage TIF

Post by fmw42 » 2015-07-08T14:36:09-07:00

clone the first page, then write to it, then replace the old first page with the new one.

User avatar
dlemstra
Posts: 1380
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Saving multipage TIF

Post by dlemstra » 2015-07-08T21:50:30-07:00

Are you writing the MagickImageCollection or the MagickImage to disk?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

ceepea
Posts: 9
Joined: 2015-07-31T09:33:52-07:00
Authentication code: 1151

Re: Saving multipage TIF

Post by ceepea » 2015-08-03T10:58:27-07:00

ARGH!!!! Found the problem, do not know if this is in a FAQ but hopefully others will see this.

Wrong "convert" program was running.
The built in "convert" of Win7 which converts file systems was being run, not the IM convert.
When i specified the path to the IM convert, image creation worked fine.

(the other day i had adjusted the path to go to IM first, that adjustment got lost upon reboot of pc)

Thank you for the working scripts .

mihail_potcoava
Posts: 4
Joined: 2017-05-12T05:41:51-07:00
Authentication code: 1151

Re: Saving multipage TIF

Post by mihail_potcoava » 2017-05-12T07:23:09-07:00

Hi,

I'm having exactly the same problem as described in this thread:
"I'm able to load the file as a MagickImageCollection and loop through the pages, but when I write it back to disk it is always only the first page of the TIF"

Unfortunately, I don't understand the solution ("Wrong "convert" program was running.")
Where I can find the "IM convert"?
How can I change the path to go to IM convert first?

Any advice is appreciated, I'm struggling for some time now and I can't seem to find a solution.

Many thanks!
Mihai.

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Saving multipage TIF

Post by snibgo » 2017-05-12T07:40:14-07:00

Please tell us what version of IM you use, on what platform.

Tell us exactly what command you used (or what program you wrote).

What result did you get? What result did you want?
snibgo's IM pages: im.snibgo.com

mihail_potcoava
Posts: 4
Joined: 2017-05-12T05:41:51-07:00
Authentication code: 1151

Re: Saving multipage TIF

Post by mihail_potcoava » 2017-05-13T00:17:37-07:00

Hi,

Many thanks for getting back to me so quickly.

I've built a small test console application with my scenario (something simple).
If you could open the solution and run it would be great. I think it's the quickest way to understand the problem.

Some details:
Platform: Windows 10, .Net Framework 4.5.2
NugetPackage: id="Magick.NET-Q16-AnyCPU" version="7.0.5.502"

Basically, I want to load a layered tiff file, change one of the layers and then rebuild the tiff file (as layered).
Everything seems to work ok but the resulting tiff file (sample_new.tif) looks like the first layer (when opened with any windows program).

If I open the resulting tiff (sample_new.tif) again it looks ok, all the layers are there, but now sure why the tiff file is not looking as if all layers are combined.

I hope it's clear, please advise.

Many thanks for your help,
Mihai.

mihail_potcoava
Posts: 4
Joined: 2017-05-12T05:41:51-07:00
Authentication code: 1151

Re: Saving multipage TIF

Post by mihail_potcoava » 2017-05-13T00:26:10-07:00

Sorry, I thought I can attach the zip containing the .NET solution but it seems I can't.

You should be able to download the zip with the solution here:
https://drive.google.com/file/d/0B0zlZw ... sp=sharing

The source code is here:

Code: Select all

	    // read the sample tif file
            using (MagickImageCollection images = new MagickImageCollection())
            {
                Console.WriteLine("reading the tif file");
                images.Read("../../res/sample.tif");

                // there should be 8 layers. (not sure why the first layer is the tif image and the seccond seems invalid)
                // iterate through layers, replace layer 4 color with a new one
                Console.WriteLine("changing layer nr 4");
                var oldColor = System.Drawing.ColorTranslator.FromHtml("#FEAF5C");
                var newColor = System.Drawing.ColorTranslator.FromHtml("#00ff00");
                images[4].Opaque(MagickColor.FromRgb(oldColor.R, oldColor.G, oldColor.B), MagickColor.FromRgb(newColor.R, newColor.G, newColor.B));

                // try to re-build the original tiff file but with one layer changed
                //just to be sure we're creating a new collection to skip the first two layers
                using (MagickImageCollection newImages = new MagickImageCollection())
                {
                    // add layers to a new collection
                    Console.WriteLine("copying to new collection and write each layer");
                    int index = 0;
                    foreach (var img in images.Skip(2))
                    {
                        // just to be sure the layers look good we're going to save all as PNG
                        img.Write(string.Format("../../res/layer_{0}_.png", index));

                        newImages.Add(img);
                        index++;
                    }

                    // write the new collection to disc
                    Console.WriteLine("writing the new file...");
                    newImages.Write("../../res/sample_new.tif");
                }
            }
 

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Saving multipage TIF

Post by snibgo » 2017-05-13T01:24:15-07:00

Sorry, I know nothing about Magick.NET.

You zip file contains res/sample.tif, which contains eight images. I suppose this is your input file. It might be helpful if you also showed your output res/sample_new.tif.
snibgo's IM pages: im.snibgo.com

User avatar
dlemstra
Posts: 1380
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Saving multipage TIF

Post by dlemstra » 2017-05-13T04:49:06-07:00

Your tiff file is organized like this: [merged image] [layer 1] [layer2] etc. The layers are normally not visible in an image viewer but Magick.NET knows how to extract the Photoshop layers. So your code should do images.Skip(1) instead and make the first image the new 'merged image' of all the layers (index 1 to 7). But that won't help you yet because writing multi layer TIFF files is not supported at the moment. Your multi layer tiff file is changed into a multi page tiff file. We have created an issue on github to add this at some point in the future: https://github.com/ImageMagick/ImageMagick/issues/425.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

mihail_potcoava
Posts: 4
Joined: 2017-05-12T05:41:51-07:00
Authentication code: 1151

Re: Saving multipage TIF

Post by mihail_potcoava » 2017-05-13T06:08:06-07:00

Many thanks for your reply.

I honestly thought that pages and layers in a tiff are the same thing.

I'll dig more, install evince / tiffinfo / photoshop to understand better.

the good thing is that I know now why my resulting tiff looks like the fist layer / page.

So I can Combine / Coalesce all the layers and add the resulting layer to be the first one and this way it will look ok.

Thanks again for your help and quick answer!

Post Reply