This project has moved. For the latest updates, please go here.

Resizing problem in TIFF to PNG conversion

Jun 24, 2014 at 9:18 AM
Edited Jun 24, 2014 at 9:19 AM
I am trying to convert a .tif to a .png image and resizing it down to 1000 pixels wide. The original image is a scanned text document of 2480x 3507 and I use the resize command on it after reading it in. The resultant image has missing pixels and it seems that anti-aliasing is not working for this.

I tried the sample command as well which works much better than the resize but still some pixels get destroyed. I am using the following code :-
    MagickReadSettings settings = new MagickReadSettings();
    settings.Density = new MagickGeometry(150, 150);

    // Read the PDF into a collection of images
    images.Read(currentDocument.OriginalPath, settings);

    foreach (MagickImage image in images)
    {
       // Formatting image
       image.Format = MagickFormat.Png;
       image.Alpha(AlphaOption.Remove);
       if (image.BaseWidth > image.BaseHeight)
       {
           //image.Resize(1000, 0);
           image.Sample(1000, 0);
       }
       else
       {
           //image.Resize(0, 1000);
           image.Sample(1000, 0);
       }
       image.Quality = 100;
       image.Annotate("Some Watermark", Gravity.Southwest);

       // Write the image to the specified path as a PNG
       image.Write("test.png");
    }
Am I missing something in this?
I am using the latest 6.8.9.401 Q16 x64 version of the Magick.NET.

Thanks
Coordinator
Jun 24, 2014 at 10:05 AM
What do you mean by 'missing pixels' and 'some pixels get destroyed', can you post an example (in something like dropbox) so I can see what you are trying to do?

I don't know if it will fix it but you should remove the 'image.Alpha(AlphaOption.Remove)' statement if you want to keep anti-aliasing. You need semi transparent pixels for that.
Jun 24, 2014 at 2:49 PM
The pixels sort of fade away when the image is shrinked. The more you shrink, the more it fades. Mine is a black and white document and part of the characters were faded and only white was left making it difficult to read the word.

This only happens if I convert to PNG. I have tried converting it to a JPEG, GIF and BMP and they all work without any problem. Also, I tried removing the Alpha Option code but still the same.

I can't upload anything from my workplace but I can email you an example of this, if that is okay?

Thanks
Coordinator
Jun 24, 2014 at 5:37 PM
Edited Jun 24, 2014 at 7:01 PM
Please contact me through Codeplex so I will get your e-mail and you can send me an example.
Coordinator
Jun 25, 2014 at 11:09 AM
Edited Jun 25, 2014 at 6:46 PM
I am sorry that I did not realize it earlier but you are using the ‘Sample’ method to resize and image. The Sample method is very quick but it works terrible for text. You should use the Resize method. You can find more info here: http://www.imagemagick.org/Usage/resize/#sample and here: http://www.imagemagick.org/Usage/resize/#resize.

Edit:
The problem was the Depth of the image. Setting this to 16 instead of 8 fixed the resize issue. The following code resized the image correctly:
using (MagickImage image = new MagickImage())
{
  image.Read("test1.tif");
  image.Depth = 16;
  //image.Format = MagickFormat.Png; // You don't need do to this when you write to a file.
  image.Alpha(AlphaOption.Remove);
  image.Resize(1000, 0);
  //image.Quality = 100; // You don't need this with a PNG image.
  
  // Sets the font and font size of the annotate operation.
  image.Font = "Tahoma";
  image.FontPointsize = 16;

  image.Annotate("Some Watermark", Gravity.Southwest);
  image.Write("test1.png");
}