您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么實現(xiàn)截圖生成符合markdown的鏈接”,在日常操作中,相信很多人在Python怎么實現(xiàn)截圖生成符合markdown的鏈接問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python怎么實現(xiàn)截圖生成符合markdown的鏈接”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
程序的整體思路是,首先需要給界面來設置存儲的類型,即是選擇存在阿里OSS還是七牛的Kodo還是其他的云存儲,還有設置key和secret以及不同存儲類型所需要的屬性,接著界面可以顯示復制的圖片,以及上傳成功后的markdownUrl和httpUrl,界面大體如下
子界面:
主界面:
程序整體選擇用python來實現(xiàn),因為之前用過QT,所以GUI的框架用的是pyqt5,數(shù)據(jù)庫用的是sqlite3,還有阿里云和七牛的sdk等。
整個界面有一個主窗口和一個子窗口構成,主界面在初始化的同時初始化數(shù)據(jù)庫
class ImgFrame(QMainWindow): def __init__(self): super().__init__() self.http_url = None self.markdown_url = None self.clipboard = None self.img = None # 初始化數(shù)據(jù)庫 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('系統(tǒng)') 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
子窗口的主要作用就是設置云存儲所需要的各種字段,然后存儲到數(shù)據(jù)庫中
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)
子窗口是通過主窗口的菜單欄的設置菜單觸發(fā)
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()
監(jiān)聽剪貼板的功能通過pyqt中的clipboard來監(jiān)聽剪貼板的實現(xiàn)
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為了防止死循環(huán),不允許通過剪貼板設置內(nèi)容,所以我們可以通過pyperclip來設置剪貼板
pyperclip.copy(urls['markdown_url'])
到此,關于“Python怎么實現(xiàn)截圖生成符合markdown的鏈接”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。