Page 1 of 1

convert/distort: wrong resizing if source image is too large

Posted: 2012-01-24T16:35:56-07:00
by marcnrw2
Hello!

I want to resize and adjust/turn a given image so that it is saved to given coordinates onto a bigger background image. That works perfect if the source image is not larger in width and height than the background image. As larger the source image as smaller the resized result (if source image size > background image size). That's funny a bit. :) Here is the code I am using:

Code: Select all

$bg='background_image.png';
$coords=array(26,169,308,182,305,341,20,354); // clockwise
$image='input_image.png';

$bgsize = getimagesize($bg);
$isize = getimagesize($image);

exec("/usr/imagemagick/bin/convert -extent $bgsize[0]x$bgsize[1] -alpha on -mattecolor transparent -virtual-pixel transparent -distort BilinearForward '0,0 $coords[0],$coords[1] $isize[0],0 $coords[2],$coords[3] $isize[0],$isize[1] $coords[4],$coords[5] 0,$isize[1] $coords[6],$coords[7]' $image temp.png");
To compensate my bad english a bit here two screenshots to demonstrate what I mean. The background image is always 640x360. For the first screenshot the source image (for overlapping the monitor screen in the background image) has a size of 288x170. That caused no trouble. But for the second screen the source image was larger than the background image. It counts 700x414. And the result is that the resized image is too small. (and not too large ironically)

Image
Image

What am I doing wrong?
And a second question: Are the used parameters the best for keeping as much as possible image quality?

Best regards
Marc

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-24T16:53:58-07:00
by fmw42
Forgive me, but I don't see any huge visual difference between your two images -- one has some extra gray at the bottom. Second, I do not fully understand as your PHP code only shows the generation of the one image and not the composite operation. Also your input image should be read right after the convert and then supply your arguments and then the output. See http://www.imagemagick.org/Usage/basics/#cmdline

Why do you need the -extent to the size of the background on your input image? Your distort should be able to handle changing the size of the output if you use +distort. Furthermore you don't specify the background color that should be used for the extent. See http://www.imagemagick.org/Usage/distor ... rt_bestfit

Note, you also may get a more correct looking distort by using perspective rather than bilinear.

Is the problem with the distort or with the compositing?

Can you post a link to your input, distorted and background images and your full command including the composite and exact control point coordinates for the distort? We really need to duplicate your command with exact numbers to see what is going on.

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-24T19:29:40-07:00
by marcnrw2
Hello!

Thank you for your answer!
You were right, my description wasn't exact enough. The result I posted as the two screenshots are not the results basing on the posted code. But the posted code was the code which makes the trouble. I post the complete code at the end of this message.
What the code I posted in my last message is doing is to edit an image which is finally overlaying the monitor screen of the background image. Here is the input image:

Image

And here is the background image:

Image

This should make more clear what the code should do and what does not work correct if the input image is too large. Then there are such grey frames you recognized. This is from the original monitor screen as you can see. But the input image should of course perfectly overlap the monitor screen.
You can play a bit around with my code on the website http://www.marcotomato.com/canvas/test/upload2.php if you want. (everybody can) There you can upload own images and let them been edited and placed onto the monitor screen of the background image. After uploading you must refresh the browser. A small bug. :-)

Best regards
Marc

Code: Select all

<?php

	
$bg='background_image.png';
$coords=array(26,169,308,182,305,341,20,354); // clockwise
$image='input_image.png';

$bgsize = getimagesize($bg);
$isize = getimagesize($image);

exec("/usr/imagemagick/bin/convert -extent $bgsize[0]x$bgsize[1] -alpha on -mattecolor transparent -virtual-pixel transparent -distort BilinearForward '0,0 $coords[0],$coords[1] $isize[0],0 $coords[2],$coords[3] $isize[0],$isize[1] $coords[4],$coords[5] 0,$isize[1] $coords[6],$coords[7]' $image temp.png");

