Imagemagik or GD?

Does anyone know if it is possible to use imagemagik or GD colorize only a defined portion of an image?

I’ve found imagik (colorize image) which colors the image but I need to use 4 different colors instead of one and define the parts of the image to change.

Any ideas?

define the parts of the image to change.

A square a circle or what - how are you defining it?

Imagemagick can probably do it with a mask but without more information I am only guessing. Your best bet is to go to the forum and post a question with an example input and output image.
Also if you are restricted to a specific version of imagemagick post that as well.

This is using a simple region to change a square in the middle of an image to grayscale and there is no need for a mask.

Out of interest I am currently working on a simple jquery interface on my website that lets you pick points to distort an image and I would think the same method could be used to define an area.

The image is rectangular. It is divided into 6 portions. Each portion would have it’s own unique color that I would pass into the function as a variable. So if we can imagine a rectangle with 6 squares, in 3 columns in 2 rows. Each square needs its own color.

I do not use Imagick but it could be done; this is an Imagemagick with exec( ) example:

$cmd = " -size 200x185 xc:none ". 
" -fill grey39 -draw \\"rectangle 47,0 102,20\\" ". 
" -fill grey39 -draw \\"rectangle 157,0 176,185\\" ". 
" -fill red -draw \\"rectangle 0,20 200,75\\" ". 
" -fill silver -draw \\"rectangle 10,36 167,145\\" ". 
" -fill blue -draw \\"rectangle 130,0 150,185\\" ". 
" -fill brown -draw \\"rectangle 72,110 200,137\\" ". 
" -fill grey26 -draw \\"rectangle 0,99 56,185\\" ". 
" -fill grey39 -draw \\"rectangle 81,102 120,185\\" ". 
" -fill green -draw \\"rectangle 30,164 200,177\\" "; 

exec("convert $cmd rectangle.gif"); ?> 

You can still use php for the coordinates and colours:

" -fill $colour1 -draw \\"rectangle $points1\\" ". 

You could draw individual rectangles and join them together with -append and +append

exec(" convert -size 20x20 xc:red top_left.jpg ");
exec(" convert -size 20x20 xc:blue top_right.jpg ");
exec(" convert top_left.jpg top_right.jpg +append output.jpg");

Or you could do the above in one go and use layers.

This is a great start. One thing I need to do is to honor the images existing transparency so that the transparency is not overwritten. I am able to do that with this code. However, as you can see in the example it only converts the entire image to black and does not utilize the multiple colors in the defined areas.

$im = new Imagick('rectangle.png');
header('Content-type: image/png');
echo $im;

Is there a way to tie what I have and what you have together?

Sorry I do not use Imagick and I am now away.

One suggestion (I’ve never used IM but I’m sure what I’m about to describe is possible): Duplicate the image as many times as you have sections. Apply your colour and transparency stuff to each whole image. Then crop and combine into one new image after colour and transparency manipulation.

That is one way of thinking. However, there are many colors that can be chosen so much so that there can be up different combinations of 20661046784 images generated. It’s better in this case to define the sections and then transmit the color. to that section. The issue being that I need to use the ability of imagick to keep transparency but also define sections such as the imagemagick example from Rubble.

Eh? Is it 6 or 20661046784? All I’m suggesting is instead of trying to do your 6 different colour manipulations constrained to 6 different areas of the image, do it to 6 separate whole images, then crop and assemble those 6 images appropriately to end up with one image.

I see what you mean. It’s 6 sections, but any of those sections can be any color in the color wheel. So you can have thousands of different combinations is what I mean.

I don’t understand. So what? And what’s the difference, in terms of numbers, between the sequence of steps you’ve sketched and the sequence of steps I’ve sketched?

What I’ve described, I mean do it programmatically at the moment it’s required. It’s just a slightly alternative way to do what you described, one which avoids, goes around, the issue of not being able to apply colour and transparency manipulations to a constrained area of the image.

You understand you can duplicate and crop and combine images programmatically? I’m sure IM will allow you to do that. GD certainly does.

I thought imagemagick is a PECL extension and aint available on shared hosts, is this right? If so, you have to run your own server to use it. If you are making a software targeting clients that are mostly on shared hosts, dont use imagemagick either.