forked from gav/es-bot
47 lines
1.0 KiB
Python
47 lines
1.0 KiB
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)))
|
|
file = 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())
|
|
image = base64.encodebytes(open(file, 'rb').read()).decode('ascii')
|
|
return jsonify({
|
|
"code": 0,
|
|
"image": image,
|
|
"first_time": 1
|
|
})
|
|
|
|
|
|
def start_app():
|
|
app.run()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
start_app()
|