当前位置:网站首页>Do an online GIF synthesis service at no cost

Do an online GIF synthesis service at no cost

2022-06-26 22:07:00 Hua Weiyun

Because writing articles, I often need to do some GIF. But all kinds of online GIF The service was not very good ,

So I wonder if I can make one myself . So I did such a service .

So let me talk about , I know what I did today ?

One Business needs .

First, the first step is to realize the synthesis GIF This function .

Then we need to upload multiple files .

And then GIF Synthesis .

Finally save to local . Then return to the front end with a download link .

Finally, the service is deployed to the ECS .

Two technical requirement .

Python (3.6),Flask,Image

Python The installation of , Use the following statement to install the third-party package , Or directly pycharm Install in , In short, just install it

pip install flask

pip install Image

3、 ... and Technical realization

Take a look at the project structure

image.png

Look at the code first and then analyze

import osimport uuid from PIL import Imagefrom flask import Flask, render_template, request, send_from_directory frame = 10sleepTime = 1.0 / frameapp = Flask(__name__)BASE_PATH = os.path.dirname(os.path.abspath(__file__)) +"/download"def makeGif(imgList,uid):   im = imgList[0]   file_name = BASE_PATH +"/"+uid +".gif"   im.save(file_name, save_all=True, append_images=imgList[1:], loop=0, duration=sleepTime) @app.route('/')def hello_world():    return render_template('upload.html') @app.route('/gif',methods=['POST'])def save_file():    uploaded_files = request.files.getlist("photo")    uid = str(uuid.uuid4())    imgList = []    for file in uploaded_files:  #  Change here to store multiple files         img = Image.open(file)        imgList.append(img)    makeGif(imgList,uid)    download_url = "/download/"+uid+".gif"    return render_template("download.html",download_url = download_url) @app.route("/download/<filename>", methods=["GET"])def download_file(filename):    return send_from_directory(BASE_PATH, filename, as_attachment=True) if __name__ == '__main__':    app.run()

Technical point :

1.uid Use

As a website , It may be used by many people at the same time, so I use it here uid As a unique identity .

2.Image Use

Image It is mainly used to synthesize GIF The main function of , Change the time of the picture duration Is to control the display time of each picture , You can set it yourself .

3.Flask Use

I haven't used it before flask. So some rendering of web pages is not very clear , But it's also very simple . As a placeholder, then pass the parameters .

Full code download address : Make your own online gif Composite services ,soeasy-Python Document resources -CSDN download

Four 、 Service deployment

First step Choose any service provider and buy a cloud server

The second step , Deployment environment install Python, establish Python Environmental Science , This step is also a routine operation , According to your own operating system installation environment .

Install third party package , Use the same command as above .

The third step , Start the server Use the following command

python main.py &

Put the service in the background , Otherwise, if you turn off the console, the service will be turned off .

Step four Start visiting Input to the browser http://ip:port/,ip It's the public network ip,port It's the external port you just opened

Replace your local ip And port , The following screen will appear , After selecting files , Click Create GIF.

Be careful : To choose more pictures , otherwise gif How to move

Click to return to the interface , There will be a download link . Just click download , You will get the following picture

5、 ... and 、 Points that can be optimized

1、 Abnormal inspection

Like synthesis , Save exception , Add exception handling to make the program more robust .

2、 Regular file cleaning

Currently synthesized gif It was not cleaned up , Will always be stored on the server , So do a scheduled task of automatic cleaning , Prevent the hard disk from bursting .

3、 Yes HTML Optimize your page .

Now the interface is so ugly , You can beautify the interface , Make users look more comfortable , There's nothing I can do about this

4、 Change how you deploy

Now it's used flask The default server . It is not recommended to use . Instability will occur when the flow is high . It can be replaced by the deployment scheme in production mode , But now it's enough to play by yourself , After all, it is not to serve all mankind .

5、 Some parameters of synthesis can be opened

Now the synthesis is to write dead parameters , The parameters can be opened and controlled by the user , For example, the changing time of each picture , The generated picture cycle effect and so on , It's more flexible .

6、 Image formatting

Now the size of the picture is not formatted , Later, you can unify the size of the picture according to your needs , Then synthesize .

6、 ... and , summary

This time is mainly to do a GIF The function of .

Used Python Of image library .

Then you have to use flask Of web function .

It's simple , They are the assembly of basic functions .

All in all : You can better .



原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262200389116.html