Steve Scrimpshire Posted February 3, 2003 Report Share Posted February 3, 2003 This may be a dumb question, but what I'd like to do is read an image file and then output pixel by pixel what the hex or RGB values are of the pixel. Is that possible? I want this to be done automatically. I know I can check a pixel in The Gimp using the eyedropper tool, but to do all pixels would be tedious. Essentially, what I'd ultimately like is a 2-dimensional array with the hexadecimal values of color for each pixel. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 3, 2003 Author Report Share Posted February 3, 2003 I think I found exactly what I wanted in the perl module Image::Magick BUT it won't run, claiming it requires libpng3 and attempting to upgrade to libpng3 from libpng2 gives me all kinds of dependency issues that cannot be resolved (I'm using MDK 8.1). gggrrrrrrrrrrrr Quote Link to comment Share on other sites More sharing options...
johnnyv Posted February 3, 2003 Report Share Posted February 3, 2003 Try php it is so much easier to learn IMO http://www.php.net/manual/en/ref.image.php imagecolorat -- is the function you want (PHP 3, PHP 4 ) imagecolorat -- Get the index of the color of a pixel Description int imagecolorat ( resource image, int x, int y) Returns the index of the color of the pixel at the specified location in the image specified by image. PHP ROCKS!!! :wink: Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 3, 2003 Author Report Share Posted February 3, 2003 Thanks. I'm actually there reading. Here is my original script: <html> <?php $im = ImageCreateFromPng("pride.png"); $imagehw = GetImageSize("pride.png"); $imagewidth = $imagehw[0]; $imageheight = $imagehw[1]; for ($j=1;$j<=$imagewidth;$j++) { for ($k=1;$k<=$imageheight;$k++){ $rgb = ImageColorAt($im, $j, $k); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; echo ("At ($j,$k) red= $r <br>"); echo ("green= $g <br>"); echo ("blue= $b <br>"); } } ?></html> Which works, but it clogs up my browser, since the image is so big. So now what I'm trying to do is write the info to a file instead...still working on that. Quote Link to comment Share on other sites More sharing options...
johnnyv Posted February 3, 2003 Report Share Posted February 3, 2003 Thanks. I'm actually there reading. Here is my original script:<html><!--QuoteEBegin--><!--QuoteEBegin--><?php<!--QuoteEBegin--><!--QuoteEBegin-->$im = ImageCreateFromPng("pride.png");<!--QuoteEBegin--><!--QuoteEBegin-->$imagehw = GetImageSize("pride.png");<!--QuoteEBegin--><!--QuoteEBegin-->$imagewidth = $imagehw[0];<!--QuoteEBegin--><!--QuoteEBegin-->$imageheight = $imagehw[1];<!--QuoteEBegin--><!--QuoteEBegin-->for ($j=1;$j<=$imagewidth;$j++) {<!--QuoteEBegin--><!--QuoteEBegin--> for ($k=1;$k<=$imageheight;$k++){<!--QuoteEBegin--><!--QuoteEBegin--> $rgb = ImageColorAt($im, $j, $k);<!--QuoteEBegin--><!--QuoteEBegin--> $r = ($rgb >> 16) & 0xFF;<!--QuoteEBegin--><!--QuoteEBegin--> $g = ($rgb >> 8) & 0xFF;<!--QuoteEBegin--><!--QuoteEBegin--> $b = $rgb & 0xFF;<!--QuoteEBegin--><!--QuoteEBegin--> echo ("At ($j,$k) red= $r <br>");<!--QuoteEBegin--><!--QuoteEBegin--> echo ("green= $g <br>");<!--QuoteEBegin--><!--QuoteEBegin--> echo ("blue= $b <br>");<!--QuoteEBegin--><!--QuoteEBegin--> }<!--QuoteEBegin--><!--QuoteEBegin--> }<!--QuoteEBegin--><!--QuoteEBegin-->?></html> Which works, but it clogs up my browser, since the image is so big. So now what I'm trying to do is write the info to a file instead...still working on that. Yes using a file would definately be better. fopen() etc you know that you can use the standalone php binary? no need fot apache eg at to run a script open a console: php myscript.php Quote Link to comment Share on other sites More sharing options...
johnnyv Posted February 3, 2003 Report Share Posted February 3, 2003 I never used any image stuff with php so yout enquiry made me interested so i modified your script abit. <? $image_name = "test.png"; $data_file = fopen("output.txt", "w"); $im = ImageCreateFromPng($image_name); $image_info = getimagesize($image_name); $width = $image_info[0]; $height = $image_info[1]; $data = "Image Name: ".$image_name."nImage Width: ".$width."nImage Height: ".$height."nn"; fwrite($data_file, $data); $count = 0; for ($j=1;$j<=$width;$j++) { for ($k=1;$k<=$height;$k++) { $rgb = ImageColorAt($im, $j, $k); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $data = "Width: ".$j." Height: ".$k." Red: ".$r." Green: ".$g." Blue: ".$b."n"; fwrite($data_file, $data); $count++; } } $data = "nFile finishednPixels Processed: ".$count; fwrite($data_file, $data); fclose($data_file); ?> you just create a folder say on your desktop create a text file call it whatever.php add you png image to the same directory or modify the $image_name variable to include the path to the image. then just open the console cd to the directory and: php whatever.php thats it. did a 75.6K png in around 2-3 seconds on my 1800xp, processing 82875 pixels, the output.txt file was 400K just as a note the version i am using is the default for mandrake 9.0 php4.2.3. ImageColorAt does not support alpha channel, if you have an alpha channel it will work it will just give you the RGB only. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 3, 2003 Author Report Share Posted February 3, 2003 For some reason, all it was doing was giving me the Blue value of the RGB, so I modified it even more: <? $image_name = "pride.png"; $data_file = fopen("output.txt", "w"); $im = ImageCreateFromPng($image_name); $image_info = getimagesize($image_name); $width = $image_info[0]; $height = $image_info[1]; $data = "Image Name: ".$image_name."nImage Width: ".$width."nImage Height: ".$height."nn"; fwrite($data_file, $data); $count = 0; for ($j=1;$j<=$width;$j++) { for ($k=1;$k<=$height;$k++) { $rgb = ImageColorAt($im, $j, $k); $b = $rgb & 0xFF; $color_tran = ImageColorsForIndex($im, $rgb); $data = "Width: ".$j." Height: ".$k."n Color Index: ".$b." Red: ".$color_tran[red]." Green: ".$color_tran[green]." Blue: ".$color_tran[blue]."n"; fwrite($data_file, $data); $count++; } } $data = "nFile finishednPixels Processed: ".$count; fwrite($data_file, $data); fclose($data_file); ?> Now I can print the Index and the individual RGB values. Thanks. Quote Link to comment Share on other sites More sharing options...
johnnyv Posted February 3, 2003 Report Share Posted February 3, 2003 For some reason, all it was doing was giving me the Blue value of the RGB, so I modified it even more: Really? it worked fine on my box, may be to do with the versions of php/gd etc. Anyway good luck and enjoy. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 6, 2003 Author Report Share Posted February 6, 2003 Thanks for all the help with this. I now have several scripts that do exactly what I want. I used PHP to extract the color values I need and write them to a file and then used Perl (I'm more comfortable with it) to write a script testing to make sure all the values are right and in the right place. Here's the original image: http://omarserenity.com/Images/pride.gif and here is the output of the test script that I wrote to make sure the PHP script captured and stored the values correctly: http://omarserenity.com/cgi-bin/pride.cgi (Warning: This is verrrrrrrry large page and the higher your resolution is set, the better, but you'll still have to scroll around...and it looks best viewed with Netscape...dunno why, but the other browsers double-space the lines and Netscape puts them right close together, at least for me.) Quote Link to comment Share on other sites More sharing options...
Guest William Strong Posted January 25, 2008 Report Share Posted January 25, 2008 I have read your question and response with solution. I've seen where you want to take an image and read it's rgb values to a text file (pixel-by-pixel). The problem is that when I try to run the script on my server, it creates the output.txt because I can see the file; however, the file is not able to be read. What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted January 26, 2008 Author Report Share Posted January 26, 2008 Just to be sure, since you only have one post...Are you running this script on a remote web host or your own Linux server you have physical access to? If it's a remote web host, Apache may have written the file as user 'nobody' and you will have to contact support at your web host to get them to change the owner and/or permissions. If it is on your own Linux server, you can su to root in a console and read the file or change the ownership of the file so your regular user can read it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.