Files
es-bot/nft_svc/app.py
Andrey Gumirov aacb413176
All checks were successful
continuous-integration/drone/push Build is passing
Some fix
2022-05-08 01:06:05 +07:00

46 lines
1007 B
Python

import io
import random
import os
from PIL import Image
from flask import Flask, request, jsonify
import matplotlib.image as mpimg
import base64
app = Flask(__name__)
IMAGE_PATH = 'pics'
def open_image_as_array(path):
return mpimg.imread(path)
def get_encoded_img(arr):
img = Image.fromarray(arr)
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii')
return str(encoded_img)
@app.route('/getImage', methods=['GET'])
def get_image():
# print(random.choice(os.listdir(IMAGE_PATH)))
image = mpimg.imread(os.path.join(IMAGE_PATH, random.choice(os.listdir(IMAGE_PATH))))
# buffered = io.BytesIO()
# image.save(buffered, format="JPEG")
# img_str = base64.b64encode(buffered.getvalue())
return jsonify({
"code": 0,
"image": get_encoded_img(image),
"first_time": 1
})
def start_app():
app.run()
if __name__ == '__main__':
start_app()