{"id":1101593,"date":"2025-01-08T15:51:15","date_gmt":"2025-01-08T07:51:15","guid":{"rendered":""},"modified":"2025-01-08T15:51:20","modified_gmt":"2025-01-08T07:51:20","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e7%89%87%e5%ad%98%e5%85%a5%e6%95%b0%e6%8d%ae%e5%ba%93-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101593.html","title":{"rendered":"python\u5982\u4f55\u5c06\u56fe\u7247\u5b58\u5165\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064303\/cdb08f4a-3157-400a-acdd-0f4bf9a467d4.webp\" alt=\"python\u5982\u4f55\u5c06\u56fe\u7247\u5b58\u5165\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u56fe\u7247\u5b58\u5165\u6570\u636e\u5e93\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u5e93\u6a21\u5757\u8fde\u63a5\u6570\u636e\u5e93\u3001\u7f16\u5199SQL\u8bed\u53e5\u8fdb\u884c\u6570\u636e\u63d2\u5165\u3002<\/strong> \u5728\u8fd9\u4e09\u8005\u4e2d\uff0c\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u6570\u636e\u5e93\u4e00\u822c\u65e0\u6cd5\u76f4\u63a5\u5b58\u50a8\u56fe\u50cf\u6587\u4ef6\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u683c\u5f0f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e76\u7ed9\u51fa\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528<code>read<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li>\u5c06\u8bfb\u53d6\u7684\u5185\u5bb9\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f5c\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def convert_image_to_binary(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        binary_data = file.read()<\/p>\n<p>    return binary_data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6570\u636e\u5e93\u6a21\u5757\uff0c\u5982<code>sqlite3<\/code>\u3001<code>pymysql<\/code>\u3001<code>psycopg2<\/code>\u7b49\u3002\u672c\u6587\u4ee5<code>sqlite3<\/code>\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>sqlite3<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u4f7f\u7528<code>connect<\/code>\u65b9\u6cd5\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002<\/li>\n<li>\u4f7f\u7528<code>cursor<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u6e38\u6807\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def connect_to_database(db_name):<\/p>\n<p>    connection = sqlite3.connect(db_name)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    return connection, cursor<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199SQL\u8bed\u53e5\u5e76\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u7f16\u5199SQL\u8bed\u53e5\u63d2\u5165\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u7f16\u5199<code>INSERT<\/code>\u8bed\u53e5\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26<code>?<\/code>\u6765\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>\u4f7f\u7528<code>execute<\/code>\u65b9\u6cd5\u6267\u884cSQL\u8bed\u53e5\uff0c\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\u3002<\/li>\n<li>\u63d0\u4ea4\u4e8b\u52a1\u5e76\u5173\u95ed\u8fde\u63a5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def insert_image_to_database(cursor, table_name, image_data):<\/p>\n<p>    sql = f&quot;INSERT INTO {table_name} (image_column) VALUES (?)&quot;<\/p>\n<p>    cursor.execute(sql, (image_data,))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u8d77\u7684\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def convert_image_to_binary(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        binary_data = file.read()<\/p>\n<p>    return binary_data<\/p>\n<p>def connect_to_database(db_name):<\/p>\n<p>    connection = sqlite3.connect(db_name)<\/p>\n<p>    cursor = connection.cursor()<\/p>\n<p>    return connection, cursor<\/p>\n<p>def create_table(cursor, table_name):<\/p>\n<p>    cursor.execute(f&quot;CREATE TABLE IF NOT EXISTS {table_name} (id INTEGER PRIMARY KEY, image_column BLOB)&quot;)<\/p>\n<p>def insert_image_to_database(cursor, table_name, image_data):<\/p>\n<p>    sql = f&quot;INSERT INTO {table_name} (image_column) VALUES (?)&quot;<\/p>\n<p>    cursor.execute(sql, (image_data,))<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    db_name = &#39;image_database.db&#39;<\/p>\n<p>    table_name = &#39;images&#39;<\/p>\n<p>    image_path = &#39;path_to_image.jpg&#39;<\/p>\n<p>    connection, cursor = connect_to_database(db_name)<\/p>\n<p>    create_table(cursor, table_name)<\/p>\n<p>    image_data = convert_image_to_binary(image_path)<\/p>\n<p>    insert_image_to_database(cursor, table_name, image_data)<\/p>\n<p>    connection.commit()<\/p>\n<p>    connection.close()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u8981\u8003\u8651\u9519\u8bef\u5904\u7406\u548c\u4ee3\u7801\u4f18\u5316\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u5728\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u53d1\u751f\u9519\u8bef\uff0c\u5e94\u8be5\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<\/li>\n<li><strong>\u4ee3\u7801\u4f18\u5316<\/strong>\uff1a\u53ef\u4ee5\u5c06\u5e38\u7528\u64cd\u4f5c\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4f18\u5316\u540e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def convert_image_to_binary(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>            binary_data = file.read()<\/p>\n<p>        return binary_data<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error converting image to binary: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>def connect_to_database(db_name):<\/p>\n<p>    try:<\/p>\n<p>        connection = sqlite3.connect(db_name)<\/p>\n<p>        cursor = connection.cursor()<\/p>\n<p>        return connection, cursor<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error connecting to database: {e}&quot;)<\/p>\n<p>        return None, None<\/p>\n<p>def create_table(cursor, table_name):<\/p>\n<p>    try:<\/p>\n<p>        cursor.execute(f&quot;CREATE TABLE IF NOT EXISTS {table_name} (id INTEGER PRIMARY KEY, image_column BLOB)&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error creating table: {e}&quot;)<\/p>\n<p>def insert_image_to_database(cursor, table_name, image_data):<\/p>\n<p>    try:<\/p>\n<p>        sql = f&quot;INSERT INTO {table_name} (image_column) VALUES (?)&quot;<\/p>\n<p>        cursor.execute(sql, (image_data,))<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error inserting image to database: {e}&quot;)<\/p>\n<p>def main():<\/p>\n<p>    db_name = &#39;image_database.db&#39;<\/p>\n<p>    table_name = &#39;images&#39;<\/p>\n<p>    image_path = &#39;path_to_image.jpg&#39;<\/p>\n<p>    connection, cursor = connect_to_database(db_name)<\/p>\n<p>    if connection and cursor:<\/p>\n<p>        create_table(cursor, table_name)<\/p>\n<p>        image_data = convert_image_to_binary(image_path)<\/p>\n<p>        if image_data:<\/p>\n<p>            insert_image_to_database(cursor, table_name, image_data)<\/p>\n<p>        connection.commit()<\/p>\n<p>        connection.close()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u5c06\u56fe\u7247\u5b58\u5165\u6570\u636e\u5e93\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5305\u62ec\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u3001\u8fde\u63a5\u6570\u636e\u5e93\u3001\u7f16\u5199SQL\u8bed\u53e5\u5e76\u63d2\u5165\u6570\u636e\u7b49\u6b65\u9aa4\u3002\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u8fd9\u4e9b\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u8d77\uff0c\u5b9e\u73b0\u56fe\u7247\u7684\u5b58\u50a8\u3002\u540c\u65f6\uff0c\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u6dfb\u52a0\u4e86\u9519\u8bef\u5904\u7406\u673a\u5236\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u987a\u5229\u5b9e\u73b0\u56fe\u7247\u7684\u5b58\u50a8\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u53ef\u5b58\u50a8\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5c06\u56fe\u7247\u5b58\u5165\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5408\u9002\u7684\u683c\u5f0f\u3002\u901a\u5e38\uff0c\u4f7f\u7528Python\u7684PIL\uff08Pillow\uff09\u5e93\u6765\u5904\u7406\u56fe\u50cf\uff0c\u53ef\u4ee5\u5c06\u56fe\u7247\u6253\u5f00\u5e76\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u3002\u53ef\u4ee5\u4f7f\u7528<code>io.BytesIO<\/code>\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u5b57\u8282\u6570\u636e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport io\n\nimage = Image.open(&#39;path_to_image.jpg&#39;)\nbyte_array = io.BytesIO()\nimage.save(byte_array, format=&#39;JPEG&#39;)\nbyte_data = byte_array.getvalue()\n<\/code><\/pre>\n<p><strong>\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u56fe\u7247\u65f6\uff0c\u5e94\u8be5\u9009\u62e9\u54ea\u79cd\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u56fe\u7247\u65f6\uff0c\u901a\u5e38\u4f7f\u7528BLOB\uff08Binary Large Object\uff09\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u79cd\u7c7b\u578b\u5141\u8bb8\u5b58\u50a8\u5927\u91cf\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u975e\u5e38\u9002\u5408\u5b58\u653e\u56fe\u50cf\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u5c06\u56fe\u7247\u7684\u5143\u6570\u636e\uff08\u5982\u6587\u4ef6\u540d\u3001\u7c7b\u578b\u548c\u5927\u5c0f\uff09\u5b58\u50a8\u5728\u5176\u4ed6\u5b57\u6bb5\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u68c0\u7d22\u548c\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u5e76\u663e\u793a\u56fe\u7247\uff1f<\/strong><br \/>\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u5b58\u50a8\u7684\u56fe\u7247\u65f6\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6BLOB\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u56de\u56fe\u50cf\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>PIL<\/code>\u5e93\u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import sqlite3\nfrom PIL import Image\nimport io\n\n# \u5047\u8bbe\u5df2\u7ecf\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u83b7\u53d6\u4e86\u56fe\u50cf\u5b57\u8282\u6570\u636e\nconn = sqlite3.connect(&#39;database.db&#39;)\ncursor = conn.cursor()\ncursor.execute(&quot;SELECT image_column FROM images WHERE id = ?&quot;, (image_id,))\nimage_data = cursor.fetchone()[0]\n\n# \u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u56fe\u50cf\nimage = Image.open(io.BytesIO(image_data))\nimage.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u68c0\u7d22\u7279\u5b9aID\u7684\u56fe\u50cf\u5e76\u663e\u793a\u51fa\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u56fe\u7247\u5b58\u5165\u6570\u636e\u5e93\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u5e93\u6a21\u5757\u8fde\u63a5\u6570\u636e\u5e93\u3001\u7f16\u5199SQ [&hellip;]","protected":false},"author":3,"featured_media":1101605,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101593"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1101593"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101593\/revisions"}],"predecessor-version":[{"id":1101606,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101593\/revisions\/1101606"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101605"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}