The Ultrashock Ultra Bundle
  • Home
  • Community
  • Forum
  • Development
  • Server Side
  • Thread
  •  
  • Previous topic
  • Next topic
Sign up to post

Development
 Server Side

  • tribalab Author 
    • 1482 
    • 0 
    • 11 
    [PHP + GD] Colors in a JPG file
    dream0r

    Last reply Nov 22 2006, 12:27 AM

    by dream0r

    Posted: Nov 20 2006, 07:55 PM

    by tribalab

     

I need to loop through the colors in a 16x16 px image file and find the most common/average color. Does anyone have any ideas as to the best way to do this?

Is there anything on the backend that I can use or should I do this on the front end with flash?

Thx!!

Aaron

  11 REPLIES
 
Jeff
1  
Jeff

Probably easiest to do on the server using something like ImageMagick/MagickWand - http://www.bitweaver.org/doc/magickwand/MagickGetImagePixels.html

  • 20 November 2006 08:18 PM
  •  
jr_alivio
2  
jr_alivio

here’s a quick one:

[php]
<?php

$box = imagecreatefromjpeg(‘test.jpg’);

$bxw = imagesx($box);
$bxh = imagesy($box);

$pixels = array();

for ($i = 0; $i < $bxh; $i++) {

for ($j = 0; $j < $bxw; $j++) {

  $color = dechex( imagecolorat($box, $j, $i) );
  $colorl = strlen($color);

  if ($colorl < 6) {
  $color = str_repeat(‘0’, 6 - $colorl) . $color;
  }

  if (array_key_exists($color, $pixels)) {
  $pixels[$color]++;
  } else {
  $pixels[$color] = 1;
  }

}

}

asort($pixels);

$common_color = end( array_keys($pixels) );
$color_count = array_pop($pixels);

echo “The common color is: #$common_color with $color_count count(s).”;

?>[/php]

my idea basically is, you record the values in an array, with the array key being the color itself, if the array key exists, then you just increment its value, if it’s not, then you add it to the array, then at the end, you just sort the array and get the end element.

  • 20 November 2006 09:12 PM
  •  
tribalab Author 
3  
tribalab

Hey, thanks guys!

jr_avilio, that’s exactly what I was thinking- the only missing piece to the puzzle is this- is there any way to break down the color count into say 64 colors or something.. I am concerned that if I allow the range of colors to get too great, there might be too much variation to get a strong reading..

Of course, on the other hand, I don’t want to go too far, as I will then have the opposite problem..

Thoughts?

  • 20 November 2006 09:57 PM
  •  
jr_alivio
4  
jr_alivio

maybe you could create a new instance of a palette-based image and imagecopy() to it.

  • 20 November 2006 09:59 PM
  •  
tribalab Author 
5  
tribalab

Is there a built-in way to constrain the palette? I’ve come across some examples of “building” the palette, but havent done anything like that before.

aaron

  • 21 November 2006 12:04 AM
  •  
jr_alivio
6  
jr_alivio

uhhhm, try imagetruecolortopalette() ?

  • 21 November 2006 05:38 AM
  •  
tribalab Author 
7  
tribalab

Alright,

I just started tinkering & this seems to work on some images and not so well on others. Anything stick out to you that I can optimize?

<?php

function ImageTrueColorToPalette2( $image, $dither, $ncolors )
{
   $width 
= imagesx( $image );
   
$height = imagesy( $image );
   
$colors_handle = ImageCreateTrueColor( $width, $height );
   
ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
   
ImageTrueColorToPalette( $image, $dither, $ncolors );
   
ImageColorMatch( $colors_handle, $image );
   
ImageDestroy( $colors_handle );
}

$filepath 
= $_GET[ "file" ];

$sourceimage = imagecreatefromjpeg( $filepath ); 

$width = imagesx($sourceimage); 
$height = imagesy($sourceimage); 

$newwidth = 5;
$newheight = 5;

ImageTrueColorToPalette2( $sourceimage, FALSE, 5 );

$outputimage    = imagecreatetruecolor($newwidth, $newheight);
                  
