其他分享
首页 > 其他分享> > 利用CSDN将图片自动存入AI Studio :pic2bml

利用CSDN将图片自动存入AI Studio :pic2bml

作者:互联网

简 介: 编写了python程序 pic2bml 可以快速借助于CSDN的图片存储功能,将图片串入AI Studio。对于有少量临时图片传输编程,可以增加程序开发的效率。

关键词 pic2bmlbmlcsdn

背景介绍 文章目录 基本方案 使用命令 实现代码 总 结

 

§01 景介绍


  AI Studio是百度提供的进行人工智能开发的平台。在提供了百度人工智能网络框架之外,利用AI Studio 可以将软件、数据库、模型训练和部署整合在一起。免去了个人在自己的计算机平台是好搭建平台和维护开发过程琐事。

  在AI Studio界面中,提供了多种途径允许将自己的图片数据文件导入云端的计算机中:

  既然有了这些手段,为什么还需要编程将图片自动导入AI Studio呢?

  主要还是为了能够产生自动化调试和测试使用。对于少量实时采集的图片,如果希望能够测试相关的算法,通过编程自动导入可以提高程序开发的效率。

1.1 基本方案

  基本处理过程

1.2 使用命令

# Transfer picture into BaiDu Machine Learning Lab.
# Usage: pic2bml * [work/1.jpg]     # transfer clipboard pic to BML
#                picid   []         # transfer DOP id to BML
#                0       []         # transfer DOP picture dop to BML
#                picfile []         # Transfer picfile to BML
#                                   # default directory: temp
#        source: * : Clipboard
#                0 : DOP picture
#                digit: DOP id
#                picfile
#        dest:   default: work/1.jpg
#                picfile: default directory : Work
#                =var   : Set url variable

1.3 实现代码

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# PIC2BML.PY                   -- by Dr. ZhuoQing 2021-12-22
#
# Transfer picture into BaiDu Machine Learning Lab.
# Usage: pic2bml * [work/1.jpg]     # transfer clipboard pic to BML
#                picid   []         # transfer DOP id to BML
#                0       []         # transfer DOP picture dop to BML
#                picfile []         # Transfer picfile to BML
#                                   # default directory: temp
#        source: * : Clipboard
#                0 : DOP picture
#                digit: DOP id
#                picfile
#        dest:   default: work/1.jpg
#                picfile: default directory : Work
#                =var   : Set url variable
#
# Note:
#============================================================

from headm import *


from PIL import Image
from io import BytesIO
import win32clipboard
import pyautogui

#------------------------------------------------------------
csdn_window = '写文章-CSDN博客'
AIStudio_Title = 'BML CodeLab'

#------------------------------------------------------------
def send_to_clipboard(clip_type, data):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(clip_type, data)
    win32clipboard.CloseClipboard()

#------------------------------------------------------------
def ClipboardCopyImage(imageFile):
    if not os.path.isfile(imageFile): return

    image = Image.open(imageFile)
    output = BytesIO()
    image.convert('RGB').save(output, 'BMP')
    data = output.getvalue()[14:]
    output.close()
    send_to_clipboard(win32clipboard.CF_DIB, data)

#------------------------------------------------------------
def GetCSDNImageURL():
    tspsendwindowkey(csdn_window, ' ',noreturn=1)
    tspsendwindowkey(csdn_window, 'z', control=1,noreturn=1)
    tspsendwindowkey(csdn_window, 'v', control=1,noreturn=1)

    for i in range(40):
        time.sleep(.5)
        readdata = tspread()

        if readdata[2] == 1: break
        if readdata[7] != 0: break
        if readdata[8] != 0: exit()
        if readdata[9] != 0: exit()

        tspsendwindowkey(csdn_window, 'c', control=1, noreturn=1)
        if clipboard.paste().find('在这里插入图片描述') >= 0:
            tspbeep(1500, 200)
            time.sleep(.25)
            break


        printf('\a')


    tspsendwindowkey(csdn_window, 'ac', control=1, noreturn=1)

    time.sleep(.1)

    pastestr = clipboard.paste().split('![在这里插入图片描述](')
    if len(pastestr) < 2:
        printf("Can not find the ![Insert picture] discriptor !\a")
        return ''


    tspsendwindowkey(csdn_window, 'z', control=1)

    pastestr = pastestr[1].split(')')[0]
    replacestr = '![在这里插入图片描述](%s)'%pastestr
    pastestr = pastestr.split('?')[0]

    return pastestr

#------------------------------------------------------------
def GetCSDNImageUrl(imageFile):
    ClipboardCopyImage(imageFile)
    return GetCSDNImageURL()

#------------------------------------------------------------
filename = '*'
outfile = '/home/aistudio/work/1.jpg'

#------------------------------------------------------------
if len(sys.argv) > 1:
    filename = sys.argv[1]
    if filename.isdigit():
        picfile = tspgetdopfile(int(filename))
        extstr = picfile.split('.')[-1].upper()
        if not extstr in 'JPG BMP'.split():
            printf("%s is not picture!\a"%picfile)
            exit()

        filename = picfile

if len(sys.argv) > 2:
    outfile = sys.argv[2]

    if outfile.find('.') < 0:
        if filename.find('.') > 0:
            fn = filename.split('.')[-1]
            outfile = outfile + '.' + fn
        else: outfile = outfile + '.jpg'

    if outfile.find('/home/aistudio/work') < 0:
        outfile = '/home/aistudio/work/' + outfile

#------------------------------------------------------------
printf('%s --> %s\a'%(filename, outfile))

#------------------------------------------------------------
if filename.find('http') >= 0:
    urlstr = filename
else: urlstr = GetCSDNImageUrl(filename)

#------------------------------------------------------------
aiscmd = "!wget -q --output-document=%s %s"%(outfile, urlstr)
clipboard.copy(aiscmd)

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)
tspfocuswindow('TEASOFT:1')


#------------------------------------------------------------
#        END OF FILE : PIC2BML.PY
#============================================================

 

  结 ※


  写了python程序 pic2bml 可以快速借助于CSDN的图片存储功能,将图片串入AI Studio。对于有少量临时图片传输编程,可以增加程序开发的效率。


标签:pic2bml,#------------------------------------------------------------,AI,BML,fil
来源: https://blog.csdn.net/zhuoqingjoking97298/article/details/122265916