Page 1 of 2

how to dig a hole of specified size from a JPG image

Posted: 2018-11-27T06:05:54-07:00
by dxr528
I want to dig a hole of specified size from a JPG image specified location to generate a PNG image. In order to reduce the size of the image, the PNG should be 8 bit. Thank you.The sample diagram is as follows:
before:Image
after:Image

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-27T07:14:31-07:00
by snibgo
What version of IM, on what platfom?

I assume you want 8 bits/channel/pixel, which JPG is anyway.

Code: Select all

magick in.jpg -region 300x400+20+30 -alpha transparent +region -depth 8 out.png

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-27T18:20:00-07:00
by fmw42
Please do not send messages as warnings. I have deleted your warning and posted it here:
Thank you very much!The version I used was 6.9.1-10, and I used this command:
magick in.jpg -region 300x400+20+30 -alpha transparent +region -depth 8 out.png
igenerate a fully transparent image, not the result I wanted.Would you please see what the problem is?

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-27T18:57:26-07:00
by snibgo
"magick" is a v7 command, but you have v6, so I'm surprised that works at all.

In some versions, "-region" didn't work. This longer command does the same job:

Code: Select all

convert in.jpg ( +clone -fill White -colorize 100 -fill Black -draw "rectangle 20,20,320,430" ) -alpha off -compose CopyOpacity -composite -depth 8 out.png

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-27T21:39:03-07:00
by dxr528
snibgo wrote:
2018-11-27T18:57:26-07:00
"magick" is a v7 command, but you have v6, so I'm surprised that works at all.

In some versions, "-region" didn't work. This longer command does the same job:

Code: Select all

convert in.jpg ( +clone -fill White -colorize 100 -fill Black -draw "rectangle 20,20,320,430" ) -alpha off -compose CopyOpacity -composite -depth 8 out.png
Thank you so much!This works!How do I implement this with the MagickWand C API?

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T06:08:25-07:00
by snibgo
dxr528 wrote:How do I implement this with the MagickWand C API?
By starting small. Find the C MagickWand functions that correspond to the CLI operations: read, colorize, draw, composite, write. Then write the program.

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T06:26:24-07:00
by dxr528
snibgo wrote:
2018-11-28T06:08:25-07:00
dxr528 wrote:How do I implement this with the MagickWand C API?
By starting small. Find the C MagickWand functions that correspond to the CLI operations: read, colorize, draw, composite, write. Then write the program.
// +clone -fill White -colorize 100
MagickWand *im_clone = NewMagickWand();
im_clone=CloneMagickWand(larg->img);
PixelWand* tone = NewPixelWand();
PixelSetColor(tone, "white");
PixelWand* opacity2 = NewPixelWand();
PixelSetColor(opacity2, "rgb(100%,100%,100%)");
MagickColorizeImage(im_clone, tone, opacity2);
DestroyPixelWand(tone);
DestroyPixelWand(opacity2);

// -fill Black -draw "rectangle 20,20,320,430"
PixelWand *p_wand = NULL;
DrawingWand *d_wand = NULL;
p_wand = NewPixelWand();
d_wand = NewDrawingWand();


PixelSetColor(p_wand,"Black");
DrawSetFillColor(d_wand,p_wand);
DrawRectangle(d_wand, 20,20,320,430 );
MagickDrawImage(im_clone, d_wand);

DestroyPixelWand(p_wand);
DestroyDrawingWand(d_wand);


// -alpha off -compose CopyOpacity -composite -depth 8
// ActivateAlphaChannel, DeactivateAlphaChannel, OpaqueAlphaChannel, or SetAlphaChannel.

MagickSetImageAlphaChannel(larg->img, OpaqueAlphaChannel);
MagickCompositeImage(larg->img,im_clone,CopyOpacityCompositeOp,0,0);


int ret = MagickSetImageDepth(larg->img, 8 );

my code is like that,but it do not work.please help me. thank you.
how to change command -alpha off to MagickWand in V6?

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T07:01:08-07:00
by snibgo
The CLI "-alpha off" operates on all the images in the current list, not just one.

When debugging any image processing, it is useful to write image files at the different stages, eg im_clone after each process. Then, when the result is wrong, you can find which part is wrong.

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T07:15:30-07:00
by dxr528
snibgo wrote:
2018-11-28T07:01:08-07:00
The CLI "-alpha off" operates on all the images in the current list, not just one.

When debugging any image processing, it is useful to write image files at the different stages, eg im_clone after each process. Then, when the result is wrong, you can find which part is wrong.
i saved im_clone to local img file.just like this:
Image

and the last png file like this:
Image

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T08:30:51-07:00
by snibgo
Why are they different sizes?

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T08:57:00-07:00
by dxr528
snibgo wrote:
2018-11-28T08:30:51-07:00
Why are they different sizes?
sorry ,i copy the small size img. i have upload the full size img. where is the problem with my code ?

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T11:13:47-07:00
by snibgo
"-alpha off" deactivates the alpha channel, so CopyOpacity will copy from the intensity, not from the alpha channel. By contrast, setting alpha to opaque activates the alpha channel, setting all pixels opaque, and that's not what you want.

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-28T21:43:13-07:00
by dxr528
snibgo wrote:
2018-11-28T11:13:47-07:00
"-alpha off" deactivates the alpha channel, so CopyOpacity will copy from the intensity, not from the alpha channel. By contrast, setting alpha to opaque activates the alpha channel, setting all pixels opaque, and that's not what you want.
// 1 add mask :convert before.jpg -fill black -colorize 30% result.jpg
PixelWand *colorize = NewPixelWand();
PixelWand *opacity = NewPixelWand();
PixelSetColor(colorize,"black");
PixelSetColor(opacity, "rgb(30%,30%,30%)");
MagickColorizeImage(larg->img,colorize,opacity);
DestroyPixelWand(colorize);
DestroyPixelWand(opacity);
MagickSetImageCompressionQuality(larg->img, 75);

// 2 hollow : convert result.jpg \( +clone -fill White -colorize 100 -fill Black -draw "rectangle 20,20,320,430" \)
// -alpha off -compose CopyOpacity -composite -depth 8 out.png

// +clone -fill White -colorize 100
MagickWand *im_clone = NewMagickWand();
im_clone=CloneMagickWand(larg->img);
PixelWand* tone = NewPixelWand();
PixelSetColor(tone, "white");
PixelWand* opacity2 = NewPixelWand();
PixelSetColor(opacity2, "rgb(100%,100%,100%)");
MagickColorizeImage(im_clone, tone, opacity2);
DestroyPixelWand(tone);
DestroyPixelWand(opacity2);

// -fill Black -draw "rectangle 20,20,320,430"
PixelWand *p_wand = NULL;
DrawingWand *d_wand = NULL;
p_wand = NewPixelWand();
d_wand = NewDrawingWand();


PixelSetColor(p_wand,"Black");
DrawSetFillColor(d_wand,p_wand);
DrawRectangle(d_wand, x,y,x+w,y+h);
MagickDrawImage(im_clone, d_wand);

DestroyPixelWand(p_wand);
DestroyDrawingWand(d_wand);

// -alpha off -compose CopyOpacity -composite -depth 8
// ActivateAlphaChannel, DeactivateAlphaChannel, OpaqueAlphaChannel, or SetAlphaChannel.

MagickSetImageAlphaChannel(im_clone, DeactivateAlphaChannel);
MagickSetImageAlphaChannel(larg->img, DeactivateAlphaChannel);

MagickSetImageDepth(larg->img, 8 );
MagickSetImageDepth(im_clone, 8 );
MagickSetFormat(larg->img, "png");

int ret = MagickCompositeImage(larg->img,im_clone,CopyOpacityCompositeOp,0,0);

Thank you for your suggestion. After modifying the code, it is ok now.But now the generated PNG image size is relatively large. Is there any way to compress it?I hope to get your help.thank you.
this is the final png img.the size is 282Kb.

Image

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-11-29T08:29:19-07:00
by snibgo
pngcrush reduces the size by 27%.

Re: how to dig a hole of specified size from a JPG image

Posted: 2018-12-05T19:00:00-07:00
by dxr528
snibgo wrote:
2018-11-29T08:29:19-07:00
pngcrush reduces the size by 27%.
Hello, if the original picture is a PNG transparent picture, how can I dig it out and Keep the original transparency?