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.