How can I do variable delay creating a gif?

8 visualizzazioni (ultimi 30 giorni)
Yolanda Pamies Rodríguez
Yolanda Pamies Rodríguez il 22 Apr 2019
Commentato: Walter Roberson il 25 Nov 2024 alle 9:58
Hello, my question is as follows:
I need to control the time/speed with which the gif is represented. That's what the DelayTime function is for. I would like the delay to be in a vector with different values. Is it possible to make variable the delay? I read in a post that puts a value of 6 seconds Is there a range of fixed values? Thank you very much, greetings.
My delay vector is the one shown in the image, it has 65 values between [1,5].
Captura.PNG
I would like the value of the time delay to be changed depending on the position (i). I show the code with which I make the gif. I've tried it but the delay/speed doesn't change.
for i = 1: Puntos_emision
.
.
.
% -- Animación GIF del nivel de atenuación acumulado
% Captura las representaciones como una imagen
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
filename = 'Nivel_presion.gif';
delay = load ('t_pos.txt');
% Guarda en el archivo GIF
if i == 1
imwrite(imind,cm,filename,'gif','DelayTime',delay(i), 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','DelayTime',delay(i), 'WriteMode','append');
end
.
.
.
end
Thank you very much, any idea will be welcome.

Risposte (1)

Himanshu
Himanshu il 25 Nov 2024 alle 9:46
Hey,
You can indeed use a vector of delay times.
The issue with the current code/approach is that the delay values are not being correctly applied to each frame of the GIF. This might be due to how the delay values are being loaded or indexed. The load function might not be correctly interpreting the data from t_pos.txt, or the indexing might not be correctly aligned with the loop iterations, causing the same delay to be applied to all frames.
To resolve this, ensure that the delay values are correctly loaded into a vector and indexed properly within the loop. Verify that the delay variable is a vector and that delay(i) correctly accesses the intended value for each frame. Additionally, ensure that the t_pos.txt file is formatted correctly and that the values are being read as expected.
% Load delay values from file
delay = load('t_pos.txt');
for i = 1:Puntos_emision
% ... (other code)
% Capture the frame
frame = getframe(h);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
filename = 'Nivel_presion.gif';
% Save to GIF with variable delay
if i == 1
imwrite(imind, cm, filename, 'gif', 'DelayTime', delay(i), 'Loopcount', inf);
else
imwrite(imind, cm, filename, 'gif', 'DelayTime', delay(i), 'WriteMode', 'append');
end
% ... (other code)
end
You must ensure that t_pos.txt is correctly formatted and contains the expected number of delay values.
Hope this helps!
  1 Commento
Walter Roberson
Walter Roberson il 25 Nov 2024 alle 9:58
The documentation for delaytime is a bit ambiguous. It talks about the delay before presenting the next image, but it is not clear whether the delay is before the frame you are currently writing, or after the frame you are currently writing.

Accedi per commentare.

Categorie

Scopri di più su Robust Control Toolbox in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by