Reputation: 5971
I am using Imagemagick
to work with images in php
. I am no-wise in ImageMagick
so could not do it. I have 2 Pictures, 1 is background and other one is above it. The one which is on top is of gray color png. While the background image can be any image. I want to set my top image's color to match the most color of background image.
For Example, this is a random background image which has beige/pink as its main color
and this is my top image
I want my above top image to change its color to match the most color of background image, as in the above image, it should be something like
Is it possible?
Upvotes: 0
Views: 551
Reputation: 5971
I have solved it myself through these 2 lines of codes
exec("convert fabric.jpg -scale 1x1\! -format '%[pixel:u]' info:-", $a);
exec('convert arm-shadow.png -fuzz 10% -fill "'.$a[0].'" +opaque black -fill "'.$a[0].'" -opaque black foo.png');
Upvotes: 0
Reputation: 208107
I don't feel like writing and debugging a load of PHP today, but can show you some techniques on the command line that you should be able to translate into PHP.
You can get the mean of the background image by resizing it to a single 1x1 pixel and then printing its value in RGB terms:
convert background.jpg -scale 1x1 -format "%[pixel:p{0,0}]" info:
srgb(219,199,164)
If I take that value and make a solid square out of it, you can see it is a beige colour like you suggest:
convert -size 100x100 xc:"srgb(219,199,164)" mean.png
You can probably use getImageChannelStatistics() in PHP for that.
If I now take that colour, and make it the fill colour for tinting and apply a tint, I get this:
convert top.png -fill "srgb(219,199,164)" -tint 100% result.png
In PHP, you'd be looking at tintImage().
Something horrible has happened down the right side - I don't understand that, but if I extract the opacity from your top image and re-apply it to the result image, it goes away:
convert top.png -alpha extract alpha.pgm
convert top.png -fill "srgb(219,199,164)" -tint 100% alpha.pgm -compose copyopacity -composite result.png
Upvotes: 1