Code Monkey home page Code Monkey logo

color-extractor's People

Contributors

akshay-kh avatar colinodell avatar curtisgibby avatar davechild avatar gdarko avatar gimler avatar grahamcampbell avatar matthecat avatar philsturgeon avatar robbamos avatar robjmills avatar thinksaydo avatar toin0u avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

color-extractor's Issues

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

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 ?

  • Could it be caused by the GD version installed on the machine ? Also perhaps their are difference between the Windows and Unix GD libraries ?
  • Could it be caused by the PHP version in use ?
  • Or are they potential rounded values in the source code which could be different between platforms ?

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

  • Windows 10 Pro 64bits, PHP 7.0.8
  • Centos 7 64bits, PHP 7.0.8

If the problem is linked to GD perhaps doing the same test with #37 could be interesting.

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

Thanks

PHP8 support

Hi, I'm getting an "Image must be a gd resource"

PHP has moved to make GD an object instead of a resource, because GD is now an object, it will never be a resource and this causes an error

General load method

// Excuse my beginner's English
Hi!
What about general load method?

Pseudo code:

load (imagePath)
    switch ext
    case jpg: loadJpeg($imagePath)
    case png: loadPng($imagePath)
    case gif: loadGif($imagePath)

Undefined offset error

Dude, fix getColorCount in Palette class. Something like this:
public function getColorCount($color) { if(array_key_exists($color, $this->colors)) { return $this->colors[$color]; } return 0; }

Get the main colors of each side

Would it be possible to get the main color of top/right/bottom/left sides of an image?

For example, you have a logo and want to match the background automatically with the outside of the logo (if I'd have a Coca-Cola logo, I'd want to use red, not white, even though white might be used more).

Doesn't work with Laravel 5.5

I installed via Composer, then added the following to my config/app.php file:

'provider' => [
......
League\ColorExtractor\Laravel\ColorExtractorServiceProvider::class,
],

'aliases' => [
.......
'ColorExtractor' => League\ColorExtractor\Laravel\ColorExtractorFacade::class,
],

But I get the following error:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'League\ColorExtractor\Laravel\ColorExtractorServiceProvider' not found

Is this a minor error of lightness normalization into colors' sort?

Hello,

according to the article about CIELAB color space from your code:
https://github.com/thephpleague/color-extractor/blob/master/src/ColorExtractor.php#L275
https://en.wikipedia.org/wiki/CIELAB_color_space

area of definition of lightness param (L*/Lstar) is [0:100], where 0 - black, 100 - white. But in colors' sorting algorithm
https://github.com/thephpleague/color-extractor/blob/master/src/ColorExtractor.php#L58

you have follow lightness normalization:
(1 - $labColor['L'] / 200)

If we follow strict classical mathematical rules about any kind of normalization, lightness normalization in your code must be looks like:
(1 - $labColor['L'] / 100)

It turns out that you have an extra 1/2 constant for lightness normalization. I understand that it is not cause any problem with correctness of sort algorithm itself, because you multiply EACH lightness value by this constant and this is why it is still correct. But I do not understand why do you divide L param by 200, not by 100?

question: is over 1gb of memory usage normal?

php memory limit is 1024 MB, the below exception occurs while processing a 12 MB jpeg. I can of course increase the memory limit, but wondered if this level of memory usage is expected?

PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in /var/www/auto/vendor/league/color-extractor/src/League/ColorExtractor/ColorExtractor.php on line 226
PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in /var/www/auto/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php on line 122

Here's my color extraction code:

$primaryPalette = Palette::fromFilename($sFullPath);
$extractor = new ColorExtractor($primaryPalette);
$primaryPalette = null;

$aExtractedColours = $extractor->extract(5);
$extractor = null;

Palette::fromImagick()

Hello

Here is a proposition for Imagick integration in Palette.



    /**
     * @param \Imagick $image
     *
     * @return Palette
     *
     * @throws \InvalidArgumentException
     */
    public static function fromImagick(\Imagick $image)
    {
        if (!is_object($image) || $image instanceof \Imagick === FALSE) {
            throw new \InvalidArgumentException('Image must be an Imagick instance');
        }

        $palette = new self();
        $palette->colors = [];

        $iterator = $image->getPixelIterator();

        foreach($iterator as $row) {

            foreach($row as $pixel) {

                $colorInfo = $pixel->getColor();
                $color = ($colorInfo['r'] * 65536) + ($colorInfo['g'] * 256) + ($colorInfo['b']);

                isset($palette->colors[$color]) ?
                    $palette->colors[$color] += 1 :
                    $palette->colors[$color] = 1;

            }

        }

        arsort($palette->colors);

        return $palette;
    }

Extract stuck in an infinite loop

I have found an issue when I try and extract colours from corrupted images, the extract method seems to get stuck in a loop and eventually times out.

I have attached a faulty image, hopefully it will survive intact.
xhtrlexhqkqy

Library not working

Your library does not work, it is impossible to connect it.
This is the worst experience I've had

Return the score for each color

Would it be possible to return the percentage of the image that each color takes?

I want to show only the meaningul colors of an image, so if a color is only used in 1% of the image (a shadow for example), I wouldn't like to show it.

color results issue

I am getting color results like [15287093,3385170,4359412,16432133,272749]

Calculate shades of black and very light colors as the most used color hex code

Getting the blouse color for other color variations is working very well when I tried to extract the most used color hex code. Sample images:
chiffon-top-green-edited

