亞寵展、全球寵物產業風向標——亞洲寵物展覽會深度解析
833
2025-03-31
簡 介:
本文給出了自動將AI Studio工作環境中的GIF目錄下的動圖圖片完成壓縮,下載并上載到CSDN的整個過程。給出了實現的詳細代碼。利用這個工具便可以完成更加豐富多彩的數據展示的功能。
關鍵詞:
GIF,AISTudio,CSDN
GIF文件
目 錄
Contents
GIF文件簡介
Studio環境
自動下載文件
主要思路
自動下載文件
壓縮GIF文件
解壓縮生成GIF文件
自動下載
文件程序
工作條件
工作代碼
總 結
GIF文件
目 錄
Contents
GIF文件簡介
Studio環境
自動下載文件
主要思路
自動下載文件
壓縮GIF文件
解壓縮生成GIF文件
自動下載
文件程序
工作條件
工作代碼
總 結
1.1 GIF文件簡介
GIF文件 ,也被稱為“動圖”【Graphics Interchange Format】是由COmpuServe公司Wilhite于1987年開發的位圖圖像格式,采用了LZW無損數據壓縮技術壓縮GIF圖像而又不降低視覺質量?,F在被廣泛應用在網頁中表現動態變化的圖像。
巧妙應用動圖及其壓縮方式 可以用于傳遞更加豐佛的動態細節, 演示數據內部變化規律 , 將現象與數據之間協同展示 ,特別是 對于負載網絡以及高維度數據形成多視角的展現 。
利用 MATPLOTLIB 可以生成數據的一維、二維的圖片,存儲之后便可以通過相應的Python軟件包形成GIF文件了。
1.2 Studio環境
AI Studio是 有百度提供的基于網頁的云端人工智能實驗環境。它為使用者提供了強大的算力環境。基于Notebook的編程調試環境也方便開發者搭建相應的算法。建立和消除環境對于用戶本地計算機沒有任何影響,大大方便開發者學習研究。
在AI Studio下生成的動態文件可以存儲在本地的目錄中,利用BML CodeLab提供的壓縮下載命令可以方便下載到計算機本地,進行后期的處理。
但是,現在還沒有找到AI Studio下動態下載文件的方法。 也許這是百度公司有意將選贏得功能進行了屏蔽。
那么如何能夠自動完成相應的穩健的下載呢? 對于需要生成大量GIF圖像的應用來說,手工下載顯得不方便。
下面介紹了自動下載壓縮文件的解決方案。
2.1 主要思路
2.1.1 自動下載文件
BML CodeLab允許是使用鍵盤右鍵給出文件的下載鏈接。利用這個鏈接,可以在Chrome瀏覽器的地址欄輸入,自動完成文件的下載。
https://aistudio.baidu.com/ibjcpu2/user/262579/3390934/files/gif.zip?_xsrf=2%7C7bb56695%7C79552b127773c1eee41d78c4e29a4104%7C1641455345
在瀏覽器地址欄輸入下載鏈接之后形成自動下載對話框,這樣就可以將AI Studio BML Codelab中的文件,特別是GIF壓縮文件進行下載了。
2.1.2 壓縮GIF文件
由于GIF文件包括有大量的圖片,所以在下載之前需要將他們進行壓縮打包,然后再使用上面的“自動下載”的方案完成文件的下載。
完成這個過程則是通過動態粘貼執行如下的端程序,利用Notebook程序的功能完成GIF聞見錄的打包。
AIStudio_Title = 'BML CodeLab' opstr = ( "import sys,os,math,time", "sys.path.append('/home/aistudio/external-libraries')", "import matplotlib.pyplot as plt", "from numpy import *", "import zipfile", "filedim = os.listdir('/home/aistudio/GIF')", "with zipfile.ZipFile('/home/aistudio/gif.zip', 'w') as zf:", " for f in filedim:", " if f.find('points') > 0: continue", " fn = os.path.join('/home/aistudio/GIF', f)", " zf.write(fn)", ) clipboard.copy('\r\n'.join(opstr)) rect = tspgetwindowrect(AIStudio_Title) pyautogui.click((rect[2] - 150), rect[1] + 320) tspsendwindowkey(AIStudio_Title, "a", control=1, noreturn=1) tspsendwindowkey(AIStudio_Title, "av", control=1, noreturn=1) tspsendwindowkey(AIStudio_Title, "\r", shift=1, noreturn=1) time.sleep(.5)
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
2.1.3 解壓縮生成GIF文件
對于下載到本地的ZIP文件,進行動態解壓縮,然后在進行生成GIF文件。如果后面帶有參數,則直接上載到CSDN網頁中。
這部分的功能則有 zip2gif程序來完成。
2.2 自動下載文件程序
2.2.1 工作條件
為了可能夠實現動圖直接從AI Studio到CSDN,需要準備一下工作條件:
打開AI Studio,進入 BML CodeLab
對于/home/aistudio中的GIF.zip,獲得其下載鏈接信息,并保存在TEASOFT的DOP中,
打開一個新的Chrome瀏覽器,不輸入任何有效地址,它的Title為“新標簽頁”。
2.2.2 工作代碼
自動下載Python程序為 zip2gif。在之前通過參數“.”表示自動將AI Studio工作目錄中的GIF文件夾中的文件進行壓縮下載,然后完成后面的動態解壓縮生成GIF文件,并上載到CSDN。使用方法為:
zip2gif … 演示圖片
下面就是將AI Studio GIF目錄中的動圖完成自動下載,生成GIF,上載到CSDN的效果。
#!/usr/local/bin/python # -*- coding: gbk -*- #============================================================ # ZIP2GIF.PY -- by Dr. ZhuoQing 2021-12-17 # # zip2gif num # process only head num file # zip2gif #timespace # gif time space # zip2gif cdpgargument # # zip2gif * # Get all the select DOP into gif # zip2gif . # Compress AI Studio GIF,download # # Note: #============================================================ from headm import * import zipfile import shutil import pyautogui #------------------------------------------------------------ filename = r'D:\Temp\GIF.zip' if not os.path.isfile(filename): filename = r'C:\Users\SCW4000\Downloads\GIF.zip' filename1 = filename cdpgarg = "" giftime = 0 #------------------------------------------------------------ if len(sys.argv) > 1: if sys.argv[1][0] == '#': giftime = int(sys.argv[1][1:]) sys.argv[1:] = sys.argv[2:] #------------------------------------------------------------ AIStudio_Title = 'BML CodeLab' if len(sys.argv) > 1: if sys.argv[1][0] == '.': if sys.argv[1] == '.': sys.argv[1:] = sys.argv[2:] else: sys.argv[1] = sys.argv[1][1:] #---------------------------------------------------- opstr = ( "import sys,os,math,time", "sys.path.append('/home/aistudio/external-libraries')", "import matplotlib.pyplot as plt", "from numpy import *", "import zipfile", "filedim = os.listdir('/home/aistudio/GIF')", "with zipfile.ZipFile('/home/aistudio/gif.zip', 'w') as zf:", " for f in filedim:", " if f.find('points') > 0: continue", " fn = os.path.join('/home/aistudio/GIF', f)", " zf.write(fn)", ) clipboard.copy('\r\n'.join(opstr)) rect = tspgetwindowrect(AIStudio_Title) pyautogui.click((rect[2] - 150), rect[1] + 320) tspsendwindowkey(AIStudio_Title, "a", control=1, noreturn=1) tspsendwindowkey(AIStudio_Title, "av", control=1, noreturn=1) tspsendwindowkey(AIStudio_Title, "\r", shift=1, noreturn=1) time.sleep(1) #---------------------------------------------------- url_title = '新標簽頁' url = 'https:' + tspstring2text('https:') rect = tspgetwindowrect(url_title) if sum(rect) == 0: printf("Can not find %s window.\a"%url_title) exit() clipboard.copy(url) pyautogui.click((rect[0]+rect[2])//2, rect[1]+65) tspsendwindowkey(url_title, "dd", alt=1, noreturn=1) tspsendwindowkey(url_title, "v", control=1, noreturn=1) tspsendwindowkey(url_title, "\r") saveas_title = "另存為" for _ in range(5): time.sleep(.5) if sum(tspgetwindowrect(saveas_title)) != 0: tspsendwindowkey(saveas_title, "s", alt=1) break clipboard.copy("") time.sleep(2) #------------------------------------------------------------ gifdir = r'd:\temp\GIF' if not os.path.isdir(gifdir): os.mkdir(gifdir) else: filedim = os.listdir(gifdir) for s in filedim: fn = os.path.join(gifdir, s) if os.path.isfile(fn): os.remove(fn) #------------------------------------------------------------ if len(sys.argv) > 1: if sys.argv[1] == '*': dopid = tspgetselectid() count = 0 for id in dopid: filename = tspgetdopfile(id) if not os.path.isfile(filename): continue extstr = filename.split('.')[-1] outfn = os.path.join(gifdir, '%03d.%s'%(count, extstr)) count += 1 shutil.copyfile(filename, outfn) if giftime == 0: dir2gif(gifdir = gifdir) else: dir2gif(gifdir = gifdir, period=giftime, last=giftime) giffilename = r'd:\temp\gif1.gif' printf("Check GIF %s file size.\a"%giffilename) time.sleep(1) file_stats = os.stat(giffilename) printf('Gif %s size:%5.2fM\a'%(os.path.basename(giffilename), file_stats.st_size/(1e6))) if file_stats.st_size > 5000000: printf('ERROR: gif size is too big!\a\a') errorflag = 1 cdpgarg = ' '.join(sys.argv[2:]) if len(cdpgarg) > 0: tspexecutepythoncmd('cdpg %s'%cdpgarg) exit() #------------------------------------------------------------ headnum = 0 #------------------------------------------------------------ fn = '' if len(sys.argv) > 1: fn = os.path.join(r'd:\temp', sys.argv[1]+'.zip') if os.path.isfile(fn): filename = fn else: cdpgarg = ' '.join(sys.argv[1:]) if len(sys.argv) > 1: if sys.argv[1][0].isdigit(): headnum = int(sys.argv[1]) cdpgarg = ' '.join(sys.argv[2:]) tspdropfile2pastetext() strall = clipboard.paste().strip() if os.path.isfile(strall): if len(strall) > 0: filename = strall #------------------------------------------------------------ #filename = filename.replace('\r','').replace('\n','') filename = filename.strip() #------------------------------------------------------------ if not os.path.isfile(filename): filename = r'd:\temp\gif.zip' if not os.path.isfile(filename): printf("Can not find zip file: %s\a"%filename) exit() if filename.find('.zip') < 0 and filename.find('.ZIP') < 0: printf("The file is not the zip file: %s"%filename) exit() #------------------------------------------------------------ errorflag = 0 with zipfile.ZipFile(filename) as zfile: nl = zfile.namelist() fdim = [] count = 0 for f in nl: if f.find('Thumbs') >= 0: continue zfile.extract(f, gifdir) fn = os.path.join(gifdir, f) fdim.append(fn.replace('/', '\')) count += 1 if headnum > 0: if count >= headnum: break #-------------------------------------------------------- if len(fdim) == 0: exit() gifpath = os.path.dirname(fdim[0]) scanfile = os.listdir(gifpath) for f in scanfile: fn = os.path.join(gifpath, f) try: id = fdim.index(fn) except: os.remove(fn) printf("Begin to unzip file: %s[%d/%d] and change to GIF1.GIF.\a"%(os.path.basename(filename), headnum, len(nl))) if len(fdim) > 0: if giftime == 0: dir2gif(gifdir = gifpath) else: dir2gif(gifdir = gifpath, period=giftime, last=giftime) for f in fdim: os.remove(f) #------------------------------------------------------------ giffilename = r'd:\temp\gif1.gif' printf("Check GIF %s file size.\a"%giffilename) time.sleep(1) file_stats = os.stat(giffilename) printf('Gif %s size:%5.2fM\a'%(os.path.basename(giffilename), file_stats.st_size/(1e6))) if file_stats.st_size > 5000000: printf('ERROR: gif size is too big!\a\a') errorflag = 1 #------------------------------------------------------------ if errorflag == 0: os.remove(filename) #------------------------------------------------------------ if len(cdpgarg) > 0 and errorflag == 0: tspexecutepythoncmd('cdpg %s'%cdpgarg) #------------------------------------------------------------ # END OF FILE : ZIP2GIF.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
本
文給出了自動將AI Studio工作環境中的GIF目錄下的動圖圖片完成壓縮,下載并上載到CSDN的整個過程。給出了實現的詳細代碼。利用這個工具便可以完成更加豐富多彩的數據展示的功能。
■ 相關文獻鏈接:
GIF文件
動圖如何做成的?
競爭網絡演化動圖
動圖與數據同步關系研究
如何生成動態matlibplot動圖?
● 相關圖表鏈接:
圖1 旋轉的數字
圖1.1 GIF懂圖形牧場變化的函數
圖1.2 展示SOFM網絡變化過程
圖1.3 顯示3D動態曲線
圖1.2.1 AI Studio BML CodeLab編程界面
圖2.1.1 AI Studio查看文件下載鏈接
圖2.1.2 在瀏覽器地址欄輸入下載鏈接之后形成自動下載對話框
圖 演示動圖上載到CSDN
AI Python
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。