Solved

Image Log

Posted on 2014-03-22
6
233 Views
Last Modified: 2014-03-30
I am trying to produce a log of all the pixels of an image file. I would like to print out the value of each pixel, one per line. I would like the R,G, and B values shown on a scale of 0-255. I have a snippet that almost does this, but returns the RGB values in a format that I am not sure how to convert to the one I need.

For example, the log should look like

0 255 255
255 0 0
etc.

This snippet produces a log file that looks like:

1542,1799,2313,0
1542,1799,2313,0
1542,1799,2313,0

The fact that the output is all the same, and I know the image is not all one color, also has me thinking that it is not moving from pixel to pixel, but just reporting one pixel for each pixel in the image.

Any ideas on how to get a log file with the data that is needed?


#!/usr/bin/perl

use Image::Magick;
my($image, $x);

$image = Image::Magick->new;
$x = $image->Read('file.jpg');
warn "$x" if "$x";

$cols=$image->get('columns');
$rows=$image->get('rows');

for($row=1; $row<=$rows; $row++){
      for($i=1; $i<=$cols; $i++){
            print $image->get("pixel") . "\n ";
      }
}

Open in new window

0
Comment
Question by:stakor
  • 4
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 500 total points
Comment Utility
Did you look at and try the code found at

http://www.perlmonks.org/?node_id=60662

Am thinking you are using get(pixel) incorrectly.
0
 

Author Comment

by:stakor
Comment Utility
Replacing:
print $image->get("pixel") . "\n ";

With:
print $image->get("$r $g $b") . "\n ";

prints about the right number of lines, but they are all blank.
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
Comment Utility
Do you also have


my ($r,$g,$b) = $myImage->rgb($index);?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:stakor
Comment Utility
Ok, switching gears slightly:

#!/usr/bin/perl

use Image::Magick;

my $input = new Image::Magick;
$input->Read ('file.jpg');
($h,$w)=$input->get('height','width');
for $x (0..$h-1){
    for $y (0..$w-1){
      ($r,$g,$b) = split/,/, $input->get("pixel($x,$y)");
       print "$r $g $b \n";
   }
}

Open in new window


This does the printing that I am looking for, but not in the format I am looking for. It is returning values like "1542", and not 0-255. Any ideas on how to get the format changed?
0
 

Author Comment

by:stakor
Comment Utility
Also, as it turns out, it is reporting the same value for every pixel. The image is not all the same color. hmm.
0
 

Author Comment

by:stakor
Comment Utility
Sorry, wrong topic.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now