{"id":24088,"date":"2024-11-21T09:40:06","date_gmt":"2024-11-21T01:40:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24088\/"},"modified":"2024-11-21T09:40:06","modified_gmt":"2024-11-21T01:40:06","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8webman%e6%a1%86%e6%9e%b6%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e5%a4%84%e7%90%86%e5%92%8c%e8%a3%81%e5%89%aa%e5%8a%9f%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24088\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Webman\u6846\u67b6\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\uff1f"},"content":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528webman\u6846\u67b6\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\uff1f<\/p>\n<p>Webman\u662f\u4e00\u6b3e\u57fa\u4e8ePython\u7684\u8f7b\u91cf\u7ea7Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u5de5\u5177\u548c\u529f\u80fd\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efaWeb\u5e94\u7528\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6846\u67b6\u6765\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5b89\u88c5Webman\u6846\u67b6\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<pre>pip install webman<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\uff1a<\/p>\n<pre>from webman import App, Response\nfrom PIL import Image<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aWebman\u5e94\u7528\u5bf9\u8c61\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5904\u7406\u56fe\u7247\u7684\u8def\u7531\uff1a<\/p>\n<pre>app = App()\n\n@app.route('\/', methods=['POST'])\ndef handle_image(request):\n    # \u83b7\u53d6\u4e0a\u4f20\u7684\u56fe\u7247\n    image = request.files.get('image')\n    \n    if image and allowed_file(image.filename):\n        # \u8bfb\u53d6\u56fe\u7247\u5185\u5bb9\n        with Image.open(image) as img:\n            # \u5904\u7406\u56fe\u7247\n            img = process_image(img)\n            \n            # \u88c1\u526a\u56fe\u7247\n            cropped_img = crop_image(img)\n            \n            # \u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u7247\n            cropped_img.save('cropped_img.jpg')\n\n        return Response('Image processed and cropped successfully')\n    \n    return Response('Invalid or unsupported image format')<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5904\u7406\u56fe\u7247\u7684\u8def\u7531 \/\uff0c\u5e76\u8bbe\u7f6e\u53ea\u63a5\u53d7POST\u65b9\u6cd5\u7684\u8bf7\u6c42\u3002\u5728\u8bf7\u6c42\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u4e0a\u4f20\u7684\u56fe\u7247\u3002\u7136\u540e\uff0c\u4f7f\u7528PIL\u5e93\u6253\u5f00\u56fe\u7247\uff0c\u5e76\u8c03\u7528process_image\u51fd\u6570\u6765\u5904\u7406\u56fe\u7247\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528crop_image\u51fd\u6570\u6765\u88c1\u526a\u56fe\u7247\u3002\u6700\u540e\uff0c\u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u7247\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a\u8f85\u52a9\u51fd\u6570allowed_file\u3001process_image\u548ccrop_image\uff1a<\/p>\n<pre>def allowed_file(filename):\n    ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}\n    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS\n\ndef process_image(img):\n    # \u56fe\u7247\u5904\u7406\u903b\u8f91...\n    return img\n\ndef crop_image(img):\n    # \u56fe\u7247\u88c1\u526a\u903b\u8f91...\n    return img<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728allowed_file\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u652f\u6301\u7684\u56fe\u7247\u683c\u5f0f\u3002\u53ea\u6709\u4e0a\u4f20\u7684\u56fe\u7247\u683c\u5f0f\u662f\u5728\u5141\u8bb8\u7684\u683c\u5f0f\u5217\u8868\u4e2d\uff0c\u624d\u4f1a\u8fd4\u56deTrue\u3002<\/p>\n<p>process_image\u548ccrop_image\u51fd\u6570\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u5177\u4f53\u7684\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u903b\u8f91\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u542f\u52a8Webman\u5e94\u7528\uff1a<\/p>\n<pre>if __name__ == '__main__':\n    app.run()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u672c\u5730\u542f\u52a8\u4e00\u4e2aWeb\u670d\u52a1\u5668\uff0c\u5e76\u76d1\u542c\u9ed8\u8ba4\u7684\u7aef\u53e3\uff08\u4f8b\u5982\uff1ahttp:\/\/127.0.0.1:5000\/\uff09\u3002\u5f53\u6211\u4eec\u901a\u8fc7POST\u8bf7\u6c42\u4e0a\u4f20\u4e00\u5f20\u56fe\u7247\u65f6\uff0c\u670d\u52a1\u5668\u5c06\u6839\u636e\u6211\u4eec\u5b9a\u4e49\u7684\u903b\u8f91\u6765\u5904\u7406\u5e76\u88c1\u526a\u56fe\u7247\u3002<\/p>\n<p>\u603b\u7ed3\u8d77\u6765\uff0c\u4f7f\u7528Webman\u6846\u67b6\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\u5904\u7406\u51fd\u6570\uff0c\u83b7\u53d6\u4e0a\u4f20\u7684\u56fe\u7247\uff0c\u5e76\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u3002\u501f\u52a9Webman\u6846\u67b6\u63d0\u4f9b\u7684\u5de5\u5177\u548c\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u529f\u80fd\u5f3a\u5927\u7684Web\u5e94\u7528\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528Webman\u6846\u67b6\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Webman\u6846\u67b6\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528webman\u6846\u67b6\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\uff1f Webman\u662f\u4e00\u6b3e\u57fa\u4e8ePython\u7684\u8f7b\u91cf\u7ea7Web\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u5de5\u5177\u548c\u529f\u80fd\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efaWeb\u5e94\u7528\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Webman\u6846\u67b6\u6765\u5b9e\u73b0\u56fe\u7247\u5904\u7406\u548c\u88c1\u526a\u529f\u80fd\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5b89\u88c5Webman\u6846\u67b6\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a pip install webman \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\uff1a from webman import App, Response from PIL import Image \u767b\u5f55\u540e\u590d\u5236 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2aWebman\u5e94\u7528\u5bf9\u8c61\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5904\u7406\u56fe\u7247\u7684\u8def\u7531\uff1a app = App() @app.route(&#8216;\/&#8217;, methods=[&#8216;POST&#8217;]) def handle_image(request): # \u83b7\u53d6\u4e0a\u4f20\u7684\u56fe\u7247 image = request.files.get(&#8216;image&#8217;) if image and allowed_file(image.filename): # \u8bfb\u53d6\u56fe\u7247\u5185\u5bb9 with Image.open(image) as img: # \u5904\u7406\u56fe\u7247 img = process_image(img) # \u88c1\u526a\u56fe\u7247 cropped_img = crop_image(img) # \u4fdd\u5b58\u88c1\u526a\u540e\u7684\u56fe\u7247 cropped_img.save(&#8216;cropped_img.jpg&#8217;) return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24088","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24088"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24088\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}