There are many tutorials on the web that use Python’s Pillow library for image compression. It is simple to use, but the compression effect is obviously unnatural because the compression algorithm adopted by the Pillow library is not optimized. This series implements a simple compression tool using PngQuant lossy compression with a compression rate of up to 80% and no significant differences in compressed images.

The previous article used the PngQuant image compression tool for compression and built the Picom package using the Click command line tool. The main function of this article is to upload pictures.

The realization of picture uploading function

After compressing the image with pngQuant, you get an image with the suffix -fs8.png. To upload an image to the cloud, you simply send the file to the image storage service through the API.

First to achieve sm.MS website picture upload. The official API document is provided, and you can do it by passing in parameters according to the document, so easy.

image.png

Parameter description of the upload interface:

  1. The content-type for multipart/form – data;
  2. Authorization is optional. If no user management is required, leave this parameter blank.
  3. The parameter passed to the file is called smfile.

The corresponding Python code:

api_addr = 'https://sm.ms/api/v2/upload'

files = {

 "smfile": open(file, 'rb')

}

res = requests.post(url, files=files)

Copy the code

Gets the address of the uploaded image

After the file is uploaded, the URL of the image needs to be obtained from the response result of the API. The response results of SM.MS include two cases:

  1. If an image has been uploaded once, the hash value of the image identifies the duplicate image. The code in the response will be “image_repeated”, and you can retrieve the address of the existing image via “images”.
{

 "code""image_repeated".

 "images""https:..."

}

Copy the code
  1. When the image is uploaded for the first time and the code is SUCCESS, the url in data can be used to obtain the image address.
{

 "code""success".

 "data": {

        "url""https:..."

    }

}

Copy the code

So add judgment to the upload function to get the image URL:

resp = res.json()

code = resp.get('code')

if code == 'image_repeated':

 url = resp["images"]

 return url

elif code == 'success':

 return resp["data"] ["url"]

Copy the code

Sometimes it is not a single image that needs to be uploaded, but the images under the whole folder, and multiple images need to be uploaded at one time. Therefore, one session can be shared, and the corresponding image uploading class is as follows:

image.png

Add for the command line--uploadOptional parameters

Now I can choose whether or not to upload the image to the web, and if SO, add the –upload parameter; If you do not need to upload, do not pass this command line argument:

picom elephant.png -f --upload
Copy the code

Add –upload Optional parameter Simply add an option to the CLI function and add the upload code:

image.png

The final run result is as follows:

image.png

Record and upload the image address through YAML

The URL of the image uploaded above is displayed on the command line, which is fine if used once, but the URL of the image needs to be saved if used repeatedly.

The account function of SM.ms can manage uploaded pictures. So if you have an SM.ms account, you don’t need this save feature. However, if you don’t want to register an account, a less radical solution is to create a file called uploaded_img.yaml in the current folder and save the addresses of the uploaded images.

You can select whether the record is local by creating a new option – Record. Python can use the Pyyaml library to manipulate yamL files and store uploaded image data. It can also be saved in JSON format, which looks like this:

{

 "upload": [

  {

   "name""elephant.png".

   "url" : "http://img-server/ofos.png"

  }

 ]

}

Copy the code

Because this function is not particularly important, even can say chicken ribs, will not post code.

conclusion

picom-upload

Uploading images is very simple, using the basic requests library to upload images from each service provider. If you find the Requests library slow, you can use AIOHTTP for asynchronous transfers. This tool does not consider speeding up the transmission speed for the time being, because many of the current chart bed services are operating on a small budget with little profitability. In order to provide long-term service, we should try our best to give others less pressure.

This article is formatted using MDNICE