Output single color as 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
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Output single color as TXT:

Post by Werty »

I need to output files as TXT: but only output a single color like "White", I'm using...

Convert myfile.png myfile.txt

..and get a 40MB large txt file when outputting a 1024x1024 image, which is quite too large, as it outputs all colors.

I only need it to output white colors in the txt file.

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

Re: Output single color as TXT:

Post by snibgo »

I'm not sure what you want. Perhaps "-unique-colors" would help.
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: Output single color as TXT:

Post by fmw42 »

In unix

convert image txt:- | grep "white"
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: Output single color as TXT:

Post by Werty »

I'm on windows :)

I would like to have IM outputting the white colors only, instead of....

0,0: ( 0, 0, 0) #000000 black
1,0: ( 0, 0, 0) #000000 black
2,0: ( 0, 0, 0) #000000 black
3,0: ( 0, 0, 0) #000000 black
4,0: ( 255, 255, 255) #FFFFFF white
5,0: ( 0, 0, 0) #000000 black
6,0: ( 255, 255, 255) #FFFFFF white
7,0: ( 0, 0, 0) #000000 black
8,0: ( 0, 0, 0) #000000 black

.. I would like....

4,0: ( 255, 255, 255) #FFFFFF white
6,0: ( 255, 255, 255) #FFFFFF white

..only.

I read somewhere anthony mentioning it could be done, but didnt leave a solution.
Windows 7 user
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output single color as TXT:

Post by snibgo »

Code: Select all

convert myfile.png txt: |find "white"
"find" is a standard Windows tool.
snibgo's IM pages: im.snibgo.com
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: Output single color as TXT:

Post by Werty »

Thanks, but I'm already sorting the .txt file, picking out the "white"'s, I would like IM to be faster outputting that .txt file.

I would guess that if it only should write the whites, the process would be much faster, like looking up the color of the pixel, if it's white then create the text line for the output, if it's black then just move on instead of spending time writing something that's not needed.

The 1024x1024 images i use are mostly pure black, with a few whites, around 1000 only, so no need to output that million of blacks. :)
Windows 7 user
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output single color as TXT:

Post by snibgo »

Sorting will take a long time. Do you need to do this before filtering the white pixels?

"txt:" will always write all the pixels. If the white pixels are clustered near each other, you might "-trim" first.
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: Output single color as TXT:

Post by fmw42 »

There is a special function create to support -sparse-color ... to efficiently get all non-transparent pixels as text input to -sparse-color. I use it in my stainedglass script. It is a simplified output like txt: but only displays the corrdinates and color for all non-fully-transparent pixels. It has been available since 6.8.3.10

For example

convert rose: +transparent white sparse-color:
61,11,white 62,15,white 62,16,white 63,16,white 14,37,white 12,38,white 13,38,white 23,39,white 12,40,white 24,40,white 25,40,white 11,41,white 12,41,white 21,41,white 26,41,white 20,42,white 28,42,white 29,43,white 18,44,white 30,44,white 31,44,white

This is a list of x,y,color triplets

I do not recall where it is documented. I cannot find it right now.

So all you need to do is make everything not white into transparent and it will find all the white pixels quickly.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output single color as TXT:

Post by snibgo »

Ah, that's useful. It's mentioned in the changelog. It could usefully be added to http://www.imagemagick.org/Usage/files/#special_formats
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: Output single color as TXT:

Post by fmw42 »

It also needs to be added some where on http://www.imagemagick.org/script/formats.php

I am trying to edit the documentation, but seem to have lost upload permission.

Anthony would need to edit the page you reference. I will try to send him a note about it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Output single color as TXT:

Post by fmw42 »

sparse-color: is documented at http://www.imagemagick.org/script/formats.php#supported

I missed finding it before.
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: Output single color as TXT:

Post by Werty »

Sorry for replying so late, I dont have my own internet currently :P

The sparsecolor: sounds awesome as my pics are only pure black and white, I'll check it out as soon as I have upgraded my IM installation, I'm currently on 6.7.6-1 as I had to downgrade because INLINE:DATA was broken in a newer 8.5.x.x I tried.

Thanks, I'll report back, when I'm able to :P
Windows 7 user
Werty
Posts: 66
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: Output single color as TXT:

Post by Werty »

It's me again, I promised to report back :p

Things are now running much much faster, whole process (including some scripting) took about 15 seconds before, now it's almost instant, :)

Thanks again.
Windows 7 user
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Output single color as TXT:

Post by anthony »

I must have missed the inclusion of "sparse-color:" format.

I would have liked it better if it was newline separated rather than space separated. The "-sparse-color" operator would not care, but it would make it easier for scripts to parse!.

Added to IM Examples (just below "txt:")

http://www.imagemagick.org/Usage/files/#sparse-color

(give it a few hours to appear)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply