Answer the question
In order to leave comments, you need to log in
How can Python save a screenshot to a db?
Actually, you need to take a screenshot and write it to Mysql (BLOB)
I know that it is not logical to store images in the database)) but it is necessary
. In Python, do not judge a beginner strictly)
I do this:
#!/usr/bin/env python
import mysql.connector
from mysql.connector import Error
from PIL import Image, ImageGrab
from StringIO import StringIO
def Screen():
screen = ImageGrab.grab()
buf = StringIO()
screen.save(buf,'PNG')
return buf.getvalue()
try:
db=mysql.connector.connect(host='x.x.x.',database='test',user='test',password='qwerty')
if db.is_connected():
cur = db.cursor()
data = Screen()
cur.execute("INSERT INTO ts (screen) VALUES (%s)"% (data))
db.commit()
except Error as e:
print "MySQL Error [%d]: %s" % (e.args[0], e.args[1])
finally:
db.close()
MySQL Error [-1]: Failed getting Error information (UnicodeDecodeError('utf8', "Invalid utf8 character string: '\x89PNG'", 32, 33, 'invalid start byte'))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question