chiffon-top-purple-edited

However, I have to extract the three most used color hex code to get a shade of black color hex code. Sample image:
chiffon-top-black-edited

Result:
Array
(
[0] => #1BA430
[1] => #FD9D91
[2] => #131313 <----- shade of black
)

As for very light pink, I have to extract at least ten most used color hex code. Sample image:
chiffon-top-pink-edited

Result:
Array
(
[0] => #910011
[1] => #C30F2C
[2] => #DEA184
[3] => #C67864
[4] => #EA214F
[5] => #D98084 <----- shade of pink
[6] => #B85945
[7] => #FD7B6D
[8] => #D5A74F
[9] => #BA6676 <----- shade of pink
)

I even tried changing the background color to black but a shade of pink is still not in the top 3. Sample Image:
chiffon-top-pink-black-bg

Result:
Array
(
[0] => #910011
[1] => #D71F41
[2] => #DFA486
[3] => #000000
[4] => #DB726C <----- shade of pink
[5] => #FCA395 <----- shade of pink
[6] => #B8634F
[7] => #934A55 <----- shade of pink ?
[8] => #BC647A <----- shade of pink
[9] => #D7A566
)

This is a great script and very easy to implement. Just left with this issue and it would be really perfect.

PHP 8.1.5 GD image resources bug?

Hey guys,

I am doing some tutorials on composer and one of the lessons uses the color-extractor package.
All seems to install ok. But I keep getting an error when I try to stream in an image.

This code block seems to cause an issue (line 54-55 - vendor/league/color-extractor/src/League/ColorExtractor/Palette.php):
$image = imagecreatefromstring(file_get_contents($filename));
$palette = self::fromGD($image, $backgroundColor);

As does this one (line 71 same file):
if (!is_resource($image) || get_resource_type($image) != 'gd')

The condition gets met and we see the following error.

Fatal error: Uncaught InvalidArgumentException: Image must be a gd resource in /Users/ufmurphy/Sites/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php:72 Stack trace: #0 /Users/ufmurphy/Sites/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php(55): League\ColorExtractor\Palette::fromGD(Object(GdImage), NULL) #1 /Users/ufmurphy/Sites/index.php(19): League\ColorExtractor\Palette::fromFilename('images/bricks.j...') #2 {main} thrown in /Users/ufmurphy/Sites/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php on line 72

Doing some sleuthing, it looks like a change in how GD images are handled in PHP8.
https://php.watch/versions/8.0/gdimage

Is there a more recent version than 0.3.2?

Thanks,
Jim

Allow to retrieve `hsl` or `cmyk` from a color ?

Hello,

This piece of code is really cool :)
Do you think it can be interesting to add methods in the Color object to mutate from RGB to HSL or even CMYK ?

If so I can make a PR 😄

Thanks !

Map int result to strings.

Hi,

I want to map the Int result that give me this library to for example:

red
green
back
white

To create a basic filter of image main color.
covering all the range of colors.

How can i do it?

Alpha channel of .png is taken into account

Hello and congratulations for this nice piece of code !

I am trying to work with some .png files and this one particularly fails and returns me #000000 as the most vibrant color.
capture d ecran 2016-03-17 a 20 38 32

I guess it fetches it from the alpha channel.

Could you have a more in-depth look please ? Here is the image
interval_contentsc

Could have better results?

Using this image it gave me (in order):

  • #364B84
  • #FEA700
  • #C37872
  • #DEAC87
  • #8C5155

Using TinyLabs I got:

  • #af8fa7 36.4%
  • #735873 21.4%
  • #d2b2a8 19.8%
  • #646a98 12.0%
  • #453548 8.9%

I don't want to sound negative, but those results are much better. How can we improve this library to get more accurate colours?

Better Palette generation

I love the interface of this package. However, the palette that it generates contains the color value of every pixel it seems, even if the colors are nearly indistinguishable. I would love to be able to find the 5 most used colors that are actually in different color spectrums instead of 5 colors that are practically the same color.

I have been using this package to find a good color palette https://github.com/ksubileau/color-thief-php but its interface is not quite as nice as this one.

Execution takes longer than 60 seconds for a single-colored logo

Basically, I use your script to extract the 5 most used colors from a logo the the user can upload to my server. It's working fine with multi-colored images but takes more than 60 seconds for single or double-colored images.

Works fine:
logo-bs-multimedia

Takes longer than 60 seconds:
logo-haarpunkt

Call to undefined function

FatalThrowableError in Palette.php line 54:
Call to undefined function League\ColorExtractor\imagecreatefromstring()

Webp image is not supported

When feeding imagecreatefromstring a webp image, the following error occurs.

imagecreatefromstring(): Data is not in a recognized format in /var/www/html/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php:54

How to filter balck color ?

I am detecting most representative color from an image but I also want to detect if detected color is white or black. Is there any way we can do that ?

How to use

Hi,

how i can use this Color Extractor? could you please add a simple help?

How to use this without Composer

Hi,
I have no idea what Composer is. I do not have command line access on my server.
Is there a way to use your package without Composer?
Thanks

speed

Is it me or is the calculation happening very slow? Nice script by the way!

Error on Ubuntu 22.04 and OpenSSL 3.0.2

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:0A000126:SSL routines::unexpected eof while reading

SSL routines::unexpected eof while reading at Palette.php:55

to text

Is it possible to map the hexcode to some human text ?
red , green , blue ..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.