Selected conversion from PNG to TXT

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
AndreaImage
Posts: 6
Joined: 2016-07-27T02:39:49-07:00
Authentication code: 1151

Selected conversion from PNG to TXT

Post by AndreaImage »

Hi

i need to convert from PNG to TXT
but i don't need to convert all the image
i need to know only the information releated to the white pixel

i already perform something with a double DO LOOP using another library but it takes some minute (about 5) because the image it's quite big

is there some other way using Imagemagick ?

maybe compiling some batch file...

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

Re: Selected conversion from PNG to TXT

Post by snibgo »

How do you convert "from PNG to TXT"? That sounds like OCR (optical character recognition), which IM doesn't do. A program called tesseract can do that.

IM can be useful for preparing an image for OCR.

Without seeing your image, further advice is difficult.
snibgo's IM pages: im.snibgo.com
AndreaImage
Posts: 6
Joined: 2016-07-27T02:39:49-07:00
Authentication code: 1151

Re: Selected conversion from PNG to TXT

Post by AndreaImage »

more easy

if you run -convert c:\image.png c:\image_out.txt

in the text file you will find for each pixel the releated color

but i don0t need all the pixel, just the white pixel

is not an ocr application

i just need to know where are located the white pixels in the image

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

Re: Selected conversion from PNG to TXT

Post by snibgo »

Okay, you want the locations of all the white pixels.

Code: Select all

convert in.png +transparent White sparse-color:
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Selected conversion from PNG to TXT

Post by fmw42 »

Snibgo: Should I suggest that sparse-color: be changed to output as list rather than a string?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Selected conversion from PNG to TXT

Post by snibgo »

Well, it's a pain having to split it into lines.

coders\txt.c function WriteTXTImage() once had code to test for an attribute that was supposed to insert '\n' characters. But that code has since been removed.

I think it's dangerous to change IM so it always breaks into lines. But adding an option to tell it to do so would be useful.
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Selected conversion from PNG to TXT

Post by glennrp »

To get a list of the white pixels:

Code: Select all

magick image.png txt:- | grep white
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Selected conversion from PNG to TXT

Post by snibgo »

glenrp wrote:magick image.png txt:- | grep white
Yes, provided the input is fully opaque. If it has transparency, "white" is written as "srgba(255,255,255,1)".
snibgo's IM pages: im.snibgo.com
AndreaImage
Posts: 6
Joined: 2016-07-27T02:39:49-07:00
Authentication code: 1151

Re: Selected conversion from PNG to TXT

Post by AndreaImage »

thanks for the reply

just some question

Code: Select all

 convert in.png +transparent White sparse-color: 
is this code write the output to some files?

Code: Select all

 image.png txt:- | grep white 
i don't think that "grep" is for Windows, i read somewhere to use find, is necessary to declare the output file?

thanks again
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Selected conversion from PNG to TXT

Post by fmw42 »

Use redirect:

Code: Select all

convert in.png +transparent White sparse-color: > textfile.txt
or

Code: Select all

convert image.png txt:- | grep "white" > textfile.txt

Search google for "grep for windows"

http://gnuwin32.sourceforge.net/packages/grep.htm
http://www.wingrep.com/
http://www.wingrep.com/download.htm

Lots of results.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Selected conversion from PNG to TXT

Post by snibgo »

Or:

Code: Select all

convert in.png +transparent White sparse-color:textfile.txt

The Microsoft program is called "findstr". So:

Code: Select all

convert in.png txt:- | findstr white
or

Code: Select all

convert in.png txt:- | findstr white >textfile.txt
This is far slower than the previous method.
snibgo's IM pages: im.snibgo.com
AndreaImage
Posts: 6
Joined: 2016-07-27T02:39:49-07:00
Authentication code: 1151

Re: Selected conversion from PNG to TXT

Post by AndreaImage »

Thanks again

just last question

is it possibile to revert the output to the text file but just one information to each line?

thanks again
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Selected conversion from PNG to TXT

Post by fmw42 »

In unix, you can do

Code: Select all

convert in.png  +transparent White sparse-color: | tr " " "\n"
Post Reply