thephpleague / color-extractor Goto Github PK
View Code? Open in Web Editor NEWExtract colors from an image like a human would do.
Home Page: thephpleague.com
License: MIT License
Extract colors from an image like a human would do.
Home Page: thephpleague.com
License: MIT License
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 #37 could be interesting.
In any case I the problem cannot be solved IMO it should be described and explained in the README.
Thanks
Issue:
In laravel 5 when the ServiceProvider is loaded it throws BadMethodCallException for use of undefined method [package].
Fix:
Commenting out the boot() section of the ServiceProvider works fine.
Hi @MatTheCat
I would like to contribute but I wonder if I should host those providers on my account or php-loep should host them. WDYT ?
Should switch service provider to singleton() method. See pull request #34 for fix @GrahamCampbell
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
// 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)
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; }
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).
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
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?
Hi, now the service providers have been created can you create a new tag so these can be used?
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;
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;
}
imagedestroy() is never called in the file League/ColorExtractor/Palette.php preventing PHP from freeing the memory previously allocated at the line 55 of the same file.
Your library does not work, it is impossible to connect it.
This is the worst experience I've had
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.
cpu Usage 20-30%,How to solve?
Using Palette::fromGD
with Image Intervetion throw exception "Image must be a gd resource"
Tried with $image->getCore()
(https://image.intervention.io/v2/api/get-core) that returns a GdImage
object.
I am getting color results like [15287093,3385170,4359412,16432133,272749]
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:
However, I have to extract the three most used color hex code to get a shade of black color hex code. Sample image:
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:
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:
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.
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
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 !
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?
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.
The statsd package is a great example:
FatalThrowableError in Palette.php line 54:
Call to undefined function League\ColorExtractor\imagecreatefromstring()
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
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 ?
Would be nice if i could pass an image stream, directly from memory instead of dumping the file to disk, and then process it via the color-extractor.
Hi,
how i can use this Color Extractor? could you please add a simple help?
Hi,
Got an infinite loop on ligne 129 of Image.php for some images.
Image size is 50*50.
As you can see on my PR, this #25 fix the thing.
Any idea ?
Thx
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
Is it me or is the calculation happening very slow? Nice script by the way!
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
Is it possible to map the hexcode to some human text ?
red , green , blue ..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.