thephpleague / color-extractor

Extract colors from an image like a human would do.
thephpleague.com
MIT License
1.3k stars 159 forks source link

Palette.getMostUsedColors() getting different values depending on OS / PHP version #45

Open bgaillard opened 8 years ago

bgaillard commented 8 years ago

Hi, I just started to use color-extractor in one or our customer's project.

We wrote a unit test to check our code and it appears the Palette.getMostUsedColors() function is not returning de same results depending on the OS and/or PHP version in use (perhaps this could also be dependent on the GD version which is installed on the machine).

For exemple on a Unix machine we get this array (the limit parameter is equal to 12 and the index of this array has been converted to hexadecimal).

[
    '#000000' => 93285,
    '#010000' => 7871,
    '#020001' => 6022,
    '#010101' => 3413,
    '#040000' => 3321,
    '#000002' => 2070,
    '#050100' => 2036,
    '#030000' => 1945,
    '#000100' => 1789,
    '#060000' => 1668,
    '#020100' => 1604,
    '#634D40' => 1263,
]

On a Windows machine and with the same image we get the following values.

[
    '#000000' => 93135,
    '#010000' => 7710,
    '#020001' => 6001,
    '#010101' => 3377,
    '#040000' => 3227,
    '#000002' => 2056,
    '#050100' => 2052,
    '#000100' => 1842,
    '#030000' => 1747,
    '#020100' => 1580,
    '#060000' => 1557,
    '#634D40' => 1264,
]

We only encounter this problem with one testing JPEG file (I can provide you the file if needed).

We also have similar unit test with GIF and PNG files, with those formats we do not encounter any problem.

So do you know what could cause differences in our case ?

If it can help here are much more details about each testing envirronment

If the problem is linked to GD perhaps doing the same test with https://github.com/thephpleague/color-extractor/issues/37 could be interesting.

In any case I the problem cannot be solved IMO it should be described and explained in the README.

Thanks

MatTheCat commented 8 years ago

I witnessed a similar problem between GD and Imagick on the same environment. Sadly I have no idea if it's related or what is causing this.

bgaillard commented 8 years ago

@MatTheCat Thanks for those additional informations, I also encounter diffrencies with the https://github.com/jenssegers/imagehash library.

I'm not 100% sure but in my opinion JPEG files (and perhaps other kind of files) are not treated the same way depending on the OS / PHP / GD versions in use.

For example this Stackoverflow post http://stackoverflow.com/questions/10647266/image-file-size-differences-between-imagemagick-and-gd-library indicates.

It turns out that JPEG quality scales are not defined in a specification or standard, and they are not uniform across encoders. A quality of 60 in Photoshop might be the same as a quality of 40 in one program, quality B+ in another and quality fantastico in a third. In my tests, I found that Photoshop’s 60 is closest to -quality 82 in ImageMagick.

Perhaps this issue is also similar https://github.com/Intervention/image/issues/355.

Does anyone know if this supposition is correct or have much more informations about those differencies ?

MatTheCat commented 8 years ago

I don't think the problem comes from compression as I saw a difference between GD and Imagick with the exact same image.

MelnikDima commented 5 years ago

Handled the similar problem that Imagemagic and gd calculates different number of unique colors imagemagic(279123) vs gd(279069) on the exact same image (difference on a lot of tested images is approximately 100)

MatTheCat commented 5 years ago

@MelnikDima how did you handle it?

MelnikDima commented 5 years ago

@MatTheCat something like $image->getImageColors(); // unique colors by Image magic

$palette = Palette::fromFilename($file); $palette_count = count($palette); // unique_colors by gd

MatTheCat commented 5 years ago

Maybe the best course would be to just ignore these differences :thinking: