Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure what your requirements are, but if all you want to do is compare the (RGB) color palettes of two images, you might want to use the <a href="http://commons.apache.org/proper/commons-imaging/apidocs/org/apache/commons/imaging/palette/PaletteFactory.html" rel="nofollow">PaletteFactory</a> methods from <a href="http://commons.apache.org/proper/commons-imaging/" rel="nofollow">Apache Commons Imaging (fka "Sanselan")</a>:</p> <p>The <code>PaletteFactory</code> methods build up collections (<code>int[]</code> and <code>List&lt;&gt;</code>) which can then be iterated over. I'm not sure just what kind of comparison you need to do, but a fairly simple case, using e.g. <code>makeExactRgbPaletteSimple()</code>, would be:</p> <pre><code>final File img1 = new File("path/to/image_1.ext") final File img2 = new File("path/to/image_2.ext") final PaletteFactory pf; final int MAX_COLORS = 256; final Palette p1 = pf.makeExactRgbPaletteSimple(img1, MAX_COLORS); final Palette p2 = pf.makeExactRgbPaletteSimple(img2, MAX_COLORS); final ArrayList&lt;Int&gt; matches = new ArrayList&lt;Int&gt;(Math.max(p1.length(), p2.length())); int matchPercent; // Palette objects are pre-sorted, afaik if ( (p1 != null) &amp;&amp; (p2 != null) ) { if (p1.length() &gt; p2.length()) { for (int i = 0; i &lt; p1.length(); i++) { final int c1 = p1.getEntry(i); final int c2 = p2.getPaletteIndex(c1); if (c2 != -1) { matches.add(c1); } } matchPercent = ( (int)( (float)matches.size()) / ((float)p1.length) * 100 ) ) } else if (p2.length() &gt;= p1.length()) { for (int i = 0; i &lt; p1.length(); i++) { final int c1 = p2.getEntry(i); final int c2 = p1.getPaletteIndex(c1); if (c2 != -1) { matches.add(c1); } } matchPercent = ( (int)( (float)matches.size()) / ((float)p2.length) * 100 ) ) } } </code></pre> <p><em>This is just a minimal example which may or may not compile and is almost certainly not what you're looking for in terms of comparison logic</em>.</p> <p>Basically what it does is check if each member of <code>p1</code> is also a member of <code>p2</code>, and if so, adds it to <code>matches</code>. Hopefully the logic is correct, no guarantees. <code>matchPercent</code> is the percentage of colors which exist in both <code>Palette</code>s.</p> <p><em>This is probably <strong>not</strong> the comparison method you want. It is just a simple example.</em></p> <p>You will definitely need to play around with the 2nd parameter to <code>makeExactRgbPaletteSimple()</code>, <code>int max</code>, as I chose 256 arbitrarily - remember, the method will (annoyingly, imo) return <code>null</code> if <code>max</code> is too small.</p> <p>I would suggest building from source as the repos have not been updated for quite some time. The project is definitely not mature, but it is fairly small, reasonably fast for medium-sized images, and pure Java.</p> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload