Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
matplotlib-dev-files/compare_ani_float.py at main · NGWi/matplotlib-dev-files · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
NGWi
/
matplotlib-dev-files
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
matplotlib-dev-files
/
compare_ani_float.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
180 lines (145 loc) · 6.3 KB
main
Breadcrumbs
matplotlib-dev-files
/
compare_ani_float.py
Copy path
Top
File metadata and controls
Code
Blame
180 lines (145 loc) · 6.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import
matplotlib
.
pyplot
as
plt
import
matplotlib
.
animation
as
animation
import
numpy
as
np
from
PIL
import
Image
import
io
from
pathlib
import
Path
import
numpy
.
testing
as
npt
def
create_animation_and_capture_frames
():
# Load the same data
file
=
r'example.dat'
nx
=
8
ny
=
9
num_frames
=
72
data
=
np
.
fromfile
(
file
,
np
.
float32
).
reshape
(
num_frames
,
ny
,
nx
)
# Create figure and plot
fig
,
ax
=
plt
.
subplots
()
vmax
=
100
vmin
=
0
h
=
ax
.
imshow
(
data
[
0
],
cmap
=
plt
.
get_cmap
(
'CMRmap_r'
),
origin
=
'lower'
,
interpolation
=
'none'
,
vmin
=
vmin
,
vmax
=
vmax
,
animated
=
True
)
ax
.
set_xticks
(
range
(
nx
))
ax
.
set_xticklabels
(
range
(
1
,
nx
+
1
))
ax
.
set_yticks
(
range
(
ny
))
ax
.
set_yticklabels
(
range
(
1
,
ny
+
1
))
plt
.
colorbar
(
h
)
fig
.
tight_layout
()
# List to store direct frames
direct_frames
=
[]
def
capture_frame
(
fig
):
buf
=
io
.
BytesIO
()
fig
.
savefig
(
buf
,
format
=
'png'
,
dpi
=
300
)
buf
.
seek
(
0
)
img
=
Image
.
open
(
buf
)
# Print transparency info for first and last few frames
if
len
(
direct_frames
)
<
2
or
len
(
direct_frames
)
>
num_frames
-
2
:
print
(
f"
\n
Frame
{
len
(
direct_frames
)
}
transparency info:"
)
print
(
f"Mode:
{
img
.
mode
}
"
)
if
img
.
mode
in
(
'RGBA'
,
'LA'
):
alpha_extrema
=
img
.
getchannel
(
'A'
).
getextrema
()
print
(
f"Alpha channel extrema:
{
alpha_extrema
}
"
)
return
np
.
array
(
img
)
def
update
(
frame
):
img
=
data
[
frame
,]
h
.
set_array
(
img
)
direct_frames
.
append
(
capture_frame
(
fig
))
return
h
,
# Create animation
interval
=
100
ani
=
animation
.
FuncAnimation
(
fig
,
update
,
frames
=
range
(
num_frames
),
interval
=
interval
,
blit
=
True
)
# Save animation
ani
.
save
(
'example_fixed.gif'
,
writer
=
'pillow'
,
fps
=
2
,
dpi
=
300
)
plt
.
close
()
# Close the figure to free memory
return
direct_frames
def
capture_gif_frames
(
gif_path
):
gif_frames
=
[]
gif
=
Image
.
open
(
gif_path
)
frame_num
=
0
try
:
while
True
:
# Print info about first and last few frames
if
frame_num
<
2
or
frame_num
>
69
:
print
(
f"
\n
GIF Frame
{
frame_num
}
info:"
)
print
(
f"Mode:
{
gif
.
mode
}
"
)
if
hasattr
(
gif
,
'info'
):
print
(
f"Frame info:
{
gif
.
info
}
"
)
gif_frames
.
append
(
np
.
array
(
gif
))
frame_num
+=
1
gif
.
seek
(
gif
.
tell
()
+
1
)
except
EOFError
:
pass
return
gif_frames
def
convert_frame_for_comparison
(
frame
):
"""Convert frame to RGB for comparison, handling different formats."""
if
isinstance
(
frame
,
np
.
ndarray
):
if
frame
.
ndim
==
2
:
# Palette mode
# Convert to RGB by repeating the channel
return
np
.
stack
([
frame
]
*
3
,
axis
=
-
1
)
elif
frame
.
shape
[
-
1
]
==
4
:
# RGBA
return
frame
[..., :
3
]
# Take only RGB channels
elif
frame
.
shape
[
-
1
]
==
3
:
# RGB
return
frame
return
None
def
compare_animations
():
print
(
"Generating direct matplotlib animation frames..."
)
direct_frames
=
create_animation_and_capture_frames
()
print
(
"Capturing frames from saved GIF..."
)
gif_frames
=
capture_gif_frames
(
'example_fixed.gif'
)
print
(
f"
\n
Original frame counts:"
)
print
(
f"Direct animation:
{
len
(
direct_frames
)
}
frames"
)
print
(
f"GIF:
{
len
(
gif_frames
)
}
frames"
)
# The direct animation has 74 frames total:
# - Frames 0,1 are setup frames
# - Frames 72,73 are cleanup frames
# The actual animation frames are 2 through 73 (72 frames)
direct_frames
=
direct_frames
[
2
:
74
]
# Take frames 2 through 73 (72 frames)
print
(
f"
\n
After selecting animation frames:"
)
print
(
f"Direct animation:
{
len
(
direct_frames
)
}
frames"
)
print
(
f"GIF:
{
len
(
gif_frames
)
}
frames"
)
if
len
(
direct_frames
)
!=
len
(
gif_frames
):
print
(
f"
\n
ERROR: Frame count mismatch!"
)
print
(
f"Direct frames:
{
len
(
direct_frames
)
}
, GIF frames:
{
len
(
gif_frames
)
}
"
)
return
print
(
"
\n
=== Comparing Frame Colors ===
\n
"
)
frame_differences
=
[]
max_diff
=
0
max_frame
=
0
for
i
, (
direct_frame
,
gif_frame
)
in
enumerate
(
zip
(
direct_frames
,
gif_frames
)):
# Convert frames to RGB numpy arrays for comparison
direct_rgb
=
convert_frame_for_comparison
(
direct_frame
)
gif_rgb
=
convert_frame_for_comparison
(
gif_frame
)
if
direct_rgb
is
None
or
gif_rgb
is
None
:
print
(
f"Error: Could not convert frame
{
i
}
for comparison"
)
continue
# Calculate color differences
diff
=
np
.
abs
(
direct_rgb
.
astype
(
float
)
-
gif_rgb
.
astype
(
float
))
mean_diff
=
np
.
mean
(
diff
)
frame_differences
.
append
(
mean_diff
)
if
i
==
0
:
print
(
f"Frame 0 analysis:"
)
print
(
f"Mean color difference:
{
mean_diff
:.4f
}
"
)
print
(
f"Max pixel difference:
{
np
.
max
(
diff
):.4f
}
"
)
print
(
f"Red channel mean difference:
{
np
.
mean
(
diff
[...,
0
]):.4f
}
"
)
print
(
f"Green channel mean difference:
{
np
.
mean
(
diff
[...,
1
]):.4f
}
"
)
print
(
f"Blue channel mean difference:
{
np
.
mean
(
diff
[...,
2
]):.4f
}
\n
"
)
frame_max
=
np
.
max
(
diff
)
if
frame_max
>
max_diff
:
max_diff
=
frame_max
max_frame
=
i
frame_differences
=
np
.
array
(
frame_differences
)
print
(
"=== Overall Analysis ==="
)
print
(
f"Maximum pixel difference across all frames:
{
max_diff
:.4f
}
(Frame
{
max_frame
}
)"
)
print
(
f"Average frame difference:
{
np
.
mean
(
frame_differences
):.4f
}
"
)
print
(
f"Standard deviation of frame differences:
{
np
.
std
(
frame_differences
):.4f
}
\n
"
)
# Calculate statistics excluding first frame
frame_differences_no_first
=
frame_differences
[
1
:]
print
(
"=== Analysis Excluding First Frame ==="
)
print
(
f"Maximum pixel difference:
{
np
.
max
(
frame_differences_no_first
):.4f
}
"
)
print
(
f"Average frame difference:
{
np
.
mean
(
frame_differences_no_first
):.4f
}
"
)
print
(
f"Standard deviation of frame differences:
{
np
.
std
(
frame_differences_no_first
):.4f
}
\n
"
)
if
np
.
max
(
frame_differences
)
>
0
:
print
(
"Note: Some frames show color differences."
)
print
(
"This is expected due to GIF color palette optimization."
)
if
__name__
==
'__main__'
:
compare_animations
()
You can’t perform that action at this time.