Python プログラムを使用して画像を MySQL に保存する方法
環境
Python 3.7.4 pymysql 8.0.11 MySQL Community Server
画像の読み取り
画像をバイナリ形式で読み取る
with open("./test.jpg", "rb") as file: image = file.read()
画像を保存するテーブルの作成
画像フィールドの属性は次のとおりです。 longblog
、つまり long バイナリ ラージ オブジェクト
def create_image_table(self): sql = 'create table if not exists picture ( \ image longblob);' try: self.cursor.execute(sql) self.connection.commit() except pymysql.Error: print(pymysql.Error)
は MySQL に格納されます
画像データをバイナリ形式で MySQL
def insert_image(self, image): sql = "insert into picture(image) values(%s)" self.cursor.execute(sql, image) self.connection.commit()
MySQLクエリで取得した画像データをpictureとして保存
画像をバイナリ形式で記述する
def get_image(self, path): sql = 'select * from picture' try: self.cursor.execute(sql) image = self.cursor.fetchone()[0] with open(path, "wb") as file: file.write(image) except pymysql.Error: print(pymysql.Error) except IOError: print(IOError)
実装コード
import pymysql class Database(): ''' Description: database demo to store image in MySQL RDBMS Attributes: None ''' def __init__(self): self.connection = pymysql.connect(host='<host name>',user='<user name>',passwd='<password>',db='<database name>',charset='utf8') self.cursor = self.connection.cursor() ''' Description: create table to store images Args: None Return: None ''' def create_image_table(self): sql = 'create table if not exists picture ( \ image longblob);' try: self.cursor.execute(sql) self.connection.commit() except pymysql.Error: print(pymysql.Error) ''' Description: insert image into table Args: image: image to store Returns: None ''' def insert_image(self, image): sql = "insert into picture(image) values(%s)" self.cursor.execute(sql, image) self.connection.commit() ''' Description: get image from database Args: path: path to save image Returns: None ''' def get_image(self, path): sql = 'select * from picture' try: self.cursor.execute(sql) image = self.cursor.fetchone()[0] with open(path, "wb") as file: file.write(image) except pymysql.Error: print(pymysql.Error) except IOError: print(IOError) ''' Description: destruction method Args: None Returns: None ''' def __del__(self): self.connection.close() self.cursor.close() if __name__ == "__main__": database = Database() # read image from current directory with open("./test.jpg", "rb") as file: image = file.read() database.create_image_table() database.insert_image(image) database.get_image('./result.jpg')
テスト結果
#
以上がPython プログラムを使用して画像を MySQL に保存する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











LinuxターミナルでPythonバージョンを表示する際の許可の問題の解決策PythonターミナルでPythonバージョンを表示しようとするとき、Pythonを入力してください...

PythonのPandasライブラリを使用する場合、異なる構造を持つ2つのデータフレーム間で列全体をコピーする方法は一般的な問題です。 2つのデータがあるとします...

Pythonパラメーター注釈の代替使用Pythonプログラミングでは、パラメーターアノテーションは、開発者が機能をよりよく理解して使用するのに役立つ非常に便利な機能です...

Pythonスクリプトは、特定の場所のカーソル位置への出力をどのようにクリアしますか? Pythonスクリプトを書くときは、以前の出力をカーソル位置にクリアするのが一般的です...

Python:Hourglassグラフィック図面と入力検証この記事では、Python NoviceがHourglass Graphic Drawingプログラムで遭遇する可変定義の問題を解決します。コード...

毎日のネットワークインタラクションでPythonを使用したクラッキング検証コードの調査、検証コードは、自動化されたプログラムの悪意のある操作を防ぐための一般的なセキュリティメカニズムです...

Pythonクロスプラットフォームデスクトップアプリケーション開発ライブラリの選択多くのPython開発者は、WindowsシステムとLinuxシステムの両方で実行できるデスクトップアプリケーションを開発したいと考えています...

Pythonでは、文字列を介してオブジェクトを動的に作成し、そのメソッドを呼び出す方法は?これは一般的なプログラミング要件です。特に構成または実行する必要がある場合は...
