Skip to content

Commit df01047

Browse files
Script optimization
1 parent 534c3d7 commit df01047

File tree

2 files changed

+125
-16
lines changed

2 files changed

+125
-16
lines changed

SD/Script_for_animated_gif/Gif_Maker_from_png_data.m

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,64 @@
22
clear
33

44
disp('-----------------------------------------------------------')
5-
disp('|Beware, this code is for GNU Octave ONLY !!! |')
5+
disp('| Optimized Indexed GIF creator for GNU Octave (bzip) |')
66
disp('-----------------------------------------------------------')
77

88
pkg load image
99

1010
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11-
%User parameters
11+
% User parameters
1212
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13-
target_gif_file='Animation.gif'; %target file for animated gif
14-
gif_deadtime=0.05; %delay is seconds between pictures for animated gifs
15-
gif_skip=1; %keep every 1 out of gif_skip image for gif
16-
scaling_factor=1; %because you may want to change the image format which is 4x natively
13+
target_gif_file = 'Animation.gif'; % target file for animated gif
14+
gif_deadtime = 0.05; % delay in seconds between pictures
15+
gif_skip = 4; % keep every 1 out of gif_skip images
16+
scaling_factor = 1; % scale images if needed
1717
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818

19+
% Load file list
1920
listing = dir('./Pictures/*.png');
20-
for i=1:1:length(listing)
21-
name=listing(i).name;
22-
disp(['Processing ',listing(i).name]);
23-
[frame,map]=imread(['./Pictures/',name]);
24-
frame=imresize(frame,scaling_factor,'nearest');
25-
if i==1
26-
imwrite(frame,map,target_gif_file,'gif', 'Loopcount',inf,'DelayTime',gif_deadtime,'Compression','bzip');
27-
else
28-
if rem(i,gif_skip)==0
29-
imwrite(frame,map,target_gif_file,'gif','WriteMode','append','DelayTime',gif_deadtime,'Compression','bzip');
21+
n_files = numel(listing);
22+
23+
if n_files == 0
24+
error("No PNG images found in ./Pictures/")
25+
end
26+
27+
% Preload frames + maps
28+
frames = {};
29+
maps = {};
30+
disp('Reading and resizing images...')
31+
for i = 1:n_files
32+
name = listing(i).name;
33+
fprintf("Processing %s (%d/%d)\n", name, i, n_files);
34+
35+
if rem(i,gif_skip) == 0
36+
[frame, map] = imread(fullfile('./Pictures', name));
37+
if scaling_factor ~= 1
38+
frame = imresize(frame, scaling_factor, 'nearest');
3039
end
40+
frames{end+1} = frame;
41+
maps{end+1} = map;
3142
end
3243
end
3344

45+
fprintf("Total frames to write: %d\n", numel(frames));
46+
47+
% Write GIF (with mandatory bzip compression)
48+
disp("Writing animated GIF...")
49+
imwrite(frames{1}, maps{1}, target_gif_file, ...
50+
"gif", "LoopCount", Inf, ...
51+
"DelayTime", gif_deadtime, ...
52+
"Compression", "bzip");
53+
54+
for i = 2:numel(frames)
55+
fprintf("Appending frame %d/%d\n", i, numel(frames));
56+
imwrite(frames{i}, maps{i}, target_gif_file, ...
57+
"gif", "WriteMode", "append", ...
58+
"DelayTime", gif_deadtime, ...
59+
"Compression", "bzip");
60+
end
61+
62+
disp('-----------------------------------------------------------')
3463
disp('End of conversion, enjoy your fancy animations !')
64+
disp('-----------------------------------------------------------')
65+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
clc
2+
clear
3+
4+
disp('-----------------------------------------------------------')
5+
disp('| Progressive Averaged Grayscale GIF for Octave (bzip) |')
6+
disp('-----------------------------------------------------------')
7+
8+
pkg load image
9+
10+
% Parameters
11+
target_gif_file = 'Animation_progressive.gif';
12+
gif_deadtime = 0.05;
13+
gif_skip = 9;
14+
scaling_factor = 1;
15+
n_colors = 256;
16+
17+
% Generate fixed grayscale colormap
18+
gray_map = repmat((0:255)'/255, 1, 3); % 256×3
19+
20+
listing = dir('./Pictures/*.png');
21+
n_files = numel(listing);
22+
if n_files == 0
23+
error("No PNG images found in ./Pictures/")
24+
end
25+
26+
disp('Reading and averaging images...')
27+
28+
% Read first frame
29+
[frame, map] = imread(fullfile('./Pictures', listing(1).name));
30+
if scaling_factor ~= 1
31+
frame = imresize(frame, scaling_factor, 'nearest');
32+
end
33+
34+
% Convert to RGB and then to grayscale
35+
current_avg = ind2rgb(frame, map);
36+
current_avg = rgb2gray(current_avg); % double [0,1]
37+
38+
frames_idx = {};
39+
frame_counter = 1;
40+
41+
% Convert first frame to indexed using fixed grayscale map
42+
frames_idx{1} = uint8(current_avg*255);
43+
44+
% Process remaining frames
45+
for i = 2:n_files
46+
if rem(i,gif_skip) == 0
47+
[frame, map] = imread(fullfile('./Pictures', listing(i).name));
48+
if scaling_factor ~= 1
49+
frame = imresize(frame, scaling_factor, 'nearest');
50+
end
51+
52+
rgb = ind2rgb(frame, map);
53+
gray = rgb2gray(rgb);
54+
55+
current_avg = ((frame_counter)*current_avg + gray) / (frame_counter+1);
56+
frame_counter += 1;
57+
58+
frames_idx{end+1} = uint8(current_avg*255);
59+
60+
fprintf("Processed %s (%d/%d)\n", listing(i).name, i, n_files);
61+
end
62+
end
63+
64+
disp('Writing animated GIF...')
65+
66+
imwrite(frames_idx{1}, gray_map, target_gif_file, ...
67+
'gif', 'LoopCount', Inf, 'DelayTime', gif_deadtime, 'Compression', 'bzip');
68+
69+
for k = 2:numel(frames_idx)
70+
fprintf("Appending frame %d/%d\n", k, numel(frames_idx));
71+
imwrite(frames_idx{k}, gray_map, target_gif_file, ...
72+
'gif', 'WriteMode', 'append', 'DelayTime', gif_deadtime, 'Compression', 'bzip');
73+
end
74+
75+
disp('-----------------------------------------------------------')
76+
disp('Done! Progressive averaged grayscale GIF is ready!')
77+
disp('-----------------------------------------------------------')
78+

0 commit comments

Comments
 (0)