SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Change Color of image using Imagick

    Hi all!

    So I have an image that has various shades of grey (I converted it to grayscale). And what I would like to allow the user to do is convert the image to a different color by selecting from a color picker (applying a hex code basically).

    PHP Code:
    $shading = new Imagick($this->shading);
    $shading->colorizeImage("#5780bd",1);
    $this->blank->compositeImage($shadingImagick::COMPOSITE_DEFAULT0,0); 
    Where $this->shading is the grayscale image and $this->blank is an image with just a white background.

    The code does change the color of the grayscale image; but it doesn't do it 100% correctly. For example, if I open photoshop and use the color code #5780bd and change the shading image to that color by using the pencil tool or w/e, the image color is noticeably different.

    If anyone has any advice on how to accomplish coloring a gray-scale image to a hex code then that would be great. I'm not sure whether the original image should be grayscale or if it should be black or white or anything in order to achieve correct colorization.

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I guess no one knows

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Well, first off, you are comparing applies to oranges. It shouldn't come as much of a surprise that Imagick and Photoshop will do them differently.

    Likewise, GD can do this too:
    http://www.sitepoint.com/forums/show...hades-are-gone

    Have you tried altering your input color to Imagick to see if you can get to the color Photoshop produces?

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the reply! I was just wondering if there was a simple effect that would allow me to do what photoshop allows.

    I managed to boost the RGB values of the color and was able to get the correct output; but obviously I can't do this for every single individual color the user inputs without knowing some kind of formula. If my original image is grayscale, and the user inputs a hex code for their color, is there a formula that can be used to calculate what values of the RGB input color need to be boosted in order to get the grayscale correctly colored?

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Patche View Post
    Hi,

    Thanks for the reply! I was just wondering if there was a simple effect that would allow me to do what photoshop allows.

    I managed to boost the RGB values of the color and was able to get the correct output; but obviously I can't do this for every single individual color the user inputs without knowing some kind of formula. If my original image is grayscale, and the user inputs a hex code for their color, is there a formula that can be used to calculate what values of the RGB input color need to be boosted in order to get the grayscale correctly colored?
    Definitely. Can you give me the new value you used to get to the color you wanted? Then I'm sure several of us can devise algorithms to do that boost for you.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    You are only overlaying a colour over the gray image which is probably not the best method.
    I can not use Imagick as I do not have it installed anywhere but you may be able to modify some Imagemagick code there may be something useful on the Imagemagick forum.

    An input image and the desired results are always helpful when dealing with image questions.

    A test I tried from one of the forum posts using Imagemagick:
    gray.jpgout.png

  7. #7
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hi guys!

    Thanks for getting back to me. I decided to try and use the command tools to get what I wanted (after being recommended to do so). And the commands seem to do exactly what I needed:

    Code:
    $cmd = "convert  -size 600x400 xc:none \( wolf_blue.png -fill rgb\(51,13,13\) -colorize 100% \) -composite ";
    $cmd .= "\( wolf_shade.png -fill rgb\(213,104,104\) -colorize 100% \) -composite ";
    It allows me to shade the layer to exact color without blending the colors at all. Is this not possible with the PHP implementation? Whenever I used the PHP function for colorizeImage it would make the colors all different compared with the command line version which always produces the correct colors.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Interesting. Not sure if this helps, but I just found this, which says, using clutImage provided better results than colorizeImage.
    http://stackoverflow.com/questions/8...age-hex-darker (last answer)

  9. #9
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Imagick is a limited implementation of Imagemagick and is not well supported. As it is a class they have made "assumptions" of what the user might want to do and have set some option defaults and combined some options into one.

    I always use Imagemagick command line with php - see my signature - and you then have access to the full range of Imagemagick options.

    Is this not possible with the PHP implementation?
    I would guess you could do that with Imagick but I am not an expert with Imagick and as I say and I have no way of testing it. You would need to split it up into the individual steps.

  10. #10
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Okay thank you guys! I think I will stick with the command line approach then!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •