Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
You can actually do that with the command line "convert" program but if you must use visual studio here is a C function which will do what you want using one of two methods. Change the filenames, coordinate and colours as required.
/*
Use either MagickMatteFloodfillImage or MagickPaintTransparentImage
to make all the selected pixels transparent and write as a png.
*/
#include <windows.h>
#include <wand/magick_wand.h>
void test_wand(void)
{
// If flood is non-zero use FloodFill, otherwise use PaintTransparent
int flood = 0;
MagickWand *magick_wand = NULL;
// Set default fuzz to zero (see below)
double fuzz = 0.;
MagickWandGenesis();
magick_wand = NewMagickWand();
MagickReadImage(magick_wand,"eqn9_rings_040.jpg");
MagickStripImage(magick_wand);
// Both Floodfill and PaintTransparent force the matte on so if you start with
// a JPG (for example) you don't need to force the matte on.
// In order to change the selected pixels to be fully transparent I had to set
// the "opacity" argument to 255 which means fully opaque not fully transparent.
// This was true for both MagickMatteFloodfillImage and MagickPaintTransparentImage
// I've set the fuzz to 20 for my image but you can change or remove
// this as needed
fuzz = 20.;
// A Floodfill only affects pixels in one contiguous area
// Flood the area that is contiguous with coordinate (50,0) and has the
// same colour to within the fuzz factor
// In this case I know that the coordinate is black and I want that one
// contiguous area filled with transparency
// MagickBooleanType MagickMatteFloodfillImage(MagickWand *wand,
// const Quantum opacity,const double fuzz,const PixelWand *bordercolor,
// const long x,const long y)
if(flood)MagickMatteFloodfillImage(magick_wand,255,fuzz,NULL,50,0);
else {
PixelWand *target;
target = NewPixelWand();
PixelSetColor(target,"black");
// A PaintTransparent affects all pixels of a given colour
// This specifically targets all pixels having the colour specified
// by the "target" wand and changes their opacity to the specified
// value - EXCEPT that I had to specify an opacity of 255 to get
// full transparency
// MagickBooleanType MagickPaintTransparentImage(MagickWand *wand,
// const PixelWand *target,const Quantum opacity,
// const double fuzz)
MagickPaintTransparentImage(magick_wand,target,255,fuzz);
DestroyPixelWand(target);
}
MagickWriteImage(magick_wand,"eqn9_trans_040.png");
/* Clean up */
if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
MagickWandTerminus();
}
Pete
I used your algorithm but the black pixels are still black. I can't understand why...
If you are using the MagickPaintTransparentImage part of the code then perhaps the black area you are trying to affect is not within the 20 fuzz factor of a pure black which is what I specified in the code. A colour like, for example, rgb(38,0,0) will look black on the screen but will not match. You would have to modify the target PixelWand colour and the fuzz factor to suit your image.
Is there some method to set the transparency pixel by pixel?
That's basically what MagickPaintTransparentImage does.
I had never used a tga before but I borrowed a tga image from Unreal Tournament. I think your tga file will have an image and an alpha layer. If you just convert the image directly to a PNG you should get the image you want.
You can either use the ImagaMagick convert program "convert input.tga output.png" or change the program so that it just does: