{"id":950817,"date":"2024-12-27T00:45:13","date_gmt":"2024-12-26T16:45:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/950817.html"},"modified":"2024-12-27T00:45:17","modified_gmt":"2024-12-26T16:45:17","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0pil","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/950817.html","title":{"rendered":"Python\u5982\u4f55\u6dfb\u52a0pIL"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085247\/ee829ee6-c576-4519-88ff-5d45eb40c834.webp\" alt=\"Python\u5982\u4f55\u6dfb\u52a0pIL\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6dfb\u52a0Pillow\uff08pIL\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u3001\u786e\u4fdd\u5b89\u88c5\u6b63\u786e\u7684Python\u7248\u672c\u3001\u521b\u5efa\u865a\u62df\u73af\u5883\u3001\u9a8c\u8bc1\u5b89\u88c5\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528Pillow\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08pIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\u548c\u6539\u8fdb\u7248\u672c\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u652f\u6301\u548c\u529f\u80fd\u3002\u4e3a\u4e86\u5728Python\u4e2d\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0cPillow\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5\u548c\u4f7f\u7528Pillow\u7684\u8be6\u7ec6\u6307\u5357\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Pillow<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528pip\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>Pillow\u53ef\u4ee5\u901a\u8fc7pip\u8f7b\u677e\u5b89\u88c5\uff0c\u8fd9\u662fPython\u5305\u7ba1\u7406\u5668\uff0c\u901a\u5e38\u968fPython\u4e00\u8d77\u5b89\u88c5\u3002\u8981\u5b89\u88c5Pillow\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\uff08Linux\u548cmacOS\uff09\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff08Windows\uff09\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5Pillow\u5e93\u3002<\/p>\n<\/p>\n<p><h4>2. \u786e\u4fddPython\u7248\u672c\u517c\u5bb9<\/h4>\n<\/p>\n<p><p>Pillow\u652f\u6301Python 3.6\u53ca\u4ee5\u4e0a\u7684\u7248\u672c\u3002\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u7b26\u5408\u8981\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684Python\u7248\u672c\u8f83\u4f4e\uff0c\u53ef\u80fd\u9700\u8981\u5347\u7ea7Python\u6216\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>3. \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u5904\u7406\u591a\u4e2a\u9879\u76ee\u65f6\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u7ba1\u7406\u4e0d\u540c\u7684\u4f9d\u8d56\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff08\u5728Windows\u4e0a\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Unix\u6216macOS\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5Pillow\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u9a8c\u8bc1\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u5bfc\u5165Pillow\u5e93\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5370Pillow\u7248\u672c<\/strong><\/h2>\n<p>print(Image.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u9519\u8bef\u6d88\u606f\u5e76\u4e14\u6b63\u786e\u6253\u5370\u51fa\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pillow\u8fdb\u884c\u57fa\u672c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>Pillow\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u548c\u663e\u793a\u56fe\u50cf\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u683c\u5f0f\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>Pillow\u5141\u8bb8\u4f60\u5c06\u56fe\u50cf\u4ece\u4e00\u79cd\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06JPEG\u683c\u5f0f\u7684\u56fe\u50cf\u8f6c\u6362\u4e3aPNG\u683c\u5f0f<\/p>\n<p>img.save(&#39;example.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/h4>\n<\/p>\n<p><p>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u5728\u5904\u7406\u56fe\u50cf\u6570\u636e\u65f6\u975e\u5e38\u5e38\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/p>\n<p>img_resized = img.resize((800, 600))<\/p>\n<h2><strong>\u4fdd\u5b58\u8c03\u6574\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img_resized.save(&#39;example_resized.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u526a\u5207\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pillow\u526a\u5207\u56fe\u50cf\uff0c\u8fd9\u5bf9\u4e8e\u63d0\u53d6\u56fe\u50cf\u7684\u7279\u5b9a\u90e8\u5206\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u526a\u5207\u56fe\u50cf\uff08\u5b9a\u4e49\u8981\u526a\u5207\u7684\u533a\u57df\uff09<\/p>\n<p>box = (100, 100, 400, 400)<\/p>\n<p>img_cropped = img.crop(box)<\/p>\n<h2><strong>\u4fdd\u5b58\u526a\u5207\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img_cropped.save(&#39;example_cropped.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u56fe\u50cf\u65cb\u8f6c<\/h4>\n<\/p>\n<p><p>\u65cb\u8f6c\u56fe\u50cf\u4e5f\u662fPillow\u7684\u57fa\u672c\u529f\u80fd\u4e4b\u4e00\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u50cf90\u5ea6<\/p>\n<p>img_rotated = img.rotate(90)<\/p>\n<h2><strong>\u4fdd\u5b58\u65cb\u8f6c\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img_rotated.save(&#39;example_rotated.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pillow\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p>Pillow\u4e0d\u4ec5\u9650\u4e8e\u57fa\u672c\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u8fd8\u652f\u6301\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5904\u7406\u900f\u660e\u5ea6\u548cAlpha\u901a\u9053<\/h4>\n<\/p>\n<p><p>Pillow\u652f\u6301\u5904\u7406\u5177\u6709\u900f\u660e\u5ea6\u7684\u56fe\u50cf\uff0c\u5141\u8bb8\u4f60\u64cd\u4f5cAlpha\u901a\u9053\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u5177\u6709Alpha\u901a\u9053\u7684\u56fe\u50cf<\/p>\n<p>img_with_alpha = Image.open(&#39;example_with_alpha.png&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u56fe\u50cf\u6a21\u5f0f<\/strong><\/h2>\n<p>print(img_with_alpha.mode)  # \u8f93\u51fa: RGBA<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u4e0d\u5e26Alpha\u901a\u9053\u7684RGB\u6a21\u5f0f<\/strong><\/h2>\n<p>img_rgb = img_with_alpha.convert(&#39;RGB&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5e94\u7528\u56fe\u50cf\u6ee4\u955c<\/h4>\n<\/p>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u6ee4\u955c\uff0c\u53ef\u4ee5\u5e94\u7528\u4e8e\u56fe\u50cf\u4ee5\u4ea7\u751f\u5404\u79cd\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageFilter<\/p>\n<h2><strong>\u5e94\u7528\u6a21\u7cca\u6ee4\u955c<\/strong><\/h2>\n<p>img_blurred = img.filter(ImageFilter.BLUR)<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u7cca\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img_blurred.save(&#39;example_blurred.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u56fe\u50cf\u5408\u6210<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u591a\u5f20\u56fe\u50cf\u5408\u6210\u5230\u4e00\u8d77\uff0c\u521b\u5efa\u590d\u6742\u7684\u56fe\u50cf\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u4e24\u5f20\u56fe\u50cf<\/p>\n<p>img1 = Image.open(&#39;background.jpg&#39;)<\/p>\n<p>img2 = Image.open(&#39;overlay.png&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u53e0\u52a0\u56fe\u50cf\u7684\u5c3a\u5bf8\u4e0e\u80cc\u666f\u56fe\u50cf\u4e00\u81f4<\/strong><\/h2>\n<p>img2 = img2.resize(img1.size)<\/p>\n<h2><strong>\u5408\u6210\u56fe\u50cf<\/strong><\/h2>\n<p>img_combined = Image.alpha_composite(img1.convert(&#39;RGBA&#39;), img2.convert(&#39;RGBA&#39;))<\/p>\n<h2><strong>\u4fdd\u5b58\u5408\u6210\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>img_combined.save(&#39;example_combined.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5904\u7406EXIF\u6570\u636e<\/h4>\n<\/p>\n<p><p>Pillow\u652f\u6301\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u50cf\u4e2d\u7684EXIF\u6570\u636e\uff0c\u8fd9\u5bf9\u4e8e\u6444\u5f71\u56fe\u50cf\u975e\u5e38\u91cd\u8981\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u56fe\u50cf<\/p>\n<p>img_with_exif = Image.open(&#39;example_with_exif.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6EXIF\u6570\u636e<\/strong><\/h2>\n<p>exif_data = img_with_exif._getexif()<\/p>\n<h2><strong>\u6253\u5370EXIF\u6570\u636e<\/strong><\/h2>\n<p>for tag_id, value in exif_data.items():<\/p>\n<p>    tag = TAGS.get(tag_id, tag_id)<\/p>\n<p>    print(f&quot;{tag}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001Pillow\u7684\u4f18\u5316\u548c\u6027\u80fd\u63d0\u5347<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6279\u91cf\u56fe\u50cf\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\u3002Pillow\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9009\u9879\u6765\u4f18\u5316\u548c\u63d0\u9ad8\u56fe\u50cf\u5904\u7406\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u5e76\u884c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5229\u7528Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u6765\u5e76\u884c\u5904\u7406\u591a\u4e2a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>import os<\/p>\n<p>def process_image(filename):<\/p>\n<p>    img = Image.open(filename)<\/p>\n<p>    img_resized = img.resize((800, 600))<\/p>\n<p>    img_resized.save(f&quot;processed_{os.path.basename(filename)}&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image_files = [f for f in os.listdir(&#39;.&#39;) if f.endswith(&#39;.jpg&#39;)]<\/p>\n<h2><strong>\u4f7f\u7528\u7ebf\u7a0b\u6c60\u5e76\u884c\u5904\u7406\u56fe\u50cf<\/strong><\/h2>\n<p>with ThreadPoolExecutor() as executor:<\/p>\n<p>    executor.map(process_image, image_files)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5185\u5b58\u6620\u5c04<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6765\u63d0\u9ad8\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageFile<\/p>\n<h2><strong>\u542f\u7528\u5185\u5b58\u6620\u5c04<\/strong><\/h2>\n<p>ImageFile.LOAD_TRUNCATED_IMAGES = True<\/p>\n<h2><strong>\u6253\u5f00\u5927\u56fe\u50cf<\/strong><\/h2>\n<p>large_img = Image.open(&#39;large_image.jpg&#39;)<\/p>\n<h2><strong>\u5904\u7406\u56fe\u50cf<\/strong><\/h2>\n<p>large_img_resized = large_img.resize((4000, 3000))<\/p>\n<p>large_img_resized.save(&#39;large_image_resized.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001Pillow\u7684\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5931\u8d25\u6216\u4f9d\u8d56\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Pillow\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u4f9d\u8d56\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728Windows\u7cfb\u7edf\u4e0a\u3002\u786e\u4fdd\u5b89\u88c5\u4e86\u6700\u65b0\u7248\u672c\u7684pip\u548csetuptools\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade pip setuptools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5728Windows\u4e0a\u9047\u5230C++\u7f16\u8bd1\u5668\u9519\u8bef\uff0c\u53ef\u4ee5\u5b89\u88c5Visual C++ Redistributable for Visual Studio\u3002<\/p>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u52a0\u8f7d\u901f\u5ea6\u6162<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u56fe\u50cf\u52a0\u8f7d\u901f\u5ea6\u8f83\u6162\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528<code>Image.load()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4f1a\u5ef6\u8fdf\u56fe\u50cf\u7684\u89e3\u7801\uff0c\u76f4\u5230\u9700\u8981\u65f6\u624d\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">img = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>img.load()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5904\u7406\u591a\u79cd\u56fe\u50cf\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u786e\u4fddPillow\u652f\u6301\u4f60\u9700\u8981\u5904\u7406\u7684\u6240\u6709\u56fe\u50cf\u683c\u5f0f\u3002\u5982\u679c\u9700\u8981\u5904\u7406TIFF\u6216\u5176\u4ed6\u7279\u6b8a\u683c\u5f0f\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u63d2\u4ef6\u6216\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u6210\u529f\u5b89\u88c5\u548c\u4f7f\u7528Pillow\u5e93\u6765\u8fdb\u884c\u5404\u79cd\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u56fe\u50cf\u7f16\u8f91\u8fd8\u662f\u590d\u6742\u7684\u56fe\u50cf\u5408\u6210\uff0cPillow\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5PIL\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528PIL\u5e93\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install Pillow<\/code>\u3002Pillow\u662fPIL\u5e93\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u652f\u6301\u548c\u66f4\u591a\u529f\u80fd\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>import PIL<\/code>\u6765\u5bfc\u5165\u5e93\u3002<\/p>\n<p><strong>PIL\u5e93\u80fd\u505a\u54ea\u4e9b\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff1f<\/strong><br \/>PIL\u5e93\u652f\u6301\u591a\u79cd\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u6253\u5f00\u548c\u4fdd\u5b58\u56fe\u50cf\u3001\u56fe\u50cf\u683c\u5f0f\u8f6c\u6362\u3001\u56fe\u50cf\u526a\u88c1\u3001\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u6ee4\u955c\u6548\u679c\u4ee5\u53ca\u6587\u672c\u548c\u56fe\u5f62\u7684\u7ed8\u5236\u7b49\u3002\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97PIL\u6210\u4e3a\u5904\u7406\u56fe\u50cf\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u9002\u5408\u4e8e\u5404\u79cd\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u56fe\u50cf\u663e\u793a\uff1f<\/strong><br \/>\u4f7f\u7528PIL\u5e93\u663e\u793a\u56fe\u50cf\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>Image<\/code>\u6a21\u5757\u4e2d\u7684<code>open()<\/code>\u65b9\u6cd5\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>show()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u56fe\u50cf\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\nimage = Image.open(&#39;your_image.jpg&#39;)\nimage.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5f00\u5e76\u663e\u793a\u6307\u5b9a\u8def\u5f84\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u786e\u4fdd\u60a8\u7684\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6dfb\u52a0Pillow\uff08pIL\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u3001\u786e\u4fdd\u5b89\u88c5\u6b63\u786e\u7684Python\u7248\u672c\u3001\u521b\u5efa\u865a [&hellip;]","protected":false},"author":3,"featured_media":950823,"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\/950817"}],"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=950817"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950817\/revisions"}],"predecessor-version":[{"id":950824,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950817\/revisions\/950824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/950823"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=950817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=950817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=950817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}