MySQL管理圖片數據2022年更新(MyS??QL 管理)
隨著(zhù)互聯(lián)網(wǎng)的管管理管理快速發(fā)展,圖片數據已經(jīng)成為了我們日常生活中不可或缺的圖片一部分,在各種網(wǎng)站和應用中,數據圖片數據的年更管理(′?`*)和存儲顯得??尤為重要,本文將介紹如何使用MySQL數據庫來(lái)管理和存儲圖片?數據,管管理管理以及在2022年的圖片更新中,MySQL為我們帶來(lái)了哪些??新的數據功能和(′_`)優(yōu)化。
MySQL數據庫的年更基本概念
MySQ(╯°□°)╯︵ ┻━┻L是一個(gè)關(guān)系型數據庫管理系統,由瑞典MySQL AB公司開(kāi)發(fā),管管理管理目前屬于Oracle公司(′▽?zhuān)?,圖片它采用了C語(yǔ)言編寫(xiě),數據支持多種操作系統,年更如Windows、管管理管理Linux、圖片Mac OSヽ(′?`)ノ等,數據M( ?ヮ?)ySQL的優(yōu)點(diǎn)包括:開(kāi)源免費、性能高、易于使用、安全性高等。
MySQL數據庫中的圖片數據存儲
1、將圖片作為二進(jìn)制數據存儲
CREATE TABLE images ( id INT AUTO_INCREMENT?? PRIMARY KEY, name VARCHAR(255) NOT NULL, data BLOB NOT NULL);
接下來(lái),可以使用Python的open="open"()函數和read()方法將圖片文件讀取為二進(jìn)制數據,并將其插入到數據庫中:
impor(′?`)t mysql.connectordef insert_image(name, file_pa??th): connection = mysql.connector.??connect(user='use??rnam??e', password='password', host='localhost', database='test') cursor = connection.cursor() with open=""(file_path, 'rb') as file: data = file.(′?ω?`)read() sql = "INSERT INTO images (name, data) VALUES (%s, %s)" values = (name, data) cursor.execute(sql??, values) connection.commit()?? cursor.close() connection.close()另一種將圖片存儲到MySQL數據庫的方法是將圖片轉換為Base64編碼的字符??串,這樣可以減少存儲空間的需求,但在顯示(shi)圖片時(shí)需要??對字符串進(jìn)行解碼,以下是將圖片轉換為Base64(′?`)編碼的字符串并存儲到數據庫的示例:
import base64import┐(′?`)┌ mysql.connec(T_T)torfrom PI??L import Imagefrom io imp(′▽?zhuān)?ort BytesIOdef convert_image_to_base64(file_path): img = Imag(°ロ°) !e.open(file_path) img_byte_arr = Bytes(?_?;)IO() img.save(img_byte_arr, format=img.format) img??_base64 = base64.b64encode(img_ヽ(′ー`)ノbyte_arr.getvalue()).decode('ut??f-8') return img_base64def insert_im(′▽?zhuān)?)age(name, file_path): connection = mysql.connector.connect(user='username', password='password', host='localhost', database='t??est') cursor = co(′?_?`)nnection.cursor() img_base64 = convert_image_to_base??64(file_path) sql = "INSERT INTO images (name, data) VALUES (%s, %s)" values = (nam(//ω//)e, img_base64) cursor.( ???)execute(sql, values) connection.commit() cursor.close() co?nnection.close()MySQL數??據庫中的圖片數據查詢(xún)與展示
1、從數據庫中查詢(xún)圖片數據并顯示在網(wǎng)頁(yè)上
要從數據庫中查詢(xún)圖片數據并顯示在網(wǎng)頁(yè)上,可以使用以下步驟:
在HTML頁(yè)面中創(chuàng )建(jian)一個(gè)<i(′?`*)mg>標簽;
在后端代ヽ(′▽?zhuān)?ノ碼中查詢(xún)數??據庫并獲取圖片數據的Base64編碼字符串;
將Base64編碼字符串解碼為二進(jìn)制數據;
將二進(jìn)制數(shu)據寫(xiě)入到響應對象中;
在瀏覽器中查??看圖片。
示例代碼如下:
from flask import Flask??, send_file, render_template_string, url_forimport mysql.connectorfrom PIL import Imag?e from io import BytesIOimport base64import osdef convert_image_to_base64(file_path): ...def get_image(name): connection = mysql.connector.connect(user='username', password='password', host??='localhost', database='test') cursor = connection??.cursor() img_base(′ω`)64 = "&qヾ(′▽?zhuān)??uot; try: cur(′?ω?`)sor.execute("??;SELECT data FROM images WHERE name=%s", (name)) result = cursor.fetchone() if resul??t: img_base64(T_T) = result[0] finally: cursor.close() connection.close() return img_base64if __name__ == '__m(′?ω?`)ain__': app = Flask(__name__) @app.route('/(⊙_⊙)image/<name>') def image(name): img_base64 = get_image(name) if img_base6??4: img = Im??age.open(Byt??esIO(base64.b64decode(img_base64))) return send_file(img, mimetype='image/png') else: return render_template_s(T_T)tring("??;<h1>Image not found</h1>") app.ru(⊙_⊙)n()```

網(wǎng)站二維碼
導航
電話(huà)
短信
咨詢(xún)
地圖
分享