gif动画图制作
程序员文章站
2022-05-30 10:15:46
...
制作自己任意设置的gif动画,用MATLAB编写程序大为方便,只有十几行代码.
Demo1:
示例显示从1变到10,附上贴图,代码如下:
%% gif图像只能显示256色,故只支持index索引图像
filename = 'mygif';
image = uint8(255*zeros(480,640));
image = cat(3,image,image,image);
for i = 1:10
%% text
RGB = insertText(image,[200,240],num2str(i),...
'FontSize',100,'font','LucidaSansRegular','BoxColor',...
'green','TextColor','red');
%% write
[im,map] = rgb2ind(RGB,255);
if i==1
imwrite(im,map,filename,'gif','LoopCount',inf,'DelayTime',1);
else
imwrite(im,map,filename,'gif','WriteMode','append','DelayTime',1);
end
end
Demo2:
这次来个正式计时,耐心等待哦~
function createGIF2()
% 创建个性化的GIF动画Demo
%
A = ones(480,480);
positions = 0.4*[size(A,2),size(A,1)];
positions(2,:) = 0.1*[size(A,2),size(A,1)];
dispnumber = 10:-1:0;
image = imread('lena.jpg');% 这里输入你自己的图片哦
Num = length(dispnumber);
imageNum = 1;%length(imds.Files);
for i = 1:Num+imageNum
if i<= Num
RGB = insertText(A,positions,{num2str(dispnumber(i)),'请等待。。。'},...
'BoxOpacity',0,...
'FontSize',60,...
'textcolor','r',...
'Font','Simsun');%宋体
else
C = {'我美吗?','还看我?','还在看我??'};
str = sprintf('%s\n%s\n%s',C{:});
RGB = insertText(image,0.07*[size(image,2),size(image,1)],...
str,...
'BoxOpacity',0,...
'FontSize',100,...
'Font','STXingkai',...% 华文行楷字体
'TextColor','g');
RGB = imresize(RGB,size(A));
end
%% write
[ind,map] = rgb2ind(RGB,255);
if i == 1
imwrite(ind,map,'mygif1.gif','gif',...
'LoopCount',inf,...
'DelayTime',0.8);
elseif i == Num+imageNum
imwrite(ind,map,'mygif1.gif','gif',...
'WriteMode','append',...
'DelayTime',3);
else
imwrite(ind,map,'mygif1.gif','gif',...
'WriteMode','append',...
'DelayTime',1);
end
end
Demo3:
另外附上制作比较绚丽的MATLAB logo动态图,如下: