How can I create a gif that plays only once?

20 ビュー (過去 30 日間)
Robert Sweeney
Robert Sweeney 2018 年 12 月 19 日
コメント済み: Aravindh Babu 2023 年 6 月 18 日
when creating an animated gif using imwrite, the result loops continuously no matter what I set LoopCount to be. I'm trying to get it to play only once, so I'm setting LoopCount to "0". Is this a bug?
  1 件のコメント
Mark Sherstan
Mark Sherstan 2018 年 12 月 19 日
Please post your code!

サインインしてコメントする。

回答 (3 件)

Chad Greene
Chad Greene 2018 年 12 月 19 日
Use my gif function!
Just type
gif('myfile.gif','LoopCount',1)
for the first frame
and then
gif
to write each subsequent frame. That's it.
  1 件のコメント
Aravindh Babu
Aravindh Babu 2023 年 6 月 18 日
I use your function a lot, actually (and I really enjoy it, btw). But because of the way imwrite is set up, you still have to set 'LoopCount' to 0, not 1, to get it to loop once. I just tried setting 'LoopCount' to 1 and when I import the exported GIF into Photoshop, the actual loop count is 2.

サインインしてコメントする。


Robert Sweeney
Robert Sweeney 2018 年 12 月 19 日
Here's my function. In the beginning of the routine it is initialized with the first frame and ovwrt = 1. For every subsequent frame, the frame is appended with ovwrt = 0.
function make_gif(fig, ovwrt, filename)
frame = getframe(fig);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ovwrt
imwrite(imind,cm,filename,'gif', 'Loopcount', int8(0), 'DelayTime', 0.1);
else
imwrite(imind,cm,filename,'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
end

Mark Sherstan
Mark Sherstan 2018 年 12 月 19 日
Using the code found from here I can confirm that if I use 0 as the 'loopcount' argument it plays once and inf it keeps repeating. Passing the information from your script to the function something is being lost.
h = figure;
axis tight manual % this ensures that getframe() returns a consistent size
filename = 'testAnimated.gif';
for n = 1:0.5:5
% Draw plot for y = x.^n
x = 0:0.01:1;
y = x.^n;
plot(x,y)
drawnow
% Capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if n == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',0);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
So I modified the above and got the following script and function which is very close to yours. Again something must just be getting lost between your function and elsewhere but hopefully this helps:
Script:
h = figure;
axis tight manual
filename = 'testinf.gif';
for n = 1:0.5:5
x = 0:0.01:1;
y = x.^n;
plot(x,y)
drawnow
make_gif(h, n, filename)
end
Function:
function make_gif(fig, ovwrt, filename)
frame = getframe(fig);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ovwrt == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',0);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
Alternatively you can use this function from the file exchange!

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

タグ

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by