8
$\begingroup$

I need Mathematica code to reverse the rotation direction and viewpoint of an animated GIF named "wheel." The current rotation is counterclockwise, and I need it to rotate clockwise while being a mirrored image of the original.

I've been working with this code provided by ChatGPT for hours, but I keep encountering errors. I've searched the forum, but I can't find exactly what I'm looking for. I'm attaching the animated GIF and giving you an idea of what I need. Also, it would be helpful to have some parameters in the code for resizing in case the result turns out too large.

Can you help me?

enter image description here

Wheel.gif

(* Import the animated GIF *)
 originalGIF = Import["wheel.gif"];

(* Reverse the frames of the GIF to change the rotation direction *)
reversedFrames = Reverse[ImageData /@ ImageList[originalGIF]];

(* Create a mirrored version of the frames *)
mirroredFrames = ImageReflect[#, Left] & /@ ImageList[originalGIF];

(* Combine the reversed and mirrored frames to create the final GIF *)
finalFrames = Riffle[reversedFrames, mirroredFrames];

(* Export the final GIF with the reversed rotation and mirrored view *)
Export["wheel_reversed.gif", finalFrames]
$\endgroup$

2 Answers 2

8
$\begingroup$

To keep same speed for GIF import its "DisplayDurations":

dur = Import["/Users/Vitaliy/Desktop/wheel.gif", "DisplayDurations"]

And use it on export:

Start by flipping horizontally ImageReflect each frame:

originalGIF=Import["/Users/Vitaliy/Desktop/wheel.gif"];
finalFrames=ImageReflect[#,Left]&/@originalGIF;
Export["wheel_reversed.gif",finalFrames, "DisplayDurations" -> dur]

enter image description here

If you still need to reverse rotation - then reverse time. You reverse time by Reverse list of frames.

originalGIF=Import["/Users/Vitaliy/Desktop/wheel.gif"];
finalFrames=ImageReflect[#,Left]&/@Reverse[originalGIF];
Export["wheel_reversed.gif",finalFrames, "DisplayDurations" -> dur]

enter image description here

Now answering the comment question:

Is it possible to insert a parameter that controls the size of the resulting GIF?

  • For pixel size use , ImageSize -> 400 option in Export

  • For byte size check DETAILS section in DOCS, especially

enter image description here

-- those options affect byte size. ImageSize also affect byte size.

$\endgroup$
5
  • 1
    $\begingroup$ Thank you very much, it works quite well. Two things: a) Your result doesn't maintain the original GIF's speed. b) Is it possible to insert a parameter that controls the size of the resulting GIF? $\endgroup$
    – Pamela
    Sep 24 at 1:51
  • 1
    $\begingroup$ @Pamela Happy to help. Please see updated answer. Please carefully review examples in doc page I linked. $\endgroup$ Sep 24 at 2:18
  • $\begingroup$ hi, I can't move forward, I get this error, this is how the GIF is exported. drive.google.com/file/d/14AObWINVKU8z9AqpMG4Erc6EG2w1lsLy/… $\endgroup$
    – Pamela
    Sep 24 at 2:41
  • 1
    $\begingroup$ @Pamela the code works perfectly for me. You might be making some syntax mistake. Please check everything very carefully up to a coma, etc. $\endgroup$ Sep 24 at 16:20
  • $\begingroup$ Thank you very much, it was a damn comma that wasn't there, but MMA didn't report it as an error. Thanks for everything. $\endgroup$
    – Pamela
    Sep 24 at 17:21
6
$\begingroup$
  • The idea is rotate the matrix(rotate 90 Degree), that is
m = Array[Subscript[a, ##] &, {5, 5}];
m1 = Reverse[Transpose[m], {2}];
m2 = Transpose@Reverse@m;
MatrixForm /@ {m, m1, m2}

enter image description here

Clear["Global`*"];
originalGIF = Import["wheel.gif"];
durations = Import["wheel.gif", "DisplayDurations"];
rotatedFrames = Image@*Transpose@*Reverse@*ImageData /@ originalGIF;
Export["test.gif", rotatedFrames, 
  "DisplayDurations" -> durations] // SystemOpen

enter image description here

  • We can also use ImageRotate to rotation 90 Degree or -90 Degree.
originalGIF = Import["wheel.gif"];
durations = Import["wheel.gif", "DisplayDurations"];
rotatedFrames = ImageRotate[#, -90 Degree] & /@ originalGIF;
Export["test.gif", 
  Rasterize[#, ImageResolution -> 15, RasterSize -> 250] & /@ 
   rotatedFrames, "DisplayDurations" -> durations] // SystemOpen

enter image description here

  • And maybe ImageTransformation also work,but I can't figure out.
$\endgroup$
5
  • 1
    $\begingroup$ ,Hello, it's interesting to see it from the perspective of a matrix array. Why does your result have jumps? Can the resulting GIF be enlarged or reduced?" $\endgroup$
    – Pamela
    Sep 24 at 1:59
  • 1
    $\begingroup$ @Pamela I cut the gif to upload ,that is why the gif have jumps. $\endgroup$
    – cvgmt
    Sep 24 at 2:27
  • $\begingroup$ Thank you for your response, but I don't handle your matrix-based approach well enough $\endgroup$
    – Pamela
    Sep 24 at 17:24
  • 1
    $\begingroup$ @cvgmt Ad ImageTransformation ImageTransformation[#, TransformationFunction[{{0, -1, 1}, {1, 0, 0}, {0, 0, 1}}]] == ImageRotate[#, -90 Degree] &[originalGIF[[1]]] $\endgroup$
    – vindobona
    Sep 24 at 20:25
  • $\begingroup$ @vindobona Thanks! $\endgroup$
    – cvgmt
    Sep 24 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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