Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Can i Compare two images in perl ? (It's a myth!)

by BrowserUk (Pope)
on May 30, 2011 at 22:50 UTC ( #907388=note: print w/replies, xml ) Need Help??


in reply to Re: Can i Compare two images in perl ?
in thread Can i Compare two images in perl ?

This is one of those internet myths that just persists.

According to your formula, this image and this one ought to approach 2.0. They are to all intents and purposes identical.

The actual result:

C:\test>jimagezipcmp.pl adding: 1.png (160 bytes security) (deflated 90%) adding: 2.png (160 bytes security) (deflated 89%) adding: 1.png (160 bytes security) (deflated 90%) adding: 2.png (160 bytes security) (deflated 89%) 1.0272952853598

Try it yourself:

#! perl -slw use strict; use GD; sub rgb2n { unpack 'N', pack 'CCCC', 0, @_ } for my $n ( 1, 2 ) { my $im1 = GD::Image->new( 600, 400, 1 ); $im1->filledRectangle( 0, 0, 600, 400, rgb2n( 255, 255, 255 ) ); $im1->filledRectangle( 100, 100, 500, 300, rgb2n( 255 - $n, 0, 0 ) + ); open O, '>:raw', $n . '.png' or die $!; print O $im1->png; close O; } system q[zip 1.zip 1.png]; system q[zip 2.zip 2.png]; system q[zip 3.zip 1.png 2.png]; my $size1 = -s '1.zip'; my $size2 = -s '2.zip'; my $size3 = -s '3.zip'; print +( $size1 + $size2 ) / $size3;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Can i Compare two images in perl ? (It's a myth!)
by Anonymous Monk on May 31, 2011 at 13:08 UTC

    Those are PNGs, not uncomressed bitmaps.

    I certainly wouldn't expect that to work.

      Okay. I saved those same two pngs as bmps; deleted the 3 old zips; and then compressed the bmps per your post:

      C:\test>dir ?.bmp 31/05/2011 14:33 720,054 1.bmp 31/05/2011 14:33 720,054 2.bmp 2 File(s) 1,440,108 bytes C:\test>del 1.zip 2.zip 3.zip C:\test>zip 1.zip 1.bmp adding: 1.bmp (160 bytes security) (deflated 100%) C:\test>zip 2.zip 2.bmp adding: 2.bmp (160 bytes security) (deflated 100%) C:\test>zip 3.zip 1.bmp 2.bmp adding: 1.bmp (160 bytes security) (deflated 100%) adding: 2.bmp (160 bytes security) (deflated 100%) C:\test>dir ?.zip 31/05/2011 14:34 2,376 1.zip 31/05/2011 14:35 2,376 2.zip 31/05/2011 14:35 4,730 3.zip

      (2376 + 2376) / 4370 = 1.0874141876430205949656750572082.

      Still nowhere near the 2.0 you would expect if this worked.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Concatenating the bmps doesn't help much either. I got 1.12

        Perhaps we need a theoretical perfect compression algorithm?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907388]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-06-27 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (601 votes). Check out past polls.