如何自动将AI Studio中的GIF文件上载到CSDN?

▲ 图1 旋转的数字 
简 介: 本文给出了自动将AI Studio工作环境中的GIF目录下的动图图片完成压缩,下载并上载到CSDN的整个过程。给出了实现的详细代码。利用这个工具便可以完成更加丰富多彩的数据展示的功能。
关键词: GIF,AISTudio,CSDN
 
  §01 GIF文件 
  
1.1 GIF文件简介
  GIF文件 ,也被称为“动图”【Graphics Interchange Format】是由COmpuServe公司Wilhite于1987年开发的位图图像格式,采用了LZW无损数据压缩技术压缩GIF图像而又不降低视觉质量。现在被广泛应用在网页中表现动态变化的图像。
 
▲ 图1.1 GIF懂图形牧场变化的函数 
巧妙应用动图及其压缩方式 可以用于传递更加丰佛的动态细节, 演示数据内部变化规律 , 将现象与数据之间协同展示 ,特别是 对于负载网络以及高维度数据形成多视角的展现 。

▲ 图1.2 展示SOFM网络变化过程 

▲ 图1.3 显示3D动态曲线 
利用 MATPLOTLIB 可以生成数据的一维、二维的图片,存储之后便可以通过相应的Python软件包形成GIF文件了。
1.2 Studio环境
AI Studio是 有百度提供的基于网页的云端人工智能实验环境。它为使用者提供了强大的算力环境。基于Notebook的编程调试环境也方便开发者搭建相应的算法。建立和消除环境对于用户本地计算机没有任何影响,大大方便开发者学习研究。
在AI Studio下生成的动态文件可以存储在本地的目录中,利用BML CodeLab提供的压缩下载命令可以方便下载到计算机本地,进行后期的处理。
但是,现在还没有找到AI Studio下动态下载文件的方法。 也许这是百度公司有意将选赢得功能进行了屏蔽。
那么如何能够自动完成相应的稳健的下载呢? 对于需要生成大量GIF图像的应用来说,手工下载显得不方便。

▲ 图1.2.1 AI Studio BML CodeLab编程界面 
下面介绍了自动下载压缩文件的解决方案。
 
  §02 自动下载文件 
  
2.1 主要思路
2.1.1 自动下载文件
BML CodeLab允许是使用键盘右键给出文件的下载链接。利用这个链接,可以在Chrome浏览器的地址栏输入,自动完成文件的下载。
https://aistudio.baidu.com/ibjcpu2/user/262579/3390934/files/gif.zip?_xsrf=2%7C7bb56695%7C79552b127773c1eee41d78c4e29a4104%7C1641455345

▲ 图2.1.1 AI Studio查看文件下载链接 
  在浏览器地址栏输入下载链接之后形成自动下载对话框,这样就可以将AI Studio BML Codelab中的文件,特别是GIF压缩文件进行下载了。
 
▲ 图2.1.2 在浏览器地址栏输入下载链接之后形成自动下载对话框 
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的效果。
 
▲ 图 演示动图上载到CSDN 
(1)py程序代码
#!/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的整个过程。给出了实现的详细代码。利用这个工具便可以完成更加丰富多彩的数据展示的功能。

▲ 图3.1 旋转的人像 
■ 相关文献链接:
● 相关图表链接:
- 图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
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/122474719
- 点赞
- 收藏
- 关注作者
 
            
 
           
评论(0)