Cube Transformation

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
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Cube Transformation

Post by dognose »

I'm trying to figure out an easier way of doing a cube effect. That is, putting 3 different pictures on 3 faces of a cube.

Something like this, but without the rotation:
http://www.isotope244.com/i/pc_large_cube.jpg

Any ideas?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Cube Transformation

Post by Bonzo »

User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cube Transformation

Post by fmw42 »

Bonzo,

Following your link, on the page that comes up, the link http://www.rubble.info/gallery/gallery.php requires the user to log-in. Would like to see your working example.

Fred
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Cube Transformation

Post by Bonzo »

That will work now Fred :)

Edit: A differnt layout but basicly the same method - there is an imagemap behind the photos and when you select one it opens a URL

The imagemap - http://www.rubble.info/gallery/gallery_map.gif

When I upload some images I can run a code that adds the text, the borders, rotates the images, puts the images on the background then creates the imagemap to reflect the size, layout and oriantation of the photos.

The power of php and Im 8)
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Cube Transformation

Post by dognose »

That's great start. It's more of an isometric image, it's missing the 3d perspective.

http://www.math.utah.edu/~treiberg/Perspect/PerCube.GIF
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Cube Transformation

Post by Bonzo »

Yes it was only something I was messing about with when I was bored one day !

You could use the distort rather than the shear for that.

http://www.rubblewebs.co.uk/imagemagick ... fo=box.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cube Transformation

Post by fmw42 »

That's great start. It's more of an isometric image, it's missing the 3d perspective.

http://www.math.utah.edu/~treiberg/Perspect/PerCube.GIF
IM can do perspective, but you still need to create each image properly and composite them so as to form your cube. IM does not have any function at this time that will take multiple images and automatically create a cube as you want it (to my knowledge). But it is all possible with multiple IM commands or a script, if you want to put one together or modify the one from Bonzo.

see -distort perspective in the IM commands
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cube Transformation

Post by anthony »

Bonzo wrote:Yes it was only something I was messing about with when I was bored one day !

You could use the distort rather than the shear for that.

http://www.rubblewebs.co.uk/imagemagick ... fo=box.png
You mention in the linked page that the lines did not join. It mat be that your control points are not quite right. Try positioning them with a .5 pixel position so the aliasing overlaps. Do use a +distort option so the resulting image is sized to fit the distorted image. Then 'layer' the two resulting images together using -layers merge, and only then JUNK the virtual canvas offset. This what the control points you define, regardless of if they are positive or negative, will match up and you image will be complete.

Also the line should not be parallel to the bottom of the image! It is perspective after all!

Also the image coordinate should be 1 less than the image size!!!

here are my test commands

Code: Select all

# Generate a Spine Image
convert -size 200x40 xc:lightblue \
  -pointsize 20 -gravity north -annotate +5+0 'IM Examples' \
  -pointsize 10 -gravity south -annotate +0+0 'ImageMagick' \
  -stroke blue -strokewidth 2 -draw 'line 30,0 30,40' \
  -rotate -90 /tmp/spine.png

# generate the front cover
convert -size 150x200 xc:lightblue \
  -fill black -pointsize 20 -gravity north -annotate +0+5 'IM Examples' \
  -fill blue -pointsize 15 -gravity northeast -annotate +5+28 'Box Set' \
  -fill black -pointsize 15 -gravity south -annotate +0+5 'ImageMagick' \
  -stroke blue -strokewidth 2 -draw 'line 0,170 150,170' \
  \( http://imagemagick.org/Usage/images/logo.gif -resize 100x100 \) \
  -gravity center -compose multiply -composite /tmp/front.png

# Distort both images and merge together.
convert /tmp/spine.png -virtual-pixel transparent \
  +distort Perspective '0,0 -40,20  0,199 -40,179  49,199 0,199  49,0 0,0' \
  \( /tmp/front.png \
  +distort Perspective '0,0 0,0  0,199 0,199 149,199 99,169 149,0 99,30' \
  \) -background black -layers merge  +repage  box_set.png

rm /tmp/spine.png /tmp/front.png
NOTE that the coordinate order is different and bozzo's coordinates was generated while the -distort command was still in an experimental state.

I am however noticing that the spine image while being transformed correctly, appeared to have been transformed as if it was offset. I have not been able to determine where the mistake lies (the above code, or in the IM code).

the above script will be available in a couple of days at
http://imagemagick.org/Usage/scripts/box_set_example

Addendum: the above works if I add a -repage +10+0\! just after reading the spine image. So there is a definite bug in the IM code, where the read in image appears to have the wrong virtual canvas offset (of -10+0 ), even though the image actually does not have this offset. Looking into the fault!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cube Transformation

Post by anthony »

Okay. It was not a bug in IM, but a bug in the script. and I have fixed it.

Basically the spline was only 40 pixels wide, but I was distorting it as if it was 50 pixels wide. the last convert command should have been...

Code: Select all

convert /tmp/spine.png -virtual-pixel transparent \
  +distort Perspective '0,0 -40,20  0,199 -40,179  39,199 0,199  39,0 0,0' \
  \( /tmp/front_box.png \
  +distort Perspective '0,0 0,0  0,199 0,199  149,199 99,169  149,0 99,30' \
  \) -background black -layers merge  +repage box_set.png
I have fixed this in the "box_set_example" script, which will appear on the offical IM Examples scripts area in a day or too (see link above).

Finally the reason the horizontal line did not match was that it was drawn on the spine line was draw with a x offset set of '30'.
On the front of the box however it was drawn at '170' which was suposed to be 30 pixels from the bottom of the image (which is 200) pixel high. However as the bottom 'pixel' position is actually '199', that '170' should have been '169' pixels. Changing the front box line to '169', and the two lines now match up properly.

Here is the scripts output after the two 'bugs' in the script were corrected...

Image
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cube Transformation

Post by fmw42 »

I have just created a script, 3Dbox, built upon Anthony Thyssens excellent examples (http://www.imagemagick.org/Usage/distorts/#cube3d and http://www.imagemagick.org/Usage/distorts/#box3d), to allow one to create perspective (or isometric) views of a box at any orientation with pictures on each of its sides. The script is available at

http://www.fmwconcepts.com/imagemagick/index.html

The script can be used within another script (examples provided) to animate the box so that it rotates about its centroid.
Post Reply