21

I am working on reading text from an image through OCR. It only supports TIFF format images.

So, I need to convert other formats to TIFF format. Can it be done? Please help by providing some references.

fmw42
  • 46,825
  • 10
  • 62
  • 80
user1509
  • 1,151
  • 5
  • 17
  • 45
  • @l--''''''---------'''''''''''' it's a different question. – Reza Aghaei Aug 15 '19 at 19:03
  • l--''''''---------'''''''''''' - as @Reza said, by adding pdf to the list you made this an entirely different question, pdf is a vector graphics format while the other formats including the target tiff are bitmap graphics formats. Thus, I'll rollback that edit. – mkl Aug 19 '19 at 18:37
  • @l--''''''---------'''''''''''' your question is How do we convert multiple files into 1 tiff? So I understand that you need to take a set of images 1,2,3,4 and make 1 image tiff that merge all of them 1,2,3,4 in one tiff file is that correct? the other part of question is, you need to cover pdf conversion as well is that correct? hence I have solution to your question. – Maytham Fahmi Aug 20 '19 at 21:17
  • @maytham-ɯɐɥʇʎɐɯ i need to be able to merge image and pdf files into 1 tiff file – Alex Gordon Aug 21 '19 at 13:17
  • @l--''''''---------'''''''''''' would you accept an Amazon Web Service(AWS) based solution? – Taterhead Aug 22 '19 at 15:58

6 Answers6

22

If you create an Image object in .NET, you can save it as a TIFF. It is one of the many ImageFormat choices at your disposal.

Example:

var png = Image.FromFile("some.png");
png.Save("a.tiff", ImageFormat.Tiff);

You'll need to include the System.Drawing assembly in your project. That assembly will give you a lot of image manipulation capabilities. Hope that helps.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Is there anything odd about the files you're converting or more details in the generic error? It's hard to know what error you may be getting with only a vague idea of what you're exactly attempting. – Jacob Aug 07 '12 at 17:17
  • I have edited the tiff images to gif image in Ms Paint. Will that cause an error? – user1509 Aug 09 '12 at 04:43
  • png.Save is a void method, thus one cannot set Tiff equal to its return value – argyle Apr 02 '13 at 18:57
  • This increases the image file size too much, from just 748 KB (image.jpg) to 4.305 KB (image.tif). Is it possible to do this conversion without increasing the image size or at the very least not increasing it that much? I have a requirement which says images should not be larger than 200 KB, so I'm trying to convert TIFF to JPG to reduce the size and than convert it back to TIFF again. After the conversion I plan to do apply some more size reduction steps, but first I need to be able to do this converstion. – Ulysses Alves Oct 11 '17 at 20:57
  • There are move overloads for `Image::Save` which let you be specific with encoder parameters. I'm not an expert in TIFF format, but you may find some useful details here: https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter(v=vs.110).aspx – Jacob Oct 12 '17 at 18:27
  • Also potentially useful: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-image-encoders-and-decoders-in-managed-gdi – Jacob Oct 12 '17 at 18:27
  • 8 years later, is this answer still applicable? – Alex Gordon Aug 16 '19 at 20:48
14

Intro Note:

  1. This answer cover the Bounty Question; which is: How do we convert multiple files into 1 tiff? For example, let's say have pdfs, jpegs, pngs, and I'd like to create 1 tiff out of them?
  2. In this answer I use .net implementation of https://imagemagick.org/index.php for image manipulation and and Ghostscript for helping read an AI/EPS/PDF/PS file so we can translate it to image files both are credible and official source.
  3. After I answered this question I got some extra question in my email asking other merging options, I have therefore extended my answer.

IMO there are 2 steps to your goal:

  1. Install required tools for pdf conversion
  2. Take all images including pdf formatted files from source and merge them together in one tiff file.

1. Install tools that helps Pdf to Image conversion:

Step 1 is only required if you intend to convert AI/EPS/PDF/PS file formats. Otherwise just jump to step2.

