Get the App
SLTechnology News&Howtos  ›  Development  › 

How to build Gitee drawing bed by Python

Shulou Source: shulou.com Published: 2022-06-03 05:01:30 10月03日 Update

This article is to share with you about how Python builds the Gitee graph bed. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Build a new warehouse

If you do not have an account, first go to the Gitee home page to register the account, and then build a new warehouse named resource and the path res (res is used to make the picture link look more concise)

After the creation is completed, you need to initialize the Readme.md file and copy the address (to use the Gitee Pages service):

Then open the service options:

Click Gitee Pages:

Click "start" to start the service:

Clone warehouse

Find a location on your computer to create a folder, and use the Git Bash command in the folder to clone the repository locally:

Git clone https://gitee.com/xxx/xxx.git

After the cloning is completed, a folder named res is generated locally. You can delete the Readme.md file in the folder and create a new empty folder upload_images in the res folder.

Create an empty folder temp in the same directory as res to store the files to be uploaded:

Get the "API Key" of TinyPng

Go to the TinyPng home page (https://tinypng.com/) and register in the upper right corner:

Enter the email address:

Open email verification, click "Log in with magic link" in the email, click the place where you just registered, and select "Account page":

After successful registration, the following page appears to enable and copy "API Key"

TinyPng supports free conversion of 500 pictures per month, and repeated images are compressed only once, which is enough for people with average number of pictures inserted. If you feel that 500 pictures a month is not enough and you don't want to spend money, you can sign up for several more accounts.

Install the required Python package

The script requires two packages: tinify and GitPython

Open the cmd command prompt and enter the installation instructions:

Pip install-upgrade tinifypip install gitpython

If it fails, you can try to install locally.

Write Python scripts

Create a new upload.py in the same directory as res:

Open the py file with python's IDE and write the following code:

Import randomimport timeimport osimport shutilfrom git import Repoimport tinifyrepo = Repo ('. / res') # create version library object tinify.key ='*'# paste here the image format supported by API Keyexts = ['.png', '.jpg', '.bmp'] # supported by compression = ['.png' '.jpg'] # supports compressed image format srcdir ='. / temp' # Source folder dstdir ='. / res/upload_images' # destination folder url = 'https://xxx.gitee.io/res/upload_images/' # Picture bed path (must add "/" at the end) Replace xxx with your own user name def random_hex (length): result = hex (random.randint). Replace ('0x girls'). Lower () if (len (result)

Tags: File folder picture warehouse mobile support service success image format directory path input home page content sibling command address more destination Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Shulou Information MySQL Linux