Page 1 of 1

queryfontmetrics - annotateImage

Posted: 2013-08-23T04:11:39-07:00
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

Re: queryfontmetrics - annotateImage

Posted: 2013-08-23T06:44:49-07:00
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.

Re: queryfontmetrics - annotateImage

Posted: 2013-08-23T06:51:30-07:00
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

Re: queryfontmetrics - annotateImage

Posted: 2013-08-23T07:28:20-07:00
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.

Re: queryfontmetrics - annotateImage

Posted: 2013-08-23T08:59:06-07:00
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

Re: queryfontmetrics - annotateImage

Posted: 2013-08-23T09:32:03-07:00
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).

Re: queryfontmetrics - annotateImage

Posted: 2013-08-24T13:51:42-07:00
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

Re: queryfontmetrics - annotateImage

Posted: 2013-08-24T13:56:28-07:00
by snibgo
Please show an example image, and example text (and the command that made the text).

Re: queryfontmetrics - annotateImage

Posted: 2013-08-24T20:22:35-07:00
by glennrp
Do a trial run with red text to find the positions, then a final run using those positions with black text.