Image to Text C#

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
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Image to Text C#

Post by rlee1985 »

Is it possible to convert an image to a text document with magick.net? I can convert an image to text by using imagemagick IMDisplay and it outputs the image into a text file and looks like this:

# ImageMagick pixel enumeration: 1057,314,65535,srgb
0,0: (33410,1799,6939) #82071B srgb(130,7,27)
1,0: (33924,1285,6168) #840518 srgb(132,5,24)
2,0: (34438,1285,6425) #860519 srgb(134,5,25)
3,0: (34181,1028,6168) #850418 srgb(133,4,24)
4,0: (34181,1028,6168) #850418 srgb(133,4,24)
5,0: (34181,1028,5654) #850416 srgb(133,4,22)
6,0: (34181,1028,5654) #850416 srgb(133,4,22)
7,0: (34695,514,5397) #870215 srgb(135,2,21)

If this is possible are there any examples available?


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

Re: Image to Text C#

Post by snibgo »

Just write the image to a "txt:" format output.
snibgo's IM pages: im.snibgo.com
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

Just read the file using read?

using (MagickImage image = new MagickImage("Image.jpg"))
{

image.Write("NewText.txt");
}


I am getting an error message.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image to Text C#

Post by snibgo »

Try:

image.Write("txt:NewText.txt");
snibgo's IM pages: im.snibgo.com
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

Still does not work. That to me looks like it just renames the file and puts txt: on the front of it. I also added this:

image.Format = MagickFormat.Txt;


and it did not make any difference.
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

private void btnDecode_Click(object sender, EventArgs e)
{
using (MagickImage image = new MagickImage("image.png"))
{
image.Format = MagickFormat.Txt;
image.Write("NewText.txt");
}




}

just doing it on a button click.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image to Text C#

Post by snibgo »

I don't use C# so I can't test it. Perhaps this isn't implemented in the C# interface. Sorry, I don't know what to suggest.
snibgo's IM pages: im.snibgo.com
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

What do you use?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Image to Text C#

Post by snibgo »

I use the command-line interface, or C mostly at the MagickCore level but sometimes MagickWand.

For example, MagickCore:

Code: Select all

  (void) strcpy(thumbnails->filename,argv[2]);
  WriteImage(image_info,thumbnails);
This writes the text you want, when argv[2] is "x.txt" or "txt:x.txt".
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Image to Text C#

Post by dlemstra »

Are you using the latest version of Magick.NET? I get a correct file with the following code:

Code: Select all

using (var image = new MagickImage("logo:"))
{
  image.Write("logo.txt");
}
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

Awesome guys thanks for the help.
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

Dlemstra,

Would you have any idea as to why my program would crash as soon as I try to execute MagickImage code.


This is the process I followed:

Using visual studio 2013.
.Nets are installed up to 4.5.1.
Installed Magick.NET-Q16-x64.
In project set using ImageMagick;
Put this code in a button.
using (MagickImage image = new MagickImage(@"C:\Images\TestImage.jpg"))
{
image.Format = MagickFormat.Txt;
image.Write(@"C:\Images\TestImage.txt");
}

Then I get this error message:
Additional information: Could not load file or assembly 'Magick.NET-Q16-x64, Version=7.0.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Then I installed this.
installed Magick.NET.Core-Q16.Native
I am still getting the same error.

Can you see anything that I am doing wrong?


Thanks,
Robert
rlee1985
Posts: 8
Joined: 2016-04-02T14:04:51-07:00
Authentication code: 1151

Re: Image to Text C#

Post by rlee1985 »

Well I am lucky and it appears to be fixed!


I uninstalled both 64 core and native and used the anycpu and it works now.


THANKS FOR THE HELP!

Have a good one
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Image to Text C#

Post by dlemstra »

It looks like your application is 32 bit. Your error messages means that you try to load a 64-bit library in a 32-bit application. Switching to AnyCPU fixed that. And you don't need to use the .NET Core version of Magick.NET.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply