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

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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
Last edited by dxr528 on 2018-11-28T08:55:42-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

Why are they different sizes?
snibgo's IM pages: im.snibgo.com
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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 ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

pngcrush reduces the size by 27%.
snibgo's IM pages: im.snibgo.com
dxr528
Posts: 9
Joined: 2018-11-27T05:55:13-07:00
Authentication code: 1152

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

Post 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?
Post Reply