{"id":994303,"date":"2024-12-27T08:54:13","date_gmt":"2024-12-27T00:54:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994303.html"},"modified":"2024-12-27T08:54:17","modified_gmt":"2024-12-27T00:54:17","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%98%e5%88%b6%e6%8b%bc%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994303.html","title":{"rendered":"\u5982\u4f55\u7528python\u7ed8\u5236\u62fc\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071343\/387e924f-c64f-4e73-af16-13011df47f48.webp\" alt=\"\u5982\u4f55\u7528python\u7ed8\u5236\u62fc\u56fe\" \/><\/p>\n<p><p> <strong>\u7528Python\u7ed8\u5236\u62fc\u56fe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528matplotlib\u5e93\u3001PIL\u5e93\u6216pygame\u5e93\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c<strong>matplotlib<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u751f\u6210\u9759\u6001\u7684\u56fe\u50cf\u548c\u7b80\u5355\u7684\u62fc\u56fe\uff0c<strong>PIL<\/strong>\uff08Pillow\uff09\u53ef\u4ee5\u5904\u7406\u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c\uff0c<strong>pygame<\/strong>\u5219\u9002\u5408\u5236\u4f5c\u4ea4\u4e92\u5f0f\u7684\u62fc\u56fe\u6e38\u620f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<strong>matplotlib<\/strong>\u5e93\u6765\u7ed8\u5236\u62fc\u56fe\uff0c\u56e0\u4e3a\u5b83\u6613\u4e8e\u4f7f\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u7ed8\u5236\u62fc\u56fe\u7684\u5173\u952e\u5728\u4e8e\u5904\u7406\u56fe\u50cf\u7684\u5206\u5272\u548c\u91cd\u65b0\u7ec4\u5408\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u56fe\u50cf\u5206\u6210\u591a\u4e2a\u5c0f\u5757\uff0c\u7136\u540e\u968f\u673a\u6253\u4e71\u8fd9\u4e9b\u5c0f\u5757\u7684\u4f4d\u7f6e\uff0c\u518d\u901a\u8fc7\u7528\u6237\u4ea4\u4e92\u7684\u65b9\u5f0f\u5c06\u5b83\u4eec\u6062\u590d\u5230\u539f\u6765\u7684\u987a\u5e8f\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u901a\u5e38\u9700\u8981\u5b89\u88c5matplotlib\u548cPIL\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff0c\u4ee5\u4fbf\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u7ed8\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a0\u8f7d\u548c\u5207\u5272\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u56fe\u50cf\u5e76\u5c06\u5176\u5207\u5272\u6210\u591a\u4e2a\u5c0f\u5757\u3002\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u6765\u6253\u5f00\u56fe\u50cf\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\uff0c\u4ee5\u4fbf\u4e8e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def load_and_cut_image(image_path, num_pieces):<\/p>\n<p>    img = Image.open(image_path)<\/p>\n<p>    img = img.resize((400, 400))  # \u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/p>\n<p>    img_array = np.array(img)<\/p>\n<p>    piece_size = img_array.shape[0] \/\/ num_pieces<\/p>\n<p>    pieces = []<\/p>\n<p>    for i in range(num_pieces):<\/p>\n<p>        for j in range(num_pieces):<\/p>\n<p>            piece = img_array[i * piece_size:(i + 1) * piece_size, j * piece_size:(j + 1) * piece_size]<\/p>\n<p>            pieces.append(piece)<\/p>\n<p>    return pieces<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6253\u4e71\u548c\u663e\u793a\u62fc\u56fe<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8fd9\u4e9b\u5c0f\u5757\u6253\u4e71\u987a\u5e8f\u5e76\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u7684random\u6a21\u5757\u8fdb\u884c\u6d17\u724c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def shuffle_and_display_pieces(pieces, num_pieces):<\/p>\n<p>    np.random.shuffle(pieces)<\/p>\n<p>    fig, axes = plt.subplots(num_pieces, num_pieces, figsize=(6, 6))<\/p>\n<p>    for i in range(num_pieces):<\/p>\n<p>        for j in range(num_pieces):<\/p>\n<p>            axes[i, j].imshow(pieces[i * num_pieces + j])<\/p>\n<p>            axes[i, j].axis(&#39;off&#39;)  # \u53bb\u6389\u5750\u6807\u8f74<\/p>\n<p>    plt.subplots_adjust(wspace=0, hspace=0)  # \u53bb\u6389\u5b50\u56fe\u4e4b\u95f4\u7684\u7a7a\u9699<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u73b0\u62fc\u56fe\u7684\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u80fd\u591f\u8ba9\u7528\u6237\u901a\u8fc7\u4ea4\u4e92\u6765\u6062\u590d\u62fc\u56fe\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u4ea4\u6362\u529f\u80fd\u3002\u5f53\u7528\u6237\u70b9\u51fb\u4e24\u4e2a\u5c0f\u5757\u65f6\uff0c\u5b83\u4eec\u4f1a\u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bb0\u5f55\u70b9\u51fb\u4e8b\u4ef6\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def on_click(event, pieces, axes, num_pieces):<\/p>\n<p>    if event.inaxes is None:<\/p>\n<p>        return<\/p>\n<p>    x, y = int(event.xdata), int(event.ydata)<\/p>\n<p>    piece_size = 400 \/\/ num_pieces<\/p>\n<p>    i, j = y \/\/ piece_size, x \/\/ piece_size<\/p>\n<p>    # \u8bb0\u5f55\u70b9\u51fb\u7684\u4e24\u4e2a\u5757\u7684\u4f4d\u7f6e<\/p>\n<p>    if not hasattr(on_click, &#39;selected&#39;):<\/p>\n<p>        on_click.selected = (i, j)<\/p>\n<p>    else:<\/p>\n<p>        i0, j0 = on_click.selected<\/p>\n<p>        pieces[i * num_pieces + j], pieces[i0 * num_pieces + j0] = pieces[i0 * num_pieces + j0], pieces[i * num_pieces + j]<\/p>\n<p>        axes[i, j].imshow(pieces[i * num_pieces + j])<\/p>\n<p>        axes[i0, j0].imshow(pieces[i0 * num_pieces + j0])<\/p>\n<p>        plt.draw()<\/p>\n<p>        del on_click.selected<\/p>\n<h2><strong>\u521d\u59cb\u5316\u62fc\u56fe\u548c\u663e\u793a<\/strong><\/h2>\n<p>pieces = load_and_cut_image(&#39;your_image_path.jpg&#39;, 4)<\/p>\n<p>fig, axes = plt.subplots(4, 4, figsize=(6, 6))<\/p>\n<p>shuffle_and_display_pieces(pieces, 4)<\/p>\n<h2><strong>\u8fde\u63a5\u4e8b\u4ef6<\/strong><\/h2>\n<p>cid = fig.canvas.mpl_connect(&#39;button_press_event&#39;, lambda event: on_click(event, pieces, axes, 4))<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5df2\u7ecf\u80fd\u591f\u4f7f\u7528Python\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u62fc\u56fe\u6e38\u620f\u3002\u8981\u63d0\u9ad8\u6e38\u620f\u7684\u590d\u6742\u5ea6\uff0c\u53ef\u4ee5\u589e\u52a0\u5207\u5272\u7684\u5757\u6570\uff0c\u6216\u662f\u5f15\u5165\u8ba1\u65f6\u5668\u548c\u6b65\u6570\u7edf\u8ba1\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u7b97\u6cd5\u6765\u5b9e\u73b0\u81ea\u52a8\u62fc\u56fe\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f18\u5316\u65b9\u9762\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u5e93\u5982pygame\u6765\u5b9e\u73b0\u66f4\u4e3a\u590d\u6742\u7684\u4ea4\u4e92\u548c\u52a8\u753b\u6548\u679c\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u53d1\u51fa\u66f4\u4e3a\u590d\u6742\u548c\u6709\u8da3\u7684\u62fc\u56fe\u6e38\u620f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u6765\u7ed8\u5236\u62fc\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u62fc\u56fe\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecMatplotlib\u3001Pygame\u548cPillow\u3002Matplotlib\u9002\u5408\u7ed8\u5236\u56fe\u5f62\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0cPygame\u5219\u66f4\u9002\u5408\u5236\u4f5c\u4ea4\u4e92\u5f0f\u6e38\u620f\u548c\u52a8\u753b\uff0c\u800cPillow\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u50cf\u7684\u521b\u5efa\u548c\u7f16\u8f91\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u662f\u5426\u9700\u8981\u4ea4\u4e92\u6027\u6216\u662f\u5426\u8981\u5904\u7406\u590d\u6742\u7684\u56fe\u5f62\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9bPython\u6280\u80fd\u624d\u80fd\u7ed8\u5236\u62fc\u56fe\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u62fc\u56fe\uff0c\u4f60\u9700\u8981\u638c\u63e1\u57fa\u672c\u7684Python\u7f16\u7a0b\u77e5\u8bc6\uff0c\u5305\u62ec\u53d8\u91cf\u3001\u5faa\u73af\u548c\u51fd\u6570\u7684\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982Matplotlib\u6216Pygame\uff09\u4f1a\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u6548\u7387\u3002\u5bf9\u56fe\u5f62\u5b66\u548c\u5750\u6807\u7cfb\u7edf\u7684\u57fa\u672c\u7406\u89e3\u4e5f\u4f1a\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u63a7\u5236\u62fc\u56fe\u7684\u5916\u89c2\u548c\u5e03\u5c40\u3002<\/p>\n<p><strong>\u7ed8\u5236\u62fc\u56fe\u65f6\u5982\u4f55\u5904\u7406\u56fe\u50cf\u7684\u5c3a\u5bf8\u548c\u6bd4\u4f8b\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u62fc\u56fe\u65f6\uff0c\u56fe\u50cf\u7684\u5c3a\u5bf8\u548c\u6bd4\u4f8b\u975e\u5e38\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u4e2d\u7684resize\u529f\u80fd\u6765\u8c03\u6574\u56fe\u50cf\u7684\u5927\u5c0f\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u9002\u5408\u62fc\u56fe\u7684\u8bbe\u8ba1\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u4e2d\u7684subplot\u529f\u80fd\u6765\u63a7\u5236\u6bcf\u4e2a\u62fc\u56fe\u5757\u7684\u4f4d\u7f6e\u548c\u6bd4\u4f8b\uff0c\u4ece\u800c\u5b9e\u73b0\u826f\u597d\u7684\u89c6\u89c9\u6548\u679c\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u4e00\u81f4\u7684\u6bd4\u4f8b\uff0c\u4ee5\u907f\u514d\u56fe\u50cf\u5931\u771f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7ed8\u5236\u62fc\u56fe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528matplotlib\u5e93\u3001PIL\u5e93\u6216pygame\u5e93\u7b49\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":994315,"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\/994303"}],"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=994303"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994303\/revisions"}],"predecessor-version":[{"id":994316,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994303\/revisions\/994316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994315"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}