Accessing Pixel

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
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Accessing Pixel

Post by rohitkrishna »

Hi everyone,

Is there any way with which i can directly access a pixel at a given x,y position in magickwand and change the color of that particular pixel to red.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Accessing Pixel

Post by el_supremo »

This code reads the built-in logo: image and changes the pixel at 200,100 to red.

[EDIT] I've modified the code to use Region Iterator, as in my original version, or to use a DrawingWand as suggested by Anthony.

Pete

Code: Select all

// Change the colour of one pixel in the logo: image
// using either DrawPoint or a RegionIterator

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL;
// Comment out this define to use the region iterator instead of the Draw
#define USE_DRAW
#ifndef USE_DRAW
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	size_t x;
#else
	DrawingWand *dw = NULL;
	PixelWand *fill = NULL;
#endif
	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"logo:");
#ifndef USE_DRAW
	// Get a one-pixel region at coordinate 200,100
	iterator = NewPixelRegionIterator(mw,200,100,1,1);
	pixels=PixelGetNextIteratorRow(iterator,&x);
	// Modify the pixel
	PixelSetColor(pixels[0],"red");
	// then sync it back into the wand
	PixelSyncIterator(iterator);
#else
	fill = NewPixelWand();
	dw = NewDrawingWand();
	PixelSetColor(fill,"red");
    DrawSetFillColor(dw,fill);
	// Uses the current Fill as the colour of the point
	DrawPoint(dw,200,100);
    MagickDrawImage(mw,dw);

#endif
	/* write it */
	MagickWriteImage(mw,"logo_pix.gif");

	/* Tidy up */
#ifndef USE_DRAW
	iterator=DestroyPixelIterator(iterator);
#else
	if(dw)dw = DestroyDrawingWand(dw);
	if(fill)fill = DestroyPixelWand(fill);
#endif
	if(mw) mw = DestroyMagickWand(mw);

	MagickWandTerminus();
}
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Accessing Pixel

Post by rohitkrishna »

Thank you very much el_supremo
el_supremo wrote:This code reads the built-in logo: image and changes the pixel at 200,100 to red.

[EDIT] I've modified the code to use Region Iterator, as in my original version, or to use a DrawingWand as suggested by Anthony.

Pete

Code: Select all

// Change the colour of one pixel in the logo: image
// using either DrawPoint or a RegionIterator

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL;
// Comment out this define to use the region iterator instead of the Draw
#define USE_DRAW
#ifndef USE_DRAW
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	size_t x;
#else
	DrawingWand *dw = NULL;
	PixelWand *fill = NULL;
#endif
	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"logo:");
#ifndef USE_DRAW
	// Get a one-pixel region at coordinate 200,100
	iterator = NewPixelRegionIterator(mw,200,100,1,1);
	pixels=PixelGetNextIteratorRow(iterator,&x);
	// Modify the pixel
	PixelSetColor(pixels[0],"red");
	// then sync it back into the wand
	PixelSyncIterator(iterator);
#else
	fill = NewPixelWand();
	dw = NewDrawingWand();
	PixelSetColor(fill,"red");
    DrawSetFillColor(dw,fill);
	// Uses the current Fill as the colour of the point
	DrawPoint(dw,200,100);
    MagickDrawImage(mw,dw);

#endif
	/* write it */
	MagickWriteImage(mw,"logo_pix.gif");

	/* Tidy up */
#ifndef USE_DRAW
	iterator=DestroyPixelIterator(iterator);
#else
	if(dw)dw = DestroyDrawingWand(dw);
	if(fill)fill = DestroyPixelWand(fill);
#endif
	if(mw) mw = DestroyMagickWand(mw);

	MagickWandTerminus();
}
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Accessing Pixel

Post by anthony »

Thank you very much el_supremo
I second the motion!

I did not really want to go though all the effort of working out the surrounding 'fluff'. It makes for a good example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Accessing Pixel

Post by el_supremo »

I'm glad it helped. I'll add it to my MagickWand examples page later today.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Accessing Pixel

Post by anthony »

Perhaps it should be included in IM sources as a basic example demo.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Accessing Pixel

Post by rohitkrishna »

anthony wrote:Perhaps it should be included in IM sources as a basic example demo.
I think you should....it definetly helps a lot of people...
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Accessing Pixel

Post by rohitkrishna »

el_supremo wrote:This code reads the built-in logo: image and changes the pixel at 200,100 to red.

[EDIT] I've modified the code to use Region Iterator, as in my original version, or to use a DrawingWand as suggested by Anthony.

Pete

Code: Select all

// Change the colour of one pixel in the logo: image
// using either DrawPoint or a RegionIterator

#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
	MagickWand *mw = NULL;
// Comment out this define to use the region iterator instead of the Draw
#define USE_DRAW
#ifndef USE_DRAW
	PixelIterator *iterator = NULL;
	PixelWand **pixels = NULL;
	size_t x;
#else
	DrawingWand *dw = NULL;
	PixelWand *fill = NULL;
#endif
	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"logo:");
#ifndef USE_DRAW
	// Get a one-pixel region at coordinate 200,100
	iterator = NewPixelRegionIterator(mw,200,100,1,1);
	pixels=PixelGetNextIteratorRow(iterator,&x);
	// Modify the pixel
	PixelSetColor(pixels[0],"red");
	// then sync it back into the wand
	PixelSyncIterator(iterator);
#else
	fill = NewPixelWand();
	dw = NewDrawingWand();
	PixelSetColor(fill,"red");
    DrawSetFillColor(dw,fill);
	// Uses the current Fill as the colour of the point
	DrawPoint(dw,200,100);
    MagickDrawImage(mw,dw);

#endif
	/* write it */
	MagickWriteImage(mw,"logo_pix.gif");

	/* Tidy up */
#ifndef USE_DRAW
	iterator=DestroyPixelIterator(iterator);
#else
	if(dw)dw = DestroyDrawingWand(dw);
	if(fill)fill = DestroyPixelWand(fill);
#endif
	if(mw) mw = DestroyMagickWand(mw);

	MagickWandTerminus();
}

I got a doubt...what is value of x ... because in region iterator we generally give the region width as second argument.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Accessing Pixel

Post by el_supremo »

PixelGetNextIteratorRow sets that variable to the number of pixelwands that it returns. In this case I only asked for one pixel and it's in the middle of the image so it can't return zero.
In the general case though you would have to check the value of x after calling PixelGetNextIteratorRow to make sure that it has the number of wands that you are expecting.

Pete
P.S. I've added the example to my MagickWand examples (see the link below)
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
rohitkrishna
Posts: 43
Joined: 2011-10-01T14:34:04-07:00
Authentication code: 8675308

Re: Accessing Pixel

Post by rohitkrishna »

el_supremo wrote:PixelGetNextIteratorRow sets that variable to the number of pixelwands that it returns. In this case I only asked for one pixel and it's in the middle of the image so it can't return zero.
In the general case though you would have to check the value of x after calling PixelGetNextIteratorRow to make sure that it has the number of wands that you are expecting.

Pete
P.S. I've added the example to my MagickWand examples (see the link below)
Thank you...
Post Reply