matlab,制作GIF动画(png转gif)

您所在的位置:网站首页 怎么把图片制作成文件 matlab,制作GIF动画(png转gif)

matlab,制作GIF动画(png转gif)

2024-06-06 21:11| 来源: 网络整理| 查看: 265

目标:将三幅图(png格式)制作成GIF

准备材料:三幅png格式的图 在这里插入图片描述

gif展现一个动态由悲到喜的过程

实现代码:

`%预定义变量 Picture_save = {0}; %初始化提高效率 filename = 'myGIF.gif'; %图像获取,图像类型转换 [P1, map] = imread('1.png'); picture1 = ind2rgb(P1,map); %索引图必须转换为真彩图,否则颜色不对 [P2, map] = imread('2.png'); picture2 = ind2rgb(P2,map); [P3, map] = imread('3.png'); picture3 = ind2rgb(P3,map); array = [{picture1}, {picture2} ,{picture3}]; %方便循环 %保留帧数据 for idx = 1:3 figure(idx) imshow(array{idx}); %索引图转换为真彩图 ,{}访问数组里面的类型 ()访问元胞类型 Picture_save{idx} = frame2im(getframe(idx)); %获取figure(idx)展示的效果,将其以图片数据保存 close all end %制作GIF %有个bug 在第一次播放的时候没有中间的那一帧,之后又有了 for idx = 1:3 [A,map] = rgb2ind(Picture_save{idx},256); %imwrite不能显示三维图像,所以要进行转换 if idx == 1 imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.5); %Inf 无限循环 else imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.5); end end close all`

以下是代码的截图 在这里插入图片描述

当然了,还有更简单的办法:

for idx = 1:3 [A,map] = imread(strcat(num2str(idx),'.PNG')); %imwrite不能显示三维图像,所以要进行转换 if idx == 1 imwrite(A,map,'new.gif','LoopCount',Inf,'DelayTime',0.5); %Inf 无限循环 else imwrite(A,map,'new.gif','WriteMode','append','DelayTime',0.8); end end

关于代码有遇到别的问题,或者有改进意见,欢迎评论。

在实现过程中我也遇到了很多问题,有些注释在代码中,有些遗漏了,希望可以在讨论中得到补充。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3