I need to be able to write some text automatically inside an image. According to the image lightness, the script must write in white or black.
So how do I check the lightness/darkness of an image with Imagick?
You could do something like this:
// Load the image
$imagick = new Imagick("image.jpg");
// convert to HSL - Hue, Saturation and LIGHTNESS
$imagick->transformImageColorspace(imagick::COLORSPACE_HSL);
// Get statistics for the LIGHTNESS
$Lchannel = $imagick->getImageChannelMean(imagick::CHANNEL_BLUE);
$meanLightness = $Lchannel['mean']/65535;
printf("Mean lightness: %f",$meanLightness);
If you want to do undercoloured text, per Fred's suggestion, you can do that in PHP with:
$image = new Imagick("image.jpg");
$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFontSize(24);
$draw->setTextUnderColor('#ff000080');
$image->annotateImage($draw,30,50,0,"Undercoloured Text");
$image->writeImage('result.jpg');
You could also just create a text image on some background color and overlay that on the image. Or use -undercolor with -draw or -annotate. That way, you do not have to worry about the color of the image. Or you could specify the region where you want to write text over, then get the average lightness of that region. Then test if the region is brighter or darker than mid-gray. If brighter, then create a text image of the same size with transparent background and use black text color. Similarly if darker, use white text color. So in ImageMagick command line, these would be:
Input:
Pink Undercolor:
convert logo.png \
\( -size 110x -background pink -font ubuntu-bold -fill $textcolor label:"Testng" \) \
-gravity northwest -geometry +395+400 -compose over -composite result3.png
Testing (dark region) - Unix syntax:
test=`convert logo.png -crop 110x36+395+400 +repage -colorspace gray -format "%[fx:(mean>0.5)?1:0]" info:`
if [ $test -eq 1 ]; then
textcolor="black"
else
textcolor="white"
fi
convert logo.png \
\( -size 110x -background none -font ubuntu-bold -fill $textcolor label:"Testng" \) \
-gravity northwest -geometry +395+400 -compose over -composite result1.png
Testing (bright region):
test=`convert logo.png -crop 110x36+100+400 +repage -colorspace gray -format "%[fx:(mean>0.5)?1:0]" info:`
if [ $test -eq 1 ]; then
textcolor="black"
else
textcolor="white"
fi
convert logo.png \
\( -size 110x -background none -font ubuntu-bold -fill $textcolor label:"Testng" \) \
-gravity northwest -geometry +100+400 -compose over -composite result2.png
Sorry, I do not know Imagick. So someone else may need to help on that.
Related
I am outputting a banner with an image to the left and some text to the right. This is working well, however the positioning is very manual. I'd like to be able to group the $robot_image and the $robot_name text into one image, and then center that image group on the background 1400x500 canvas. Any ideas how I'd achieve this? The attached image is what I'd like to achieve: The robot + text centered as a group on the canvas
$banner = exec("convert {$robot_image} -resize 500x500 -gravity west -geometry +100+100 -background '#{$hex}' -extent 1400x500 -font 'SignPainter.ttc' -pointsize 300 -fill 'white' -strokewidth 20 -fill white \
-stroke 'rgba(0,0,0,0.3)' -annotate +550+40 '{$robot_name}' \
-stroke none -annotate +550+40 '{$robot_name}' output.jpg");
For anyone interested, this is how I achieved it:
convert robot.png -resize 500x500 -background "#31BEFF" -size 600x -font 'SignPainter.ttf' -gravity center label:"RobotName" -alpha off +smush -50 -gravity center -background "#31BEFF" -extent 1400x500 result.jpg
I am attempting to write an imagemagick command to trim the transparent pixels from the bottom of a transparent PNG. I found these commands and modified them to take off just the bottom. However the output is not as expected.
With some trial and error I've identified that the command appears to be generating an image of a minimum height. If the design is too high it leaves transparent pixels underneath. But nothing jumps out at me as being the element of the command that causes that?
I've included my 3 files that I trimmed and the 3 results that the following command(s)
generates:
convert \( ORIGINAL.png -bordercolor none -border 1x0 \) -size 1x1 xc:black -gravity west -composite -size 1x1 xc:black -gravity east -composite -size 1x1 xc:black -gravity north -composite -fuzz 10% -trim +repage -bordercolor white -shave 1x0 TRIMMED.png
convert \( ORIGINAL2.png -bordercolor none -border 1x0 \) -size 1x1 xc:black -gravity west -composite -size 1x1 xc:black -gravity east -composite -size 1x1 xc:black -gravity north -composite -fuzz 10% -trim +repage -bordercolor white -shave 1x0 TRIMMED2.png
convert \( ORIGINAL3.png -bordercolor none -border 1x0 \) -size 1x1 xc:black -gravity west -composite -size 1x1 xc:black -gravity east -composite -size 1x1 xc:black -gravity north -composite -fuzz 10% -trim +repage -bordercolor white -shave 1x0 TRIMMED3.png
If someone could please explain what I am missing in terms of this height issue that would be really appreciated.
These are the 3 ORIGINAL files and the 3 outcomes showing how the outcome changes with the original file changes.
This is what I am trying to acheive - delete the empty space from the bottom of the image. I want no space after the image at the moment if it is too high I still get space underneath (see the last outcome, bottom right)
---- BELOW ARE JUST THE ORIGINAL FILES IF ANYONE WANTED TO TRY IT ON THEIR SETUP ETC ----
If you're trying to remove what a "-trim" would remove, but only from the bottom edge of an image, this command should give you that result...
convert input.png -background none -set page %[#] \
-set option:distort:viewport %[w]x%[fx:page.y+page.height] \
+repage -distort SRT 0 result.png
It starts by setting some variables that contain the results of a "-trim" operation, but without actually removing anything. Then it uses those variables to calculate the after-trim dimensions for the output viewport. Then it uses a no-op "-distort" to effectively crop the image to the calculated output dimensions, removing only the excess transparent pixels toward the bottom.
I'd like to outline an object in a transparent background with a 20px border. But, I want to fill in the closed regions with the border color.
$image = new Imagick('./img/hinata.png');
$mask = clone $image;
$mask->separateImageChannel(Imagick::CHANNEL_ALPHA);
$mask->negateImage(true);
$mask->edgeImage(20);
$mask->opaquePaintImage("white","blue",65000,false);
//// TODO: I don't know how to fill the holes
$mask->transparentPaintImage("black",0.0,0,false);
$image->compositeImage($mask,Imagick::COMPOSITE_DEFAULT,0,0);
I referenced this question:
Outline a transparent image using imagick PHP
This is the image:
This is what I want to achieve:
This is not what I want to achieve:
This is how I would do it in ImageMagick command line.
Make the background under the transparency blue.
Extract the alpha channel.
Dilate the alpha channel.
Use connected components to fill in any "holes" smaller than some threshold in area.
Replace the old alpha channel with the new one
Input: from here
convert cartoon_girl.png -background blue -alpha background \
\( -clone 0 -alpha extract \
-morphology dilate diamond:12 \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=500 \
-connected-components 8 \) \
-alpha off -compose copy_opacity -composite \
result.png
Unfortunately, Imagick does not support connected components as far as I know. So the only other way would be to use flood fill at some point inside each "hole". That means you have to pick x,y coordinate inside each hole to use for doing the flood fill after doing the dilate. See https://www.php.net/manual/en/imagick.floodfillpaintimage.php
convert cartoon_girl.png -background blue -alpha background \
\( -clone 0 -alpha extract \
-morphology dilate diamond:12 \
-fuzz 80% -fill white \
-draw "color 100,310 floodfill" \
-draw "color 200,235 floodfill" -alpha off \) \
-alpha off -compose copy_opacity -composite \
result2.png
I have some file like :
As you seen, in this png image, 2 or 3 area are over together,
When i want to composite with :
( code is example and isn't for this image )
exec("convert 1.png ".$newfile01." -geometry +208+225 -compose DstOver -composite ".$newfile02." -geometry +434+346 -compose DstOver -composite ".$newfile03." -geometry +656+211 -compose DstOver -composite ".$result_image);
after composite $newfile01 with png file, few of other transparent area will be withe.
Any help? (sorry for my bad english :)
I solve the problem.
In fact when i resize or rotate image it will be with white background:
exec('convert '.$newfile1.' -resize 325!x440! -rotate -5.9 '.$newfile01.'');
and best solution is $newfile1 in this example be PNG file with transparent background and code is:
-matte -background transparent
so :
exec('convert '.$newfile1.' -resize 325!x440! -matte -background transparent -rotate -5.9 '.$newfile01.'');
in this case, you don't have any problem with two or more image over together.
best Regards.
I been working all day to add shadow to text using command line imagemagick. Googled many pages and experimented with gaussian, blur, shadow commands for dropping shadow but no success.
Here is my command in php:
$img_save = 'C:\Users\abc\Desktop\testimage.jpg';
$line = "Anatidaephobia is the fear that somewhere in the world there is a duck watching you. ";
$line = wordwrap($line, 25, "\\n");
exec("convert -background white -weight bold -size 500x -pointsize 35 -font arial-italic -gravity center -fill black caption:\"$line\" $img_save");
The above code gives image without difficulty. Can anyone tell how to add shadow to text in the image?
Thanks
With caption just clone and shadow it with,
convert logo: -resize 40%x40 \
\( -size "80x40" -background none -gravity west \
-fill green caption:"Caption text" \
\( +clone -background navy -shadow 80x3+5+5 \) \
+swap -background none -layers merge +repage \) -composite out.png
You just have to draw the shadow first then draw over it with the font
convert -size 500x500 xc:white -pointsize 35 -font arial-italic -gravity center - fill red -draw "text 2,2 'text'" -fill black -draw "text 0,0 'text'" outfile.jpg