Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我很难将blob图像转换为一些扩展,如jpg或png_Python_Mysql_Flask_Web_Wtforms - Fatal编程技术网

Python 我很难将blob图像转换为一些扩展,如jpg或png

Python 我很难将blob图像转换为一些扩展,如jpg或png,python,mysql,flask,web,wtforms,Python,Mysql,Flask,Web,Wtforms,main.py @app.route('/products/<name>') def products(name): cursor = mysql.connection.cursor() query = 'SELECT duty FROM staff WHERE name = %s' cursor.execute(query,(name,)) cuisine_type = cursor.fetchone()[0] query = 'SELECT

main.py

@app.route('/products/<name>')
def products(name):
    cursor = mysql.connection.cursor()
    query = 'SELECT duty FROM staff WHERE name = %s'
    cursor.execute(query,(name,))
    cuisine_type = cursor.fetchone()[0]
    query = 'SELECT * from cuisines WHERE type = %s'
    cursor.execute(query,(cuisine_type,))
    datas = cursor.fetchall()
    
    for data in datas:
        image_file = data[3]
        image_real = base64.decodebytes(data[3])
      
    return render_template('products.html',image_real=image_real,name=name,cuisine_type=cuisine_type,datas=datas)

@app.route('/product_edit/<name>',methods=['GET','POST'])
def product_edit(name):
    cursor = mysql.connection.cursor()
    query = 'SELECT duty FROM staff WHERE name = %s'
    cursor.execute(query,(name,))
    cuisine_type = cursor.fetchone()[0]
    form = Products(CombinedMultiDict((request.files, request.form)))
    if form.validate_on_submit():
        item = form.item.data
        price = form.price.data
        f = form.image.data
        filename = secure_filename(f.filename)
        if os.path.exists('photos') == False:
            os.mkdir('photos')
        else:
            pass
        f.save(os.path.join(
            UPLOAD_FOLDER,filename
        ))
        cursor = mysql.connection.cursor()
        query = 'INSERT INTO cuisines(type,item,price,img) VALUES(%s,%s,%s,%s)'
        values = (cuisine_type,item,price,filename)
        cursor.execute(query,values)
        mysql.connection.commit()
        cursor.close()
##        return 'Success'
        return redirect(url_for('products',name=name,cuisine_type=cuisine_type))
    return render_template('product_edit.html',cuisine_type=cuisine_type,name=name,form=form)
class Products(FlaskForm):
  
    item = StringField('Item',[validators.Length(max=100)])
    price = DecimalField('Price per unit', validators=[validators.Optional()], places=1)
    image = FileField('image',validators=[FileRequired()])
    submit = SubmitField('Add')    
products.html

{% for data in datas %} 
            <tr>
                    
                    <td>{{ data[1] }}</td>
                    <td>{{ data[2] }}</td>
                    <td><img src=image_real></td>
                    <td>
                        <a href="" id="view_btn"><button type="button" class="btn btn-primary">View</button></a>
                        <a href="" id="edit_btn"><button type="button" class="btn btn-warning">Edit</button></a>
                        <a href="" id="del_btn"><button type="button" class="btn btn-danger">Delete</button></a>
                    </td>
               
                </th>
            </tr>
            {% endfor %}
{%用于数据%中的数据]
{{数据[1]}
{{数据[2]}
{%endfor%}
我的目标是通过使用
data[3]
来表示,在html模板的表上显示存储在数据库phpmyAdmin中的图像。但是,当我运行程序时,图像无法显示。如何将blob属性转换为jpg或png,以便在html网页上显示图像?是否有任何方法可以通过使用for循环
数据
来表示图像文件名?我想显示数据库中相应的特定图像。对于编码部分,我可能会问如何使用For循环遍历每个二进制数据,将它们转换为图像文件名,以便在web模板上显示图像

如果您有base64编码图像,则可以在


如果您不知道存储的图像类型,请查看内置模块。

在哪里可以找到
IVBORW0KGGOAANSUHEUGAAAAUAAAAFCAIAAACDBGYAAAAHULEQIVQIHU3BSQEAAAABOBPX/NKSKDUKDUCFUCCUG4V8FABQNDSYAAASUVORK5CYII=
以及为什么代码这么长?@DominicSham这是base64编码的
png图像文件,如果您想用另一个图像运行我的示例,您需要图像文件和工具来对其进行base64编码。您可以使用Ubuntu或python内置的
base64
模块或一些在线工具(搜索base64在线编码器)。由于base64编码的工作原理(如果您想了解更多信息,请参阅RFC 3548),输出的长度与大小(字节数)成比例我可以在哪里搜索我的照片代码?@DominicSham你的代码表明你的数据库中已经有base64编码文件我的编码文件存储在数据库屏幕截图中,那么我如何调用它们在网页上显示?
    image_file = data[3]
    image_real = base64.decodebytes(data[3])
<html>
<head><title>Blue square</title></head>
<body>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAHUlEQVQIHU3BsQEAAAABoPx/NKsKdUKdUCfUCXUG4V8FAbQndsYAAAAASUVORK5CYII=" alt="blue square">
</body>
</html>