To make it possible converting pdf to any image format, we need a library that can read pdf files and we need a tool to convert it to image type. For this purpose, we will need to install Ghostscript (GNU Affero General Public License).

Here after, we need to install ImageMagick.net for .net in Visual Studio, nuget link.

So far so good.

2. Code part

Second and Last step is we need to read files (png, jpg, bmp, pdf etc) from folder location and add each file to MagickImageCollection, then we have several options to merge use AppendHorizontally, AppendVertically, Montage or Multiple page Tiff. ImageMagick has tons of features, like resizing, resolution etc, this is just example to demonstrate merging features:

public static void MergeImage(string src, string dest, MergeType type = MergeType.MultiplePage)
{
    var files = new DirectoryInfo(src).GetFiles();

    using (var images = new MagickImageCollection())
    {
        foreach (var file in files)
        {
            var image = new MagickImage(file)
            {
                Format = MagickFormat.Tif,
                Depth = 8,
            };
            images.Add(image);
        }

        switch (type)
        {
            case MergeType.Vertical:
                using (var result = images.AppendVertically())
                {
                    result.AdaptiveResize(new MagickGeometry(){Height = 600, Width = 800});
                    result.Write(dest);
                }
                break;
            case MergeType.Horizontal:
                using (var result = images.AppendHorizontally())
                {
                    result.AdaptiveResize(new MagickGeometry(){Height = 600, Width = 800});
                    result.Write(dest);
                }
                break;
            case MergeType.Montage:
                var settings = new MontageSettings
                {
                    BackgroundColor = new MagickColor("#FFF"),
                    Geometry = new MagickGeometry("1x1<")
                };

                using (var result = images.Montage(settings))
                {
                    result.Write(dest);
                }
                break;
            case MergeType.MultiplePage:
                images.Write(dest);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, "Un-support choice");
        }

        images.Dispose();
    }
}

public enum MergeType
{
    MultiplePage,
    Vertical,
    Horizontal,
    Montage
}

To run the code

public static void Main(string[] args)
{
    var src = @"C:\temp\Images";
    var dest1 = @"C:\temp\Output\MultiplePage.tiff";
    var dest2 = @"C:\temp\Output\Vertical.tiff";
    var dest3 = @"C:\temp\Output\Horizontal.tiff";
    var dest4 = @"C:\temp\Output\Montage.tiff";

    MergeImage(src, dest1);
    MergeImage(src, dest2, MergeType.Vertical);
    MergeImage(src, dest3, MergeType.Horizontal);
    MergeImage(src, dest4, MergeType.Montage);
}

Here is 4 input files in C:\temp\Images:

enter image description here enter image description here enter image description here enter image description here

After running the code, we get 4 new files under C:\temp\Output looks like this:

enter image description here 4 page Multiple Page Tiff

enter image description here 4 image Vertical Merge

enter image description here 4 image Horizontal Merge

enter image description here 4 image Montage Merge

Final note:

  1. it is possible to merge multiple images to tiff using System.Drawing; and using System.Drawing.Imaging; with out using ImageMagick, but pdf does require a third party conversion library or tool, therefore I use Ghostscript and ImageMagick for C#.
  2. ImageMagick has many features, so you can change the resolution, size of output file etc. it is well recognized library.

Disclaimer: A part of this answer is taken from my my personal web site https://itbackyard.com/how-to-convert-ai-eps-pdf-ps-to-image-file/ with source code to github.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • ImageMagick can merge the output into one multipage tiff without merging the input images first. See my answer. – fmw42 Aug 23 '19 at 05:57
  • I saw that thump up, the thing is OP asked for C# code, and as far as I know ImageMagick feature is implementable in different languages, here include C#. My point changing the code make it possible instead of merging, making multiple tiff page. But as I understood in comments from OP, he need to merge them in one tiff file not mention multiple page. If it is multiple page I will update my answer, but I am also waiting to get some comments from OP. That wont change the fact OP need to use imageMagick to convert pdf to image. – Maytham Fahmi Aug 23 '19 at 06:31
4

