{"id":1065106,"date":"2024-12-31T16:14:46","date_gmt":"2024-12-31T08:14:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1065106.html"},"modified":"2024-12-31T16:14:48","modified_gmt":"2024-12-31T08:14:48","slug":"python%e5%a6%82%e4%bd%95%e8%bf%90%e7%94%a8%e5%88%b0%e5%b9%b3%e9%9d%a2%e8%ae%be%e8%ae%a1%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1065106.html","title":{"rendered":"python\u5982\u4f55\u8fd0\u7528\u5230\u5e73\u9762\u8bbe\u8ba1\u4e0a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/450a0d8c-f5bf-4702-8d68-b274104f52e8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8fd0\u7528\u5230\u5e73\u9762\u8bbe\u8ba1\u4e0a\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fd0\u7528\u5230\u5e73\u9762\u8bbe\u8ba1\u4e0a\uff0c\u4f8b\u5982\uff1a\u81ea\u52a8\u5316\u8bbe\u8ba1\u4efb\u52a1\u3001\u56fe\u50cf\u5904\u7406\u3001\u751f\u6210\u827a\u672f\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u5f00\u53d1\u8bbe\u8ba1\u5de5\u5177\u7b49\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u81ea\u52a8\u5316\u8bbe\u8ba1\u4efb\u52a1<\/strong> \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5e94\u7528\u9886\u57df\u3002\u5728\u5e73\u9762\u8bbe\u8ba1\u4e2d\uff0c\u91cd\u590d\u6027\u4efb\u52a1\u662f\u4e0d\u53ef\u907f\u514d\u7684\uff0c\u4f8b\u5982\u8c03\u6574\u56fe\u7247\u5927\u5c0f\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6\u3001\u751f\u6210\u56fe\u5f62\u5143\u7d20\u7b49\u3002\u901a\u8fc7Python\u7f16\u7a0b\uff0c\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u6765\u81ea\u52a8\u6267\u884c\u8fd9\u4e9b\u4efb\u52a1\uff0c\u4ece\u800c\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u7cbe\u529b\u3002\u6b64\u5916\uff0cPython\u8fd8\u53ef\u4ee5\u4e0e\u5404\u79cd\u56fe\u5f62\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u5982Pillow\u3001OpenCV\u3001Matplotlib\u7b49\uff0c\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u81ea\u52a8\u5316\u8bbe\u8ba1\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u5728\u5e73\u9762\u8bbe\u8ba1\u4e2d\uff0c\u81ea\u52a8\u5316\u8bbe\u8ba1\u4efb\u52a1\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e93\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u81ea\u52a8\u5b8c\u6210\u4e00\u4e9b\u7e41\u7410\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8c03\u6574\u56fe\u7247\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u8c03\u6574\u56fe\u7247\u5927\u5c0f\u662f\u5e73\u9762\u8bbe\u8ba1\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u3002\u4f7f\u7528Python\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7f16\u5199\u811a\u672c\u6765\u6279\u91cf\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528Pillow\u5e93\u6765\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import os<\/p>\n<p>def resize_images(input_folder, output_folder, size):<\/p>\n<p>    if not os.path.exists(output_folder):<\/p>\n<p>        os.makedirs(output_folder)<\/p>\n<p>    for filename in os.listdir(input_folder):<\/p>\n<p>        if filename.endswith(&quot;.jpg&quot;) or filename.endswith(&quot;.png&quot;):<\/p>\n<p>            img = Image.open(os.path.join(input_folder, filename))<\/p>\n<p>            img = img.resize(size, Image.ANTIALIAS)<\/p>\n<p>            img.save(os.path.join(output_folder, filename))<\/p>\n<p>resize_images(&quot;input_images&quot;, &quot;output_images&quot;, (800, 600))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u5e73\u9762\u8bbe\u8ba1\u9879\u76ee\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u5927\u91cf\u6587\u4ef6\u8fdb\u884c\u76f8\u540c\u7684\u5904\u7406\uff0c\u4f8b\u5982\u8f6c\u6362\u683c\u5f0f\u3001\u6dfb\u52a0\u6c34\u5370\u7b49\u3002Python\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u811a\u672c\u6765\u6279\u91cf\u5904\u7406\u8fd9\u4e9b\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528Pillow\u5e93\u4e3a\u56fe\u7247\u6dfb\u52a0\u6c34\u5370\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont<\/p>\n<p>import os<\/p>\n<p>def add_watermark(input_folder, output_folder, watermark_text):<\/p>\n<p>    if not os.path.exists(output_folder):<\/p>\n<p>        os.makedirs(output_folder)<\/p>\n<p>    for filename in os.listdir(input_folder):<\/p>\n<p>        if filename.endswith(&quot;.jpg&quot;) or filename.endswith(&quot;.png&quot;):<\/p>\n<p>            img = Image.open(os.path.join(input_folder, filename))<\/p>\n<p>            watermark = Image.new(&quot;RGBA&quot;, img.size)<\/p>\n<p>            draw = ImageDraw.Draw(watermark)<\/p>\n<p>            font = ImageFont.truetype(&quot;arial.ttf&quot;, 36)<\/p>\n<p>            text_width, text_height = draw.textsize(watermark_text, font)<\/p>\n<p>            position = (img.width - text_width - 10, img.height - text_height - 10)<\/p>\n<p>            draw.text(position, watermark_text, (255, 255, 255, 128), font=font)<\/p>\n<p>            watermarked_image = Image.alpha_composite(img.convert(&quot;RGBA&quot;), watermark)<\/p>\n<p>            watermarked_image.save(os.path.join(output_folder, filename))<\/p>\n<p>add_watermark(&quot;input_images&quot;, &quot;output_images&quot;, &quot;Sample Watermark&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u5904\u7406\u662f\u5e73\u9762\u8bbe\u8ba1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u5904\u7406\u548c\u7f16\u8f91\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684Pillow\u5e93\uff0c\u53ef\u4ee5\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u8c03\u6574\u4eae\u5ea6\u548c\u5bf9\u6bd4\u5ea6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageEnhance<\/p>\n<h2><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/h2>\n<p>img = Image.open(&quot;example.jpg&quot;)<\/p>\n<p>cropped_img = img.crop((100, 100, 400, 400))<\/p>\n<p>cropped_img.save(&quot;cropped_example.jpg&quot;)<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_img = img.rotate(45)<\/p>\n<p>rotated_img.save(&quot;rotated_example.jpg&quot;)<\/p>\n<h2><strong>\u8c03\u6574\u4eae\u5ea6<\/strong><\/h2>\n<p>enhancer = ImageEnhance.Brightness(img)<\/p>\n<p>bright_img = enhancer.enhance(1.5)<\/p>\n<p>bright_img.save(&quot;bright_example.jpg&quot;)<\/p>\n<h2><strong>\u8c03\u6574\u5bf9\u6bd4\u5ea6<\/strong><\/h2>\n<p>enhancer = ImageEnhance.Contrast(img)<\/p>\n<p>contrast_img = enhancer.enhance(1.5)<\/p>\n<p>contrast_img.save(&quot;contrast_example.jpg&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9ad8\u7ea7\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0cPython\u8fd8\u53ef\u4ee5\u901a\u8fc7OpenCV\u5e93\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\uff0c\u5982\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u8fc7\u6ee4\u3001\u989c\u8272\u8f6c\u6362\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>img = cv2.imread(&quot;example.jpg&quot;, cv2.IMREAD_GRAYSCALE)<\/p>\n<p>edges = cv2.Canny(img, 100, 200)<\/p>\n<p>cv2.imwrite(&quot;edges_example.jpg&quot;, edges)<\/p>\n<h2><strong>\u56fe\u50cf\u8fc7\u6ee4<\/strong><\/h2>\n<p>blurred_img = cv2.GaussianBlur(img, (15, 15), 0)<\/p>\n<p>cv2.imwrite(&quot;blurred_example.jpg&quot;, blurred_img)<\/p>\n<h2><strong>\u989c\u8272\u8f6c\u6362<\/strong><\/h2>\n<p>color_img = cv2.imread(&quot;example.jpg&quot;)<\/p>\n<p>gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)<\/p>\n<p>cv2.imwrite(&quot;gray_example.jpg&quot;, gray_img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u827a\u672f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u827a\u672f\u662f\u4e00\u79cd\u5229\u7528\u7b97\u6cd5\u548c\u7f16\u7a0b\u751f\u6210\u89c6\u89c9\u827a\u672f\u4f5c\u54c1\u7684\u65b9\u5f0f\u3002Python\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u751f\u6210\u827a\u672f\u4f5c\u54c1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Turtle\u5e93\u751f\u6210\u56fe\u6848<\/h4>\n<\/p>\n<p><p>Turtle\u5e93\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u751f\u6210\u7b80\u5355\u7684\u56fe\u6848\u548c\u827a\u672f\u4f5c\u54c1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Turtle\u5e93\u751f\u6210\u5bf9\u79f0\u56fe\u6848\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p>def draw_pattern():<\/p>\n<p>    window = turtle.Screen()<\/p>\n<p>    window.bgcolor(&quot;white&quot;)<\/p>\n<p>    pen = turtle.Turtle()<\/p>\n<p>    pen.speed(0)<\/p>\n<p>    pen.color(&quot;black&quot;)<\/p>\n<p>    for i in range(72):<\/p>\n<p>        pen.forward(100)<\/p>\n<p>        pen.right(45)<\/p>\n<p>        pen.forward(50)<\/p>\n<p>        pen.right(135)<\/p>\n<p>        pen.forward(50)<\/p>\n<p>        pen.right(45)<\/p>\n<p>        pen.forward(100)<\/p>\n<p>        pen.right(175)<\/p>\n<p>    window.exitonclick()<\/p>\n<p>draw_pattern()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Processing.py\u751f\u6210\u590d\u6742\u827a\u672f\u4f5c\u54c1<\/h4>\n<\/p>\n<p><p>Processing.py\u662fProcessing\u7f16\u7a0b\u8bed\u8a00\u7684Python\u7248\uff0c\u9002\u5408\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684\u751f\u6210\u827a\u672f\u4f5c\u54c1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Processing.py\u751f\u6210\u62bd\u8c61\u827a\u672f\u4f5c\u54c1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def setup():<\/p>\n<p>    size(800, 800)<\/p>\n<p>    noLoop()<\/p>\n<p>def draw():<\/p>\n<p>    background(255)<\/p>\n<p>    for i in range(100):<\/p>\n<p>        x = random(width)<\/p>\n<p>        y = random(height)<\/p>\n<p>        r = random(50, 150)<\/p>\n<p>        fill(random(255), random(255), random(255), 150)<\/p>\n<p>        ellipse(x, y, r, r)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u89c6\u5316\u662f\u5c06\u6570\u636e\u8f6c\u5316\u4e3a\u56fe\u5f62\u6216\u56fe\u8868\u7684\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u66f4\u5bb9\u6613\u7406\u89e3\u548c\u5206\u6790\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Matplotlib\u521b\u5efa\u56fe\u8868<\/h4>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\uff0c\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Line Chart&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>x = [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;]<\/p>\n<p>y = [10, 20, 30, 40]<\/p>\n<p>plt.bar(x, y)<\/p>\n<p>plt.title(&quot;Bar Chart&quot;)<\/p>\n<p>plt.xlabel(&quot;Categories&quot;)<\/p>\n<p>plt.ylabel(&quot;Values&quot;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [5, 7, 8, 5, 6]<\/p>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.title(&quot;Scatter Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Seaborn\u521b\u5efa\u9ad8\u7ea7\u56fe\u8868<\/h4>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u56fe\u8868\u7c7b\u578b\u548c\u66f4\u7b80\u6d01\u7684API\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = pd.DataFrame({<\/p>\n<p>    &quot;Category&quot;: [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;],<\/p>\n<p>    &quot;Value&quot;: [10, 20, 15, 25, 30]<\/p>\n<p>})<\/p>\n<h2><strong>\u6761\u5f62\u56fe<\/strong><\/h2>\n<p>sns.barplot(x=&quot;Category&quot;, y=&quot;Value&quot;, data=data)<\/p>\n<p>plt.title(&quot;Bar Plot&quot;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>data = sns.load_dataset(&quot;tips&quot;)<\/p>\n<p>sns.boxplot(x=&quot;day&quot;, y=&quot;total_bill&quot;, data=data)<\/p>\n<p>plt.title(&quot;Box Plot&quot;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>data = sns.load_dataset(&quot;flights&quot;)<\/p>\n<p>data = data.pivot(&quot;month&quot;, &quot;year&quot;, &quot;passengers&quot;)<\/p>\n<p>sns.heatmap(data, annot=True, fmt=&quot;d&quot;)<\/p>\n<p>plt.title(&quot;Heatmap&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5f00\u53d1\u8bbe\u8ba1\u5de5\u5177<\/h3>\n<\/p>\n<p><p>Python\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u751f\u6210\u827a\u672f\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1\u8bbe\u8ba1\u5de5\u5177\uff0c\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u56fe\u50cf\u7f16\u8f91\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u548c\u56fe\u5f62\u754c\u9762\u5e93\uff08\u5982Tkinter\u3001PyQt\u7b49\uff09\uff0c\u53ef\u4ee5\u5f00\u53d1\u7b80\u5355\u7684\u56fe\u50cf\u7f16\u8f91\u5de5\u5177\uff0c\u63d0\u4f9b\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u8c03\u6574\u989c\u8272\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Tkinter\u521b\u5efa\u7b80\u5355\u56fe\u50cf\u7f16\u8f91\u5de5\u5177\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import filedialog<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<p>def open_image():<\/p>\n<p>    file_path = filedialog.askopenfilename()<\/p>\n<p>    if file_path:<\/p>\n<p>        img = Image.open(file_path)<\/p>\n<p>        img.thumbn<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l((400, 400))<\/p>\n<p>        img_tk = ImageTk.PhotoImage(img)<\/p>\n<p>        label.config(image=img_tk)<\/p>\n<p>        label.image = img_tk<\/p>\n<p>def rotate_image():<\/p>\n<p>    if label.image:<\/p>\n<p>        img = label.image._PhotoImage__photo.rotate(45)<\/p>\n<p>        img_tk = ImageTk.PhotoImage(img)<\/p>\n<p>        label.config(image=img_tk)<\/p>\n<p>        label.image = img_tk<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Image Editor&quot;)<\/p>\n<p>open_button = tk.Button(root, text=&quot;Open Image&quot;, command=open_image)<\/p>\n<p>open_button.pack()<\/p>\n<p>rotate_button = tk.Button(root, text=&quot;Rotate Image&quot;, command=rotate_image)<\/p>\n<p>rotate_button.pack()<\/p>\n<p>label = tk.Label(root)<\/p>\n<p>label.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5f00\u53d1\u751f\u6210\u827a\u672f\u5de5\u5177<\/h4>\n<\/p>\n<p><p>Python\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5f00\u53d1\u751f\u6210\u827a\u672f\u5de5\u5177\uff0c\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u66f4\u65b9\u4fbf\u5730\u521b\u5efa\u751f\u6210\u827a\u672f\u4f5c\u54c1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Processing.py\u521b\u5efa\u751f\u6210\u827a\u672f\u5de5\u5177\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def setup():<\/p>\n<p>    size(800, 800)<\/p>\n<p>    noLoop()<\/p>\n<p>def draw():<\/p>\n<p>    background(255)<\/p>\n<p>    for i in range(100):<\/p>\n<p>        x = random(width)<\/p>\n<p>        y = random(height)<\/p>\n<p>        r = random(50, 150)<\/p>\n<p>        fill(random(255), random(255), random(255), 150)<\/p>\n<p>        ellipse(x, y, r, r)<\/p>\n<p>def mousePressed():<\/p>\n<p>    saveFrame(&quot;artwork-####.png&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230Python\u5728\u5e73\u9762\u8bbe\u8ba1\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u65e0\u8bba\u662f\u81ea\u52a8\u5316\u8bbe\u8ba1\u4efb\u52a1\u3001\u56fe\u50cf\u5904\u7406\u3001\u751f\u6210\u827a\u672f\u3001\u6570\u636e\u53ef\u89c6\u5316\uff0c\u8fd8\u662f\u5f00\u53d1\u8bbe\u8ba1\u5de5\u5177\uff0cPython\u90fd\u53ef\u4ee5\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\uff0c\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u521b\u9020\u51fa\u66f4\u52a0\u7cbe\u5f69\u7684\u4f5c\u54c1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u5e73\u9762\u8bbe\u8ba1\u4e2d\uff0cPython\u53ef\u4ee5\u7528\u4e8e\u54ea\u4e9b\u5177\u4f53\u7684\u5e94\u7528\uff1f<\/strong><br \/>Python\u5728\u5e73\u9762\u8bbe\u8ba1\u4e2d\u6709\u591a\u79cd\u5e94\u7528\uff0c\u4f8b\u5982\u81ea\u52a8\u5316\u8bbe\u8ba1\u6d41\u7a0b\u3001\u751f\u6210\u56fe\u5f62\u548c\u56fe\u50cf\u5904\u7406\u3002\u901a\u8fc7\u4f7f\u7528\u5e93\u5982PIL\uff08Python Imaging Library\uff09\u548cOpenCV\uff0c\u8bbe\u8ba1\u5e08\u53ef\u4ee5\u521b\u5efa\u548c\u7f16\u8f91\u56fe\u50cf\uff0c\u8fdb\u884c\u6279\u91cf\u5904\u7406\u3002\u6b64\u5916\uff0cPython\u8fd8\u53ef\u4ee5\u4e0e\u8bbe\u8ba1\u8f6f\u4ef6\uff08\u5982Adobe Photoshop\uff09\u8fdb\u884c\u96c6\u6210\uff0c\u5e2e\u52a9\u7528\u6237\u81ea\u52a8\u5316\u91cd\u590d\u6027\u4efb\u52a1\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p><strong>\u4f5c\u4e3a\u5e73\u9762\u8bbe\u8ba1\u5e08\uff0c\u6211\u5e94\u8be5\u5b66\u4e60\u54ea\u4e9bPython\u5e93\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5e73\u9762\u8bbe\u8ba1\u5e08\u6765\u8bf4\uff0c\u5b66\u4e60\u4e00\u4e9b\u7279\u5b9a\u7684Python\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u3002\u63a8\u8350\u7684\u5e93\u5305\u62ecPillow\uff08\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff09\u3001Matplotlib\uff08\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u548c\u56fe\u5f62\u751f\u6210\uff09\u3001Turtle\uff08\u7528\u4e8e\u7b80\u5355\u7ed8\u56fe\uff09\u4ee5\u53caSVGwrite\uff08\u7528\u4e8e\u751f\u6210SVG\u6587\u4ef6\uff09\u3002\u8fd9\u4e9b\u5e93\u80fd\u591f\u5e2e\u52a9\u8bbe\u8ba1\u5e08\u5b9e\u73b0\u5404\u79cd\u8bbe\u8ba1\u6548\u679c\u548c\u56fe\u5f62\u521b\u4f5c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5229\u7528Python\u8fdb\u884c\u56fe\u50cf\u6279\u5904\u7406\uff1f<\/strong><br \/>\u5229\u7528Python\u8fdb\u884c\u56fe\u50cf\u6279\u5904\u7406\u975e\u5e38\u7b80\u5355\u3002\u8bbe\u8ba1\u5e08\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u7f16\u5199\u811a\u672c\uff0c\u81ea\u52a8\u5316\u5904\u7406\u5927\u91cf\u56fe\u50cf\u6587\u4ef6\uff0c\u4f8b\u5982\u8c03\u6574\u5927\u5c0f\u3001\u8f6c\u6362\u683c\u5f0f\u6216\u6dfb\u52a0\u6c34\u5370\u3002\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5faa\u73af\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u5904\u7406\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\uff0c\u4ece\u800c\u8282\u7701\u5927\u91cf\u65f6\u95f4\u548c\u7cbe\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fd0\u7528\u5230\u5e73\u9762\u8bbe\u8ba1\u4e0a\uff0c\u4f8b\u5982\uff1a\u81ea\u52a8\u5316\u8bbe\u8ba1\u4efb\u52a1\u3001\u56fe\u50cf\u5904\u7406\u3001\u751f\u6210\u827a\u672f\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u5f00\u53d1\u8bbe\u8ba1\u5de5 [&hellip;]","protected":false},"author":3,"featured_media":1065109,"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\/1065106"}],"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=1065106"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065106\/revisions"}],"predecessor-version":[{"id":1065111,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065106\/revisions\/1065111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065109"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1065106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1065106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1065106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}