imagecopyresampled($outputimage, $sourceimage, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                  
imagejpeg($outputimage,"temp.jpg",100);


$bxw = imagesx($outputimage); 
$bxh = imagesy($outputimage); 

$pixels = array(); 

for (
$i = 0; $i < $bxh; $i++) { 

    
for ($j = 0; $j < $bxw; $j++) { 

        $color 
= dechex( imagecolorat($outputimage, $j, $i) ); 
        
$colorl = strlen($color); 

        if (
$colorl < 6) { 
            $color 
= str_repeat('0', 6 - $colorl) . $color; 
        
} 

        
if (array_key_exists($color, $pixels))
        
{ 
            $pixels[$color]
++; 
        
}
        
else
        
{ 
            $pixels[$color] 
= 1; 
        
} 

    } 

} 

asort
($pixels); 

$common_color = end( array_keys($pixels) ); 
$color_count  = array_pop($pixels); 

?> 

& Here are a couple previews:
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=red.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=_ADL6311_rt.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=maskedriderstatue.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=_ADL2820_rt.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=colone.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=DSC_7424_rt.jpg
http://lab-media.com//build/ttu/_tests/mostCommonColor.php?file=football2smallweb.jpg

Thx!!

Aaron

  • 21 November 2006 07:01 AM
  •  
tribalab Author 
8  
tribalab

I just found out from my client’s IT group that we cannot use php. Is it possible to convert this to ASP?

—edit—

I may end up running the admin & feeds on another server that can run php, so plz feel free to offer any ideas to fix the issues i mentioned previously & if any ahas happen re:porting to asp, I would love to hear em.

thanks again!!
Aaron

  • 21 November 2006 03:04 PM
  •  
jr_alivio
9  
jr_alivio

Of course, on the other hand, I don’t want to go too far, as I will then have the opposite problem..

what do you mean it doesn’t work so well on others? it could be that the conversion to palette leads to a significant color loss? like the red.jpg for example, too much colors were lost in the palette shift.

i dont know if asp can do it, asp.net probably can, any asp dudes around?

  • 21 November 2006 07:03 PM
  •  
tribalab Author 
10  
tribalab

http://lab-media.com//build/ttu/mostCommonColor.php?file=_ADL2820_rt.jpg seems to be pulling a color that doesnt seem to dominate the canvas, but it may just be my eyes..

  • 21 November 2006 07:08 PM
  •  
dream0r
11  
dream0r

Yo,

I got same Idea few months ago, but I had no time to do something similar.

Now I decided to do such a thingy in flash (frontend).

The rar includes swf & fla.

If someone wants to improve the script, please contact me, or much better: post your improvements here, thx

Grab it here

greetings, dan

  • 22 November 2006 12:27 AM
  •  
  •   Log in or join for free to make a comment.
 
Topic actions
  •  Share on Facebook
  •  Share on Twitter
Topic Categories
  •  Show All Topics
  •  Development
    •  Server Side
    •  Client Side
  •  Creative Software
    •  Web
    •  Video
    •  3D
    •  Illustrator
    •  Photoshop Battles
    •  Photoshop
  •  Design
    •  Typography
    •  Resources & Insight
    •  Checkpoint
  •  Career
    •  Copyright Matters
    •  Advice & issues
    •  Job Seekers
    •  Job Offers
  •  Flash
    •  UltraMath
    •  OOP
    •  Third Party Tools
    •  Open Source alternatives
    •  Data Communication
    •  Components
    •  Flex
    •  AIR
    •  Flash Lite
    •  Flash Professional
    •  Flash Newbie
    •  ActionScript
    •  XML
  •  Lounge
    •  Polls
    •  Random Chat
    •  Showcase And Critique
    •  BombShock Award Nominations
  •  Community Essentials
    •  BombShock Award Winners
    •  Tutorials
    •  Interviews
    •  News
    •  Bitmap tutorials
Popular Topics
  • Sort by: 
  • Activity
  • Views
  • Comments
  • Likes
Advertise with us
  • Your advertisement here!
  • loading
Ultrashock
  • Creative Assets
  • Community
  • Blog
  1. Home
  2. Forum
+/-
Creative Assets
  • Categories
  • Contributors
  • How to buy
Make Money
  • Commission Rates
  • Referral Program
  • Contributor Program
Community
  • Activity Feed
  • Forum
  • Profiles
About
  • Quick Tour
  • Our History
  • Banners & Logos
Support
  • Contact Ultrashock
  • Advertise with us
  • Legal Information
  •  Keep up to date
  • Flash 780  Flash
  • Audio 6,481  Audio
  • Vector 2,130  Vectors
  • Image 12,338  Images
  • Creative Assets 21,729  Assets
  • Profiles 284,156  Members
  • Topics 93,805  Topics
  • Blog 4  Blog
  • Facebook 1,679  Facebook
  • Twitter 1,160  Twitter
  • Join our FREE monthly newsletter!
  • Archive
  • Invalid email address. Please try again.
Subscribe
  • ©2012 Ultrashock LLC - All rights reserved
  • Terms of Use
  • Privacy Policy
  • Switch to dark theme
  • RSS Feeds
  • Top

©2012 Ultrashock LLC - All rights reserved

Printed on Thu, February 23, 2012 - 18:26:17