To be covert the image in tif format.In the below example to be convert the image and set to a text box.to be see the image in text box is (.tif formate).This sources code is working.

private void btn_Convert(object sender, EventArgs e)
    {
        string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
        newName = newName + ".tif";
        try
        {
            img.Save(newName, ImageFormat.Tiff);
        }
        catch (Exception ex)
        {
            string error = ee.Message.ToString();
            MessageBox.Show(MessageBoxIcon.Error);

        }
        textBox2.Text = System.IO.Path.GetFullPath(newName.ToString());
    }
Bibin
  • 492
  • 5
  • 11
  • How does this answer the question, does it convert pdf? does it merge the files as OP ask? – Maytham Fahmi Aug 22 '19 at 05:58
  • To be convert pdf to tiff.using PDF to image converter SDK – Bibin Aug 22 '19 at 06:22
  • This is exactly what the question about, OP wants to convert multiple files including pdf to one tiff file. – Maytham Fahmi Aug 22 '19 at 06:28
  • Create an instance of PQScan.PDFToImage.PDFDocument object.Load a local PDF file.Obtain the total page count.Turn PDF file page to image.Save image as tiff image file type. – Bibin Aug 22 '19 at 06:29
  • The Bounty question: How do we convert multiple files into 1 tiff? For example, let's say have pdfs, jpegs, pngs, and I'd like to create 1 tiff out of them? – Maytham Fahmi Aug 22 '19 at 07:46
3

I tested this with jpg, bmp, png, and gif. Works for single and multipage creation of tiffs. Pass it a full pathname to the file. Hope it helps someone. (extracted from MSDN)

