{"id":1091881,"date":"2025-01-08T14:15:54","date_gmt":"2025-01-08T06:15:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091881.html"},"modified":"2025-01-08T14:15:58","modified_gmt":"2025-01-08T06:15:58","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%8016%e4%bd%8d%e5%9b%be%e5%83%8f-dat-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091881.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f0016\u4f4d\u56fe\u50cf.dat"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205159\/29f02baf-9067-41a5-9113-4d923eb0e68d.webp\" alt=\"python\u5982\u4f55\u6253\u5f0016\u4f4d\u56fe\u50cf.dat\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u6253\u5f0016\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5408\u9002\u7684\u5e93\u3001\u89e3\u6790\u6587\u4ef6\u5934\u3001\u8bfb\u53d6\u6570\u636e\u5e76\u5904\u7406\u56fe\u50cf\u3002<\/strong> \u4f7f\u7528Python\u6253\u5f0016\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\uff0c\u53ef\u4ee5\u501f\u52a9numpy\u3001struct\u548cPIL\u7b49\u5e93\u3002\u9996\u5148\u9700\u8981\u4e86\u89e3.dat\u6587\u4ef6\u7684\u683c\u5f0f\u548c\u7ed3\u6784\uff0c\u901a\u5e38.dat\u6587\u4ef6\u5305\u542b\u6709\u6587\u4ef6\u5934\u4fe1\u606f\u548c\u56fe\u50cf\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u5e76\u5904\u740616\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e86\u89e3.dat\u6587\u4ef6\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>.dat\u6587\u4ef6\u683c\u5f0f\u901a\u5e38\u5305\u542b\u6587\u4ef6\u5934\u4fe1\u606f\u548c\u56fe\u50cf\u6570\u636e\u3002\u6587\u4ef6\u5934\u4fe1\u606f\u53ef\u80fd\u5305\u62ec\u56fe\u50cf\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u6570\u636e\u7c7b\u578b\u7b49\u3002\u89e3\u6790\u6587\u4ef6\u5934\u662f\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u5934\u4fe1\u606f<\/strong>\uff1a\u6587\u4ef6\u5934\u4fe1\u606f\u901a\u5e38\u5305\u62ec\u56fe\u50cf\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u6570\u636e\u7c7b\u578b\u7b49\u3002\u4e86\u89e3\u6587\u4ef6\u5934\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6b63\u786e\u89e3\u6790\u56fe\u50cf\u6570\u636e\u3002<\/li>\n<li><strong>\u56fe\u50cf\u6570\u636e<\/strong>\uff1a\u56fe\u50cf\u6570\u636e\u90e8\u5206\u5b58\u50a8\u7684\u662f\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u3002\u5bf9\u4e8e16\u4f4d\u56fe\u50cf\uff0c\u6bcf\u4e2a\u50cf\u7d20\u5360\u75282\u4e2a\u5b57\u8282\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6.dat\u6587\u4ef6\u5934\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u89e3\u6790\u6587\u4ef6\u5934\u4fe1\u606f\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684struct\u5e93\u6765\u89e3\u6790\u6587\u4ef6\u5934\u3002\u5047\u8bbe\u6587\u4ef6\u5934\u4fe1\u606f\u5305\u62ec\u56fe\u50cf\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u6bcf\u4e2a\u4fe1\u606f\u5360\u75284\u4e2a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>def read_header(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        header = file.read(8)  # \u8bfb\u53d6\u524d8\u4e2a\u5b57\u8282<\/p>\n<p>        width, height = struct.unpack(&#39;ii&#39;, header)  # \u89e3\u6790\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/p>\n<p>    return width, height<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bfb\u53d6\u56fe\u50cf\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u4ef6\u5934\u4fe1\u606f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5f53\u7684\u683c\u5f0f\u3002\u5047\u8bbe.dat\u6587\u4ef6\u5b58\u50a8\u7684\u662f16\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def read_image_data(file_path, width, height):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        file.seek(8)  # \u8df3\u8fc7\u6587\u4ef6\u5934<\/p>\n<p>        image_data = np.fromfile(file, dtype=np.uint16, count=width*height)<\/p>\n<p>        image_data = image_data.reshape((height, width))  # \u5c06\u6570\u636e\u91cd\u5851\u4e3a\u56fe\u50cf<\/p>\n<p>    return image_data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\u3002\u7531\u4e8e\u56fe\u50cf\u662f16\u4f4d\u7684\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u4ee5\u4fbf\u6b63\u786e\u663e\u793a\u548c\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def save_image(image_data, output_path):<\/p>\n<p>    # \u5c0616\u4f4d\u56fe\u50cf\u6570\u636e\u8f6c\u6362\u4e3a8\u4f4d\u56fe\u50cf\u6570\u636e<\/p>\n<p>    image_data_8bit = (image_data \/ 256).astype(np.uint8)<\/p>\n<p>    image = Image.fromarray(image_data_8bit)<\/p>\n<p>    image.save(output_path)<\/p>\n<p>def display_image(image_data):<\/p>\n<p>    image_data_8bit = (image_data \/ 256).astype(np.uint8)<\/p>\n<p>    image = Image.fromarray(image_data_8bit)<\/p>\n<p>    image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\u6765\u6253\u5f0016\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u5e76\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<p>def read_header(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        header = file.read(8)  # \u8bfb\u53d6\u524d8\u4e2a\u5b57\u8282<\/p>\n<p>        width, height = struct.unpack(&#39;ii&#39;, header)  # \u89e3\u6790\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/p>\n<p>    return width, height<\/p>\n<p>def read_image_data(file_path, width, height):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        file.seek(8)  # \u8df3\u8fc7\u6587\u4ef6\u5934<\/p>\n<p>        image_data = np.fromfile(file, dtype=np.uint16, count=width*height)<\/p>\n<p>        image_data = image_data.reshape((height, width))  # \u5c06\u6570\u636e\u91cd\u5851\u4e3a\u56fe\u50cf<\/p>\n<p>    return image_data<\/p>\n<p>def save_image(image_data, output_path):<\/p>\n<p>    # \u5c0616\u4f4d\u56fe\u50cf\u6570\u636e\u8f6c\u6362\u4e3a8\u4f4d\u56fe\u50cf\u6570\u636e<\/p>\n<p>    image_data_8bit = (image_data \/ 256).astype(np.uint8)<\/p>\n<p>    image = Image.fromarray(image_data_8bit)<\/p>\n<p>    image.save(output_path)<\/p>\n<p>def display_image(image_data):<\/p>\n<p>    image_data_8bit = (image_data \/ 256).astype(np.uint8)<\/p>\n<p>    image = Image.fromarray(image_data_8bit)<\/p>\n<p>    image.show()<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    file_path = &#39;path_to_your_image.dat&#39;<\/p>\n<p>    output_path = &#39;output_image.png&#39;<\/p>\n<p>    width, height = read_header(file_path)<\/p>\n<p>    image_data = read_image_data(file_path, width, height)<\/p>\n<p>    save_image(image_data, output_path)<\/p>\n<p>    display_image(image_data)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u683c\u5f0f<\/strong>\uff1a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c.dat\u6587\u4ef6\u7684\u683c\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5728\u89e3\u6790\u6587\u4ef6\u5934\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6587\u4ef6\u683c\u5f0f\u8fdb\u884c\u8c03\u6574\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u672c\u6587\u5047\u8bbe.dat\u6587\u4ef6\u5b58\u50a8\u7684\u662f16\u4f4d\u65e0\u7b26\u53f7\u6574\u6570\u56fe\u50cf\u6570\u636e\u3002\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6587\u4ef6\u683c\u5f0f\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u5728\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u9700\u8981\u5c0616\u4f4d\u56fe\u50cf\u6570\u636e\u8f6c\u6362\u4e3a8\u4f4d\u56fe\u50cf\u6570\u636e\u3002\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u8f6c\u6362\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u6210\u529f\u6253\u5f00\u5e76\u5904\u740616\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u3002\u4e86\u89e3\u6587\u4ef6\u683c\u5f0f\u5e76\u6b63\u786e\u89e3\u6790\u6587\u4ef6\u5934\u4fe1\u606f\u662f\u5173\u952e\u6b65\u9aa4\u3002\u4f7f\u7528numpy\u5e93\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\uff0c\u5e76\u501f\u52a9PIL\u5e93\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5c55\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d616\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u8bfb\u53d616\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684NumPy\u548cPIL\u5e93\uff08\u6216OpenCV\uff09\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8fd9\u4e9b\u5e93\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u800cPIL\u5219\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u53ef\u89c6\u5316\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom PIL import Image\n\n# \u5047\u8bbe\u6587\u4ef6\u540d\u4e3a&#39;image.dat&#39;\ndata = np.fromfile(&#39;image.dat&#39;, dtype=np.uint16)  # \u8bfb\u53d616\u4f4d\u6570\u636e\n# \u9700\u8981\u6839\u636e\u5b9e\u9645\u56fe\u50cf\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u8fdb\u884c\u8c03\u6574\nimage = data.reshape((height, width))  \nimg = Image.fromarray(image)\nimg.show()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528OpenCV\u53ef\u4ee5\u6253\u5f0016\u4f4d\u56fe\u50cf\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cOpenCV\u4e5f\u652f\u6301\u8bfb\u53d616\u4f4d\u56fe\u50cf\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.imread()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e\u9002\u5f53\u7684\u6807\u5fd7\u6765\u786e\u4fdd\u56fe\u50cf\u4ee516\u4f4d\u683c\u5f0f\u52a0\u8f7d\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d616\u4f4d\u56fe\u50cf\nimage = cv2.imread(&#39;image.dat&#39;, cv2.IMREAD_UNCHANGED)\ncv2.imshow(&#39;16-bit Image&#39;, image)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p><strong>16\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u4e0e\u5176\u4ed6\u56fe\u50cf\u683c\u5f0f\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><br \/>16\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u901a\u5e38\u5305\u542b\u66f4\u591a\u7684\u7070\u5ea6\u7ea7\u522b\uff0c\u76f8\u6bd4\u4e8e8\u4f4d\u56fe\u50cf\uff0c16\u4f4d\u56fe\u50cf\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u7684\u8272\u5f69\u6df1\u5ea6\u548c\u66f4\u7ec6\u817b\u7684\u7ec6\u8282\u3002\u8fd9\u4f7f\u5f9716\u4f4d\u56fe\u50cf\u5728\u533b\u7597\u6210\u50cf\u3001\u5929\u6587\u6444\u5f71\u548c\u79d1\u5b66\u7814\u7a76\u7b49\u9886\u57df\u4e2d\u975e\u5e38\u91cd\u8981\u3002\u5904\u740616\u4f4d\u56fe\u50cf\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u652f\u6301\u9ad8\u52a8\u6001\u8303\u56f4\u7684\u5e93\u548c\u5de5\u5177\uff0c\u4ee5\u907f\u514d\u4fe1\u606f\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u6253\u5f0016\u4f4d\u56fe\u50cf.dat\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5408\u9002\u7684\u5e93\u3001\u89e3\u6790\u6587\u4ef6\u5934\u3001\u8bfb\u53d6\u6570\u636e\u5e76\u5904\u7406\u56fe\u50cf\u3002 \u4f7f\u7528Py [&hellip;]","protected":false},"author":3,"featured_media":1091891,"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\/1091881"}],"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=1091881"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091881\/revisions"}],"predecessor-version":[{"id":1091893,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091881\/revisions\/1091893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091891"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}