-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
75 lines (52 loc) · 1.8 KB
/
utils.py
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
import os
import pyautogui as pt
from PIL import Image, ImageTk
def get_screen_size():
return pt.size()
def get_gif_frame_count(gif_path):
with Image.open(gif_path) as im:
im.seek(0)
img_frame_count = 1
while True:
try:
im.seek(im.tell() + 1) # 尝试获取下一帧
img_frame_count += 1
except EOFError:
break # 到达文件末尾,退出循环
return img_frame_count # 加上第一帧
def get_png_frame_count(pngDir_path):
files = os.listdir(pngDir_path)
img_frame_count = 0
# 遍历文件列表
for file in files:
if file.endswith('.png'):
img_frame_count += 1
return img_frame_count
def resize_image_png(image_path, new_size):
with Image.open(image_path) as im:
if new_size != (0, 0):
im_resized = im.resize(new_size, resample=Image.BICUBIC)
# im_resized = im.resize(new_size, resample=Image.BILINEAR)
# im_resized = im.resize(new_size, resample=Image.LANCZOS)
else:
im_resized = im
return ImageTk.PhotoImage(im_resized)
def resize_image_gif(image_path, new_size):
im = Image.open(image_path)
num_frames = im.n_frames
frames_tk = []
for i in range(num_frames):
im.seek(i)
if new_size != (0, 0):
# 缩放当前帧
resized_frame = im.resize(new_size, resample=Image.BICUBIC)
else:
# 图像不进行放缩操作
resized_frame = im
photo_tk = ImageTk.PhotoImage(resized_frame)
frames_tk.append(photo_tk)
return frames_tk
if __name__ == "__main__":
path = './CharacterGifs/momo/RunRight.gif'
frame_count = get_gif_frame_count(path)
print(f"gif帧数 {path}: {frame_count}")