Multiple output images

This commit is contained in:
Nomi Nonsense (Nonszy) 2023-09-08 23:46:49 +07:00
parent ec7e692c25
commit 7724ce0da3
2 changed files with 29 additions and 11 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
.env
images/output/*
images/output/

22
main.py
View File

@ -13,6 +13,13 @@ from email.mime.image import MIMEImage
app = Flask(__name__)
def add_textimg(text):
def generatePath():
some_id = random.randint(2000, 9000)
return f"images/output/pos-{str(some_id)}.png"
path = generatePath()
try:
img = Image.open("images/main/black-suit.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("fonts/impact.ttf", 64)
@ -21,8 +28,17 @@ def add_textimg(text):
text_color = (255, 255, 255)
draw.text(pos, text, fill=text_color, font=font)
img.save("images/output/pos.png")
if not os.path.exists("images/output"):
os.mkdir("images/output")
while os.path.exists(path):
path = generatePath()
img.save(path)
img.close()
except Exception as e:
print("Failed to add image")
print(str(e))
@app.route('/hello', methods=['GET'])
def hello():
@ -39,6 +55,7 @@ def send_email():
if mail_receiver is None:
return jsonify({
"status": 400,
"error": "You must include an email address param query to send to email"
}), 400
@ -60,7 +77,7 @@ def send_email():
# attach picure
add_textimg("Your otp code is " + str(randis))
pict = open("images/output/pos.png", "rb").read()
zmail.attach(MIMEImage(pict, name="black man suit"))
zmail.attach(MIMEImage(pict, name="black-man-wearing-suit.png"))
# creating context
context = ssl.create_default_context()
@ -70,6 +87,7 @@ def send_email():
smtp.sendmail(mail_sender, mail_receiver, zmail.as_string())
return jsonify({
"status": 200,
"message": "Success sending email",
"email": {
"sender": mail_sender,