How to change histogram stdout format

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
santeko
Posts: 1
Joined: 2018-03-14T14:49:59-07:00
Authentication code: 1152

How to change histogram stdout format

Post by santeko »

Hi All,

Trying to read the result of histogram analysis in python but running into issues based on the stdout formatting.

I run:

Code: Select all

convert pic_4.jpg -colors 256 -depth 8 -format "%c" histogram:info: | head -n 5 >> hist_4.txt
which gives me:

Code: Select all

    705711: ( 37, 42, 35) #252A23 srgb(37,42,35)
    562775: ( 39, 39, 41) #272729 srgb(39,39,41)
    154348: ( 39, 42, 30) #272A1E srgb(39,42,30)
     88998: ( 40, 41, 54) #282936 srgb(40,41,54)
      5824: ( 40, 49,102) #283166 srgb(40,49,102)
I can read this into python using space ( ' ' )

Code: Select all

df = pd.read_table(filename, sep='\s+', header=None)
as a delimiter, however, this leads to issues with the last row where because the blue value (102) has 3 digits instead of 2 (like the rest of the blue values), there isn't a space and this breaks the file read.

Ideally I would like to format the stdout to use comma or tab separated values. OR just strip out the formatting and not add leading spaces in the case of a 1 or 2 digit value.

I've tried searching but only get results showing how to format image outputs, not histogram text stdout.

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

Re: How to change histogram stdout format

Post by snibgo »

In that location (colour tuples), the reliable separator is a comma, not a space. The format is hard-coded in histogram.c GetNumberColors().
snibgo's IM pages: im.snibgo.com
Post Reply