set pixel to be transparent with VS2005

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?".
Post Reply
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

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.

Code: Select all

/*	
	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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

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.

Can you post the URL of an example image?

Pete
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

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:

Code: Select all


	MagickWandGenesis();
	magick_wand = NewMagickWand();
	MagickReadImage(magick_wand,"input.tga");
	MagickWriteImage(magick_wand,"output.png");
	/* Clean up */
	if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
	MagickWandTerminus();
Pete
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Also make sure the in memory image has a matte channel to hold the transparency!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

I can't open either of those images - the page gives me "Forbidden
Error 403".

Pete
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

I'm curious - how did you fix the problem?

Pete
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

The 403 forbidden problem with those images remain!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

See IM examples, Replace areas of colors (floodfill)
http://www.cit.gu.edu.au/~anthony/graph ... #floodfill

Yes no real examples yet, but should give you the ideas.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply