IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.

I'm trying to remove the green background using IMagick because I have way to many pictures and I want to make it "automatic". I'm not familiar with IMagick but I still managed to see some results using what I've found on Google.

So, I'm joining a picture of what I managed to achieve with my "noob" script and I would like to have some advices to improve it.

Pad your image with a 1 pixel border of the green color before processing. Then use one of the flood fill methods from the corners. That way it will fill in between the legs. If you want further help, then post one of your input images.

I'm on Windows, it seems a bit tricky to use your script on it. And the "production" server is on Linux.

But, I'm not planning to use it for commercial purposes. I just need to crop and remove the green screen of 3000 images quickly. I could do it manually with Paint.NET for example but this would take way too many times.

What is frustrating is that i'm so close to the perfect result using "clean" PHP but yeah, like I said, when you zoom in...

Light skin or clothing with a green background shows a mixed colour at edges for two reasons: (a) camera pixels have a non-zero area, so some will straddle the skin and the background, and (b) white skin that is nearly co-planar with the lens acts as a mirror, reflecting the background, so it "picks up" the green colour.

To remove the green, a simple method is to erode the mask by a couple of pixels, and perhaps blur it. This "shrinks" the person, which may be a problem.

A more complex method is to process every pixel near an edge: guess at the green background colour (assume it is the same as the nearest pure background), and guess either the correct skin colour or the effective transparency. From these two values we can calculate the third. Thus, we can replace pixels with the correct skin colour and transparency. We might then threshold the transparency, perhaps by distance from the edge, because skin that has only "picked up" the green should be opaque.

Thanks for the information but I won't be able to fix it by myself, I don't really know how to work with images. The results i'm looking for should be similar to this website ( https://www.remove.bg ), sadly, this is not free.

Little update, I managed to make your script work on the Linux server, the "human" is colorized and the background is in in black/grey, it is not deleted. It looks good but this is not the behavior i'm looking for of course

If I set the background to white, the image is fully white, everything disappears.

Post the image and I will process it and tell you the arguments. Note that if you want a transparent background, then you need to output to PNG. If you want a white background or some other color, then use the -b bgcolor argument -b white. Then you can save to JPG.