Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve screenshot and generate markdown-compliant links with Python

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Python how to achieve screenshots to generate links in line with markdown", in the daily operation, I believe that many people in Python how to achieve screenshots to generate links in line with the markdown have doubts, editor access to all kinds of information, sort out a simple and easy-to-use method of operation, hope to answer the "Python how to achieve screenshots to generate links in line with markdown" doubts help! Next, please follow the editor to study!

Train of thought

The overall idea of the program is that first of all, you need to set the storage type for the interface, that is, to choose whether there is Ali OSS or Qiniu Kodo or other cloud storage, as well as setting key and secret as well as the attributes required for different storage types, and then the interface can display the copied pictures, as well as the markdownUrl and httpUrl after the upload is successful. The interface is roughly as follows

Subinterface:

Main interface:

Realize

The overall program chooses to use python to achieve, because QT has been used before, so the framework of GUI uses pyqt5, the database uses sqlite3, as well as Ali Cloud and Qiniu sdk and so on.

The whole interface consists of a main window and a child window. The main interface initializes the database at the same time.

Class ImgFrame (QMainWindow): def _ init__ (self): super (). _ _ init__ () self.http_url = None self.markdown_url = None self.clipboard = None self.img = None # initialize the database self.db = init_db () self.init_ui () def init_ui (self): self.setGeometry (300,300,500) Self.setWindowTitle ('MarkDown-Img') widget = QWidget () setupAction = QAction (QIcon (' setup.png'), 'setup', 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 ('system') 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 # global variable conn conn = Connect cursor = connect.cursor () cursor.execute (sql) # returns the cursor return cursor

The main function of the child window is to set up the various fields needed for cloud storage, and then store them in the database.

Class secondFrame (QWidget): def _ _ init__ (self, db): super (). _ _ init__ () # self.init_ui () self.db = db self.resize (400,100) self.setWindowTitle ('Storage Settings') formlayout = QFormLayout () storageLabel = QLabel ("Storage") self.storageBox = QComboBox () self.storageBox.addItems (['Ali OSS'') Kodo') self.endpointLabel = QLabel ("endpoint") self.endpointLineEdit = QLineEdit ("") self.endpointLineEdit.setStyleSheet ("width:200px") self.qntLabel = QLabel ("Qiniu Domain name") 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 ("OK") 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.connect (self.changeLabel) confirmButton.clicked.connect (self.confirm) self.setLayout (formlayout) self.setVisible (True)

The child window is triggered by the settings menu in the menu bar of the main window

SetupAction = QAction (QIcon ('setup.png'),' setup', self) setupAction.setStatusTip ('Exit application') setupAction.triggered.connect (self.openSecondFrame) def openSecondFrame (self): self.frame = secondFrame (self.db) self.frame.show ()

The function of monitoring the clipboard monitors the implementation of the clipboard through the clipboard in pyqt

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 = 'Ali 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] = 'Ali OSS': bucket = init_ali (bucket_name, extra, key Secret) return 'Ali OSS', {' bucket': bucket, 'bucket_name': bucket_name,' extra': extra}

When the picture is uploaded successfully, the image url of markdown will be generated by default, and then the url will be set to the clipboard, and then the picture of the picture bed can be pasted in marktext, because pyqt is not allowed to set content through the clipboard in order to prevent endless loops, so we can set the clipboard through pyperclip.

Pyperclip.copy (urls ['markdown_url']) at this point, the study on "how to achieve screenshots in Python to generate links in line with markdown" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report