How do I automatically upload GIF files from AI Studio to CSDN?

Introduction: This paper presents the whole process of automatically compressing, downloading and uploading the motion pictures in GIF directory in AI Studio working environment to CSDN. The detailed code of implementation is given. Using this tool, you can complete more colorful data display functions.

Keywords: GIF, AISTudio, CSDN

▲ Figure 1 rotating numbers

 

§ 01 GIF file

1.1 introduction to GIF documents

  GIF file , also known as "Graphics Interchange Format", is a bitmap image format developed by Wilhite of COmpuServe company in 1987. It uses LZW lossless data compression technology to compress GIF images without reducing visual quality. Now it is widely used to represent dynamic images in web pages.

▲ Figure 1.1 GIF understand the function of graph change

  Ingenious application of dynamic graph and its compression method It can be used to convey more dynamic details, Demonstrate the internal variation of dataDisplay phenomena and data together , especially The load network and high-dimensional data are presented from multiple perspectives .

▲ Figure 1.2 shows the change process of SOFM network

▲ figure 1.3 shows the 3D dynamic curve

  utilization MATPLOTLIB One dimensional and two-dimensional images of data can be generated. After storage, GIF files can be formed through corresponding Python software packages.

1.2 Studio environment

  AI Studio is a web-based cloud artificial intelligence experimental environment provided by Baidu. It provides users with a powerful computing environment. The programming and debugging environment based on Notebook also facilitates developers to build corresponding algorithms. The establishment and elimination of the environment has no impact on the user's local computer, which is greatly convenient for developers to study and research.

   the dynamic files generated in AI Studio can be stored in the local directory. The compressed download command provided by BML CodeLab can be easily downloaded to the local computer for later processing.

  however, there is no way to download files dynamically under AI Studio. Perhaps this is Baidu's intention to shield the winning function.

  so how can I automatically complete the corresponding robust download? For applications that need to generate a large number of GIF images, manual downloading is inconvenient.

▲ figure 1.2.1 AI Studio BML CodeLab programming interface

  the solution of automatically downloading compressed files is introduced below.

 

§ 02 automatic file download

2.1 main ideas

2.1.1 automatic file download

  BML CodeLab allows you to use the right key of the keyboard to give the download link of the file. With this link, you can enter in the address bar of Chrome browser to automatically complete the file download.

https://aistudio.baidu.com/ibjcpu2/user/262579/3390934/files/gif.zip?_xsrf=2%7C7bb56695%7C79552b127773c1eee41d78c4e29a4104%7C1641455345

▲ figure 2.1.1 AI Studio view file download link

   after entering the download link in the browser address bar, an automatic download dialog box is formed, so that the files in AI Studio BML Codelab, especially GIF compressed files, can be downloaded.

▲ figure 2.1.2 after inputting the download link in the browser address bar, an automatic download dialog box is formed

2.1.2 compressed GIF file

  GIF files contain a large number of pictures, so they need to be compressed and packaged before downloading, and then use the above "automatic download" scheme to complete the file download.

   to complete this process, the following end program is executed through dynamic paste, and the GIF smell record is packaged by using the function of Notebook program.

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)

2.1.3 extract and generate GIF file

   for the ZIP file downloaded locally, dynamically decompress it, and then generate the GIF file. If it is followed by a parameter, it is uploaded directly to the CSDN web page.

  the functions of this part are completed by zip2gif program.

2.2 automatic file download program

2.2.1 working conditions

   in order to realize the dynamic diagram directly from AI Studio to CSDN, the following working conditions need to be prepared:

  • Open AI Studio and enter BML CodeLab
  • For gifs in / home/aistudio Zip, obtain the download link information, and save it in the DOP of TEASOFT,
  • Open a new Chrome browser without entering any valid address. Its Title is "new tab".

2.2.2 work code

  automatically download the Python program as zip2gif. Pass parameter '.' before It means that the files in the GIF folder in the AI Studio working directory are automatically compressed and downloaded, and then the subsequent dynamic decompression is completed to generate GIF files and upload them to CSDN. Usage:

zip2gif... Demo image

  the following is the effect of automatically downloading the motion pictures in AI Studio GIF directory, generating gifs and uploading them to CSDN.

▲ upload the diagram to CSDN

(1) py program code

#!/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 = 'New tab'
        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 = "Save as"
        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
#============================================================

 

※ general ※ conclusion ※

   this paper presents the whole process of automatically compressing, downloading and uploading the motion pictures in the GIF directory in the AI Studio working environment to CSDN. The detailed code of implementation is given. Using this tool, you can complete more colorful data display functions.

■ links to relevant literature:

● relevant chart links:

Keywords: AI csdn gif

Added by skoobi on Thu, 13 Jan 2022 10:10:22 +0200