exec("/usr/imagemagick/bin/convert -size $bgsize[0]x$bgsize[1] xc:none  -draw 'polygon $coords[0],$coords[1] $coords[2],$coords[3] $coords[4],$coords[5] $coords[6],$coords[7]' temp2.png");

exec("/usr/imagemagick/bin/composite -compose In temp.png temp2.png temp3.png");
exec("/usr/imagemagick/bin/composite -gravity center temp3.png background_image3.png result.png");

?>
<img src="result.png">

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-24T21:10:03-07:00
by fmw42
Anthony can verify, but it seems to be a problem with +distort bilinearforward.

The following fails with +distort bilinearforward, but works just fine with +distort perspective.

IM 6.7.4.9 Q16 Mac OSX Snow Leopard. What version are you using?


convert background_image2.png \
\( xxx2.png -alpha set -virtual-pixel transparent +distort perspective \
"0,0 26,168 %w,0 308,181 %w,%h 307,341 0,%h 21,354" \) \
-compose over -layers merge result.png


Image

See -layers merge use with +distort at
http://www.imagemagick.org/Usage/distorts/#cube3d
http://www.imagemagick.org/Usage/distorts/#box3d
http://www.imagemagick.org/script/comma ... qs6#layers

P.S. Sorry I just picked my own control points, but they are only different from yours by a couple of pixels. You can put your own values in my command line, if you want.

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-26T17:15:29-07:00
by marcnrw2
Wow! Really? I will check it out tomorrow and come back here. :D

Thank you very much, fmw!!!

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T20:13:33-07:00
by marcnrw2
Hi fmw!

Sorry that I am so late!
I checked out your code now but it seems that it still has the problem that input images which are "too large" are converted as too small images. Please try out this image which counts 700x414 px:

Image


By the way I let run my code on a Linux Debian-50-lenny-64-LAMP 2.6.26-2

Best regards
Marc

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T20:23:42-07:00
by fmw42
This works just fine for me on IM 6.7.5.0 Q16 Mac OSX Snow Leopard. (Same result as I posted earlier)


convert background_image2.png \
\( yyy.png -alpha set -virtual-pixel transparent +distort perspective \
"0,0 26,168 %w,0 308,181 %w,%h 307,341 0,%h 21,354" \) \
-layers merge result.png

What version of IM are you using? If you are not current enough, you could be having problems.

Are you using my code above exactly? Note -alpha set (which could be replaced with -matte for older IM versions), +distort and -layers merge.

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T21:14:43-07:00
by marcnrw2
I must give in I wasn't sure about the formatation. I used this PHP syntax:

Code: Select all

exec("/usr/imagemagick/bin/convert background_image3.png (yyy.png -alpha set -virtual-pixel transparent +distort perspective '0,0 26,168 %w,0 308,181 %w,%h 307,341 0,%h 21,354') -layers merge result.png");
I am rather sure I am using the last IM version. How can I find it out? PHPInfo didn't help.
Can you upload the result image you received on your Mac please?

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T21:39:18-07:00
by marcnrw2
6.7.3 is my version.

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T21:50:32-07:00
by fmw42
You have to escape the parentheses with \( ... \) and put spaces between the parens and the command -- they cannot be next to each other -- there must be white space. I am not sure what you need to do in PHP with regard to quoting here if at all.

exec("/usr/imagemagick/bin/convert background_image3.png \( yyy.png -alpha set -virtual-pixel transparent +distort perspective '0,0 26,168 %w,0 308,181 %w,%h 307,341 0,%h 21,354' \) -layers merge result.png");

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T22:07:15-07:00
by marcnrw2
DAMN! Yeah! That works!!! You are a genious! :-) I thought such elements for uncommenting are not needed in PHP but obviously it is. Thasnk you very much for all the effort, fmw!
Again: Thank you very much!

Marc

Re: convert/distort: wrong resizing if source image is too l

Posted: 2012-01-30T22:44:04-07:00
by fmw42