queryfontmetrics - annotateImage

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
kenny
Posts: 4
Joined: 2013-08-23T04:03:43-07:00
Authentication code: 6789

queryfontmetrics - annotateImage

Post by kenny »

Dear Forum,
i have a little Problem with ImageMagick. I want to place some Text on an Image. I want to avoid overlap the Text.
The Position is random(x, y) is it possible to avoid the overlap? "Is there some text, dont put it on this position.. .
I want to write some Text on a big Map. Without overlap.

This is my Code (PHP):

Code: Select all

$width = 1024;
$height = 768;
$draw->setFillColor('black');
		$draw->setFont('wyswindsorhand.ttf');
	
		$sql = "SELECT spruch, userID FROM sprueche LIMIT 20";
		$todo = mysql_query($sql);

		while($daten = mysql_fetch_array($todo))
		{
			$draw->setFontSize(rand(50,80));

			$spruch = $daten['spruch'];
			$userID = $daten['userID'];

			$x = rand(1,$width);
			$y = rand(1,$height);
			$drehung = mt_rand(-20, 20);

			$img->annotateImage($draw, $x, $y, $drehung, utf8_encode($spruch));
		}

		$img->setImageFormat('jpg');

		$img->writeImage("tmp/img/map.jpg");

Can anybody help me?
Thanks for this..

Best regards
kenny
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: queryfontmetrics - annotateImage

Post by snibgo »

I don't quite understand. I think you want to place text on an image, but want to position it so it doesn't overlap other text or some other detail. Is that correct?

Here is one method:

1. Make an image of the new text. Get the dimensions.

2. From the main image, crop an area the same dimensions at a random location.

3. Establish whether any text is already there.

4. If no text was there, place the text image at that location; end algorithm. Otherwise, repeat from step 2.

I would set a limit on the maximum number of loops.

Step (3) will depend on the nature of the image and text. If the image is a fixed colour and the text is a different fixed colour, step (3) could search for the text colour in the crop.
snibgo's IM pages: im.snibgo.com
kenny
Posts: 4
Joined: 2013-08-23T04:03:43-07:00
Authentication code: 6789

Re: queryfontmetrics - annotateImage

Post by kenny »

Hi, thank you for the answer.
That is absolutely correct.

Can you give me a code example for (step 3) - Establish whether any text is already there?

Great thanks
kenny
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: queryfontmetrics - annotateImage

Post by snibgo »

Suppose the text is red, and the image contains no other red pixels.

This command ...

Code: Select all

compare -metric RMSE cropped.png xc:Red -similarity-threshold 0 -dissimilarity-threshold 1 -subimage-search NULL:
... will always succeed, but will return in STDERR a string like:

Code: Select all

0 (0) @ 356,389
or

Code: Select all

54.6554 (0.000833988) @ 0,0
"0 (0)" means a red pixel was found (the RMSE was zero), at pixel coordinate (356,389), but the location within the cropped area doesn't matter to you.

"54.6554 (0.000833988)" means no pixels were exactly red, and the closest to red had that RMSE.

For your purpose, you can just test if the first number is zero.

Sorry, I don't do PHP.

If you have "-dissimilarity-threshold 0", the command will succeed if a red pixel was found or fail if it wasn't. As this is all you need, that might be more convenient.

See http://www.imagemagick.org/script/comma ... -threshold etc.
snibgo's IM pages: im.snibgo.com
kenny
Posts: 4
Joined: 2013-08-23T04:03:43-07:00
Authentication code: 6789

Re: queryfontmetrics - annotateImage

Post by kenny »

Thank you very much!
My Background havn't only white oder black bckground. Its an Image with little points in black and scratches. Textcolor also in Black.
Did you have a other Method for me?

Best regards
Henry
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: queryfontmetrics - annotateImage

Post by snibgo »

Perhaps a noise-reduction could eliminate the "little points in black and scratches".

Please show an example image, and example text (and the command that made the text).
snibgo's IM pages: im.snibgo.com
kenny
Posts: 4
Joined: 2013-08-23T04:03:43-07:00
Authentication code: 6789

Re: queryfontmetrics - annotateImage

Post by kenny »

Hi,
thank you for the help.
Is there a other possibility maybe to calculate the free position?
Maybe to use $metrics = $image->queryFontMetrics($draw, $text);

Code: Select all

[boundingBox] => Array
        (
            [x1] => 0
            [y1] => -2
            [x2] => 6.890625
            [y2] => 7
        )
 
    [originX] => 70
    [originY] => 0
I do not come to the solution.

Great thanks
kenny
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: queryfontmetrics - annotateImage

Post by snibgo »

Please show an example image, and example text (and the command that made the text).
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: queryfontmetrics - annotateImage

Post by glennrp »

Do a trial run with red text to find the positions, then a final run using those positions with black text.
Post Reply