public static string[] ConvertJpegToTiff(string[] fileNames, bool isMultipage)
    {
        EncoderParameters encoderParams = new EncoderParameters(1);
        ImageCodecInfo tiffCodecInfo = ImageCodecInfo.GetImageEncoders()
            .First(ie => ie.MimeType == "image/tiff");

        string[] tiffPaths = null;
        if (isMultipage)
        {
            tiffPaths = new string[1];
            System.Drawing.Image tiffImg = null;
            try
            {
                for (int i = 0; i < fileNames.Length; i++)
                {
                    if (i == 0)
                    {
                        tiffPaths[i] = String.Format("{0}\\{1}.tif",
                            Path.GetDirectoryName(fileNames[i]),
                            Path.GetFileNameWithoutExtension(fileNames[i]));

                        // Initialize the first frame of multipage tiff.
                        tiffImg = System.Drawing.Image.FromFile(fileNames[i]);
                        encoderParams.Param[0] = new EncoderParameter(
                            System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                        tiffImg.Save(tiffPaths[i], tiffCodecInfo, encoderParams);
                    }
                    else
                    {
                        // Add additional frames.
                        encoderParams.Param[0] = new EncoderParameter(
                            System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                        using (System.Drawing.Image frame = System.Drawing.Image.FromFile(fileNames[i]))
                        {
                            tiffImg.SaveAdd(frame, encoderParams);
                        }
                    }

                    if (i == fileNames.Length - 1)
                    {
                        // When it is the last frame, flush the resources and closing.
                        encoderParams.Param[0] = new EncoderParameter(
                            System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                        tiffImg.SaveAdd(encoderParams);
                    }
                }
            }
            finally
            {
                if (tiffImg != null)
                {
                    tiffImg.Dispose();
                    tiffImg = null;
                }
            }
        }
        else
        {
            tiffPaths = new string[fileNames.Length];

            for (int i = 0; i < fileNames.Length; i++)
            {
                tiffPaths[i] = String.Format("{0}\\{1}.tif",
                    Path.GetDirectoryName(fileNames[i]),
                    Path.GetFileNameWithoutExtension(fileNames[i]));

                // Save as individual tiff files.
                using (System.Drawing.Image tiffImg = System.Drawing.Image.FromFile(fileNames[i]))
                {
                    tiffImg.Save(tiffPaths[i], ImageFormat.Tiff);
                }
            }
        }

        return tiffPaths;
    }
wbt11a
  • 798
  • 4
  • 12
3

ImageMagick command line can do that easily. It is supplied on most Linux systems and is available for Mac or Windows also. See https://imagemagick.org/script/download.php

convert image.suffix -compress XXX image.tiff


or you can process a whole folder of files using

mogrify -format tiff -path path/to/output_directory *


ImageMagick supports combining multiple images into a multi-page TIFF. And the images can be of mixed types even including PDF.

convert image1.suffix1 image2.suffix2 ... -compress XXX imageN.suffixN output.tiff


You can choose from a number of compression formats or no compression.

See

https://imagemagick.org/script/command-line-processing.php

https://imagemagick.org/Usage/basics/

https://imagemagick.org/Usage/basics/#mogrify

https://imagemagick.org/script/command-line-options.php#compress


Or you can use Magick.Net for a C# interface. See https://github.com/dlemstra/Magick.NET

Main ImageMagick page is at https://imagemagick.org.

Supported formats are listed at https://imagemagick.org/script/formats.php

You can easily process your images to resize them, convert to grayscale, filter (sharpen), threshold, etc, all in the same command line.

See

https://imagemagick.org/Usage/

https://imagemagick.org/Usage/reference.html

fmw42
  • 46,825
  • 10
  • 62
  • 80
2

This is how I convert images that are uploaded to a website. Changed it so it outputs Tiff files. The method input and outputs a byte array so it can easily be used in a variety of ways. But you can easily modify it.

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public byte[] ConvertImageToTiff(byte[] SourceImage)
{
    //create a new byte array
    byte[] bin = new byte[0];

    //check if there is data
    if (SourceImage == null || SourceImage.Length == 0)
    {
        return bin;
    }

    //convert the byte array to a bitmap
    Bitmap NewImage;
    using (MemoryStream ms = new MemoryStream(SourceImage))
    {
        NewImage = new Bitmap(ms);
    }

    //set some properties
    Bitmap TempImage = new Bitmap(NewImage.Width, NewImage.Height);
    using (Graphics g = Graphics.FromImage(TempImage))
    {
        g.CompositingMode = CompositingMode.SourceCopy;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.DrawImage(NewImage, 0, 0, NewImage.Width, NewImage.Height);
    }
    NewImage = TempImage;

    //save the image to a stream
    using (MemoryStream ms = new MemoryStream())
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 80L);

        NewImage.Save(ms, GetEncoderInfo("image/tiff"), encoderParameters);
        bin = ms.ToArray();
    }

    //cleanup
    NewImage.Dispose();
    TempImage.Dispose();

    //return data
    return bin;
}


//get the correct encoder info
public ImageCodecInfo GetEncoderInfo(string MimeType)
{
    ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
    for (int j = 0; j < encoders.Length; ++j)
    {
        if (encoders[j].MimeType.ToLower() == MimeType.ToLower())
            return encoders[j];
    }
    return null;
}

To test

var oldImage = File.ReadAllBytes(Server.MapPath("OldImage.jpg"));
var newImage = ConvertImageToTiff(oldImage);
File.WriteAllBytes(Server.MapPath("NewImage.tiff"), newImage);
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • does it convert pdf to image as OP asks – Maytham Fahmi Aug 22 '19 at 06:00
  • @maytham-ɯɐɥʇʎɐɯ no because OP is asking for conversion to TIFF. He already has OCR software. – VDWWD Aug 22 '19 at 07:12
  • 1
    as my understanding is this is old question, so the bounty question does ask following question: Looking for an answer drawing from credible and/or official sources. How do we convert multiple files into 1 tiff? For example, let's say have pdfs, jpegs, pngs, and I'd like to create 1 tiff out of them? – Maytham Fahmi Aug 22 '19 at 07:45
  • @maytham-ɯɐɥʇʎɐɯ I think you are right. OP is asking about TIFF but the bounty giver wants to combine multiple images and pdf into one file. Did not read the comments... Will try to create a solution if I can. – VDWWD Aug 22 '19 at 07:49