5
$\begingroup$

Is there a way to directly transform an image?

This works but seems klunky :

Graphics[{Translate[ Raster@ImageData@image, {x,y}], .. other graphics objects .. }]

Just wondering if there is a better way..

Per JM.s comment, you can sort of do this with ImageForwardTransformation I actually looked at those before asking the question and didn't think they would work, but now I went back and figured it out..

here is a simple translation:

{w, h} = ImageDimensions[img]
ImageForwardTransformation[ img , (# + {x, y}) &, Background -> 1, 
        DataRange -> Full, PlotRange -> {{0, w + x}, {0, h + y}}]

This creates a new larger image, large enough to contain the transformed result but still anchored with a corner at {0,0}, so isn't really equivalent to simply translating the image.

$\endgroup$
3
  • $\begingroup$ Look up ImageTransformation[] and ImageForwardTransformation[]. $\endgroup$ May 30, 2013 at 20:27
  • $\begingroup$ If I understand correctly, all you need is Inset. $\endgroup$
    – Jens
    May 30, 2013 at 21:28
  • $\begingroup$ Thanks, I was not aware of Inset, useful indeed! $\endgroup$
    – george2079
    May 31, 2013 at 19:04

1 Answer 1

8
$\begingroup$

Here is a completely random illustration of how to use Inset to place an image into a Graphics:

img = Image[
   Show[
    ExampleData[{"Geometry3D", "Cow"}],
    ImageSize -> 360, Background -> None],
   ImageResolution -> 144
   ];
ListAnimate@Table[
  Graphics[{
    Darker[Green],
    Disk[{.2, .4}, .6],
    Inset[Rotate[img, -t^2], {0, 1} + t {.9, -.8}]
    },
   Background -> Lighter[Gray],
   PlotRange -> {{0, 1}, {0, 1}},
   ImageSize -> 360
   ],
  {t, 0, 1, .1}
  ]

cow movie

The image img has transparent background, and is superimposed on a green circle in a Graphics object. When positioning the Inset inside Graphics, it is useful to always specify an explicit PlotRange, because it allows you to determine the position more accurately in the logical coordinate system of the other graphics objects. Here, I chose to make the left and bottom edges be at 0, and the top and right correspond to 1. In addition, I also included a Rotate command to show that you can add other transformations of the img inset.

$\endgroup$
1
  • $\begingroup$ Thanks for this illustration of "the cow jumped over the (green) moon". $\endgroup$
    – bill s
    Jun 1, 2013 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.