目录
  • 背景
  • 思路
  • 实现

背景

之前是用的是typora来写的文章,最近typora最近开始收费了,所以就不想用了,于是找到了一个替代品marktext,感觉跟typora差不多

整体样子就像上面,简约风,个人挺喜欢的。唯一的一个问题就是粘贴图片的时候,图片只能放在本地,虽然marktext有图片上传的功能,但是只支持github的图床,设置了过后经常会上传失败,导致还是存在本地,存在本地的弊端就是文章在转移的需要把图片都带上或者复制到掘金等平台的时候,图片会失效。于是打算用python写个自动生成markdownurl的程序。

思路

程序的整体思路是,首先需要给界面来设置存储的类型,即是选择存在阿里oss还是七牛的kodo还是其他的云存储,还有设置key和secret以及不同存储类型所需要的属性,接着界面可以显示复制的图片,以及上传成功后的markdownurl和httpurl,界面大体如下

子界面:

主界面:

实现

程序整体选择用python来实现,因为之前用过qt,所以gui的框架用的是pyqt5,数据库用的是sqlite3,还有阿里云和七牛的sdk等。

整个界面有一个主窗口和一个子窗口构成,主界面在初始化的同时初始化数据库

class imgframe(qmainwindow):
    def __init__(self):
        super().__init__()
        self.http_url = none
        self.markdown_url = none
        self.clipboard = none
        self.img = none
        # 初始化数据库
        self.db = init_db()
        self.init_ui()

    def init_ui(self):
        self.setgeometry(300, 300, 500, 500)
        self.setwindowtitle('markdown-img')
        widget = qwidget()
        setupaction = qaction(qicon('setup.png'), '设置', self)
        setupaction.setstatustip('exit application')
        setupaction.triggered.connect(self.a)
        menubar = qmenubar(self)
        menubar.setgeometry(qtcore.qrect(0, 0, 251, 23))
        menubar.setobjectname("menubar")
        setup = menubar.addmenu('系统')

        setup.addaction(setupaction)
        menubar.setvisible(true)
        menubar.setnativemenubar(false)
        self.setmenubar(menubar)
        self.img = qlabel()
        layout = qvboxlayout()
        layout.addwidget(markdown_widget(self))
        layout.addwidget(url_widget(self))
        layout.addwidget(self.img)
        layout.setalignment(qt.aligncenter)

        self.clipboard = qapplication.clipboard()
        self.clipboard.datachanged.connect(self.paste)

        widget.setlayout(layout)
        self.setcentralwidget(widget)
        self.show()
def init_db():
    connect = sqlite3.connect('markdown-img.db')
    global conn
    #全局变量conn
    conn = connect
    cursor = connect.cursor()
    cursor.execute(sql)
    #返回游标
    return cursor

子窗口的主要作用就是设置云存储所需要的各种字段,然后存储到数据库中

class secondframe(qwidget):
    def __init__(self, db):
        super().__init__()
        # self.init_ui()
        self.db = db
        self.resize(400, 100)
        self.setwindowtitle('存储设置')

        formlayout = qformlayout()
        storagelabel = qlabel("存储")
        self.storagebox = qcombobox()
        self.storagebox.additems(['阿里oss', '七牛kodo'])

        self.endpointlabel = qlabel("endpoint")
        self.endpointlineedit = qlineedit("")
        self.endpointlineedit.setstylesheet("width:200px")

        self.qntlabel = qlabel("七牛域名")
        self.qnlineedit = qlineedit("")
        self.qnlineedit.setstylesheet("width:200px")

        keylabel = qlabel("access_key")
        self.keylineedit = qlineedit("")
        self.keylineedit.setstylesheet("width:350px")

        secretlabel = qlabel("secret_key")

        self.secretlineedit = qlineedit()
        self.secretlineedit.setstylesheet("width:350px")
        self.secretlineedit.settext('')

        bucketlabel = qlabel("bucket_name")
        self.bucketlineedit = qlineedit("")

        confirmbutton = qpushbutton("确定")

        formlayout.addrow(storagelabel, self.storagebox)
        formlayout.addrow(bucketlabel, self.bucketlineedit)
        formlayout.addrow(self.endpointlabel, self.endpointlineedit)
        formlayout.addrow(self.qntlabel, self.qnlineedit)
        self.qntlabel.setvisible(false)
        self.qnlineedit.setvisible(false)

        formlayout.addrow(keylabel, self.keylineedit)
        formlayout.addrow(secretlabel, self.secretlineedit)
        formlayout.addrow(confirmbutton)

        self.storagebox.currentindexchanged[int].connect(self.changelabel)
        confirmbutton.clicked.connect(self.confirm)

        self.setlayout(formlayout)
        self.setvisible(true)

子窗口是通过主窗口的菜单栏的设置菜单触发

    setupaction = qaction(qicon('setup.png'), '设置', self)
    setupaction.setstatustip('exit application')
    setupaction.triggered.connect(self.opensecondframe)
    def opensecondframe(self):
        self.frame = secondframe(self.db)
        self.frame.show()        

监听剪贴板的功能通过pyqt中的clipboard来监听剪贴板的实现

    self.clipboard.datachanged.connect(self.paste)
    def paste(self):
        data = self.clipboard.mimedata()
        if data.hasimage():
            print(data.formats())
            pixmap = self.clipboard.pixmap()
            height = pixmap.height()
            width = pixmap.width()
            if height > 300 and width > 300:
                self.img.setpixmap(shrink_img(pixmap))
            else:
                self.img.setpixmap(pixmap)
            filename = 'ink_' + ''.join(str(uuid.uuid1()).split('-')) + '.png'
            self.clipboard.pixmap().save(filename, 'png')
            urls = generate_url(self.upload(filename))
            print(urls)
            self.img.setscaledcontents(true)
            self.markdown_url.settext(urls['markdown_url'])
            self.http_url.settext(urls['http_url'])
            pyperclip.copy(urls['markdown_url'])
    def shrink_img(pixmap):
        scale = 0.3
        height = pixmap.height()
        width = pixmap.width()
        shrink_height = int(height * scale)
        shrink_width = int(width * scale)
        size = qsize(shrink_width, shrink_height)
        image = pixmap.toimage()
        return qpixmap.fromimage(image.scaled(size, qt.ignoreaspectratio))
  def upload(self, filename):
        type, storage = self.get_storage_data()
        if type == '阿里oss':
            url = upload2ali(storage, filename)
            return url


    def get_storage_data(self):
        self.db.execute(select_sql)
        data = self.db.fetchone()
        bucket_name = data[1]
        extra = data[2]
        key = data[3]
        secret = data[4]
        if data[0] == '阿里oss':
            bucket = init_ali(bucket_name, extra, key, secret)
            return '阿里oss', {'bucket': bucket, 'bucket_name': bucket_name, 'extra': extra}

当图片上传成功后,默认会生成markdown的图片url,然后将这个url设置到剪贴板中,然后在marktext中只需要粘贴就能贴上图床的图片,因为pyqt为了防止死循环,不允许通过剪贴板设置内容,所以我们可以通过pyperclip来设置剪贴板

pyperclip.copy(urls['markdown_url'])

目前只支持阿里oss的,七牛的也是一样,只是上传的sdk会有些不同,看官方文档即可。程序我也在慢慢完善。本文更多的是记录提供个思路

本文的所有图片都是通过这个程序生成链接然后直接到文章中

以上就是python实现截图生成符合markdown的链接的详细内容,更多关于python截图生成链接的资料请关注www.887551.com其它相关文章!