<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Pillow 教程 > 原文: [http://zetcode.com/python/pillow/](http://zetcode.com/python/pillow/) Pillow 教程展示了如何在 Python 中使用 Pillow 來處理圖像。 來源可從作者的 Github [倉庫](https://github.com/janbodnar/pillow-examples)中獲得。 ## Pillow Pillow 是 Python 圖像庫(PIL),它增加了對打開,操作和保存圖像的支持。 當前版本標識并讀取大量格式。 有意將寫支持限制為最常用的交換和表示格式。 ## 顯示圖片 在第一個示例中,我們讀取圖像文件并在外部程序中顯示它。 `show_image.py` ```py #!/usr/bin/python3 from PIL import Image import sys try: tatras = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) tatras.show() ``` 該程序讀取 JPG 圖像并將其顯示在外部應用中。 ```py from PIL import Image ``` 在 PIL 模塊中,我們包含`Image`類。 ```py tatras = Image.open("tatras.jpg") ``` `Image.open()`方法讀取圖像文件。Pillow 可以讀取 30 多種不同的文件格式。 ```py tatras.show() ``` `show()`方法主要用于調試目的。 它將圖像保存到一個臨時文件中并在外部程序中顯示。 在 Linux 上可以是 ImageMagic,在 Windows 上可以是 Paint。 ## Pillow 的基本圖像信息 Pillow 使我們可以獲得有關圖像的一些基本信息。 `basic_info.py` ```py #!/usr/bin/python3 from PIL import Image import sys try: tatras = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) print("Format: {0}\nSize: {1}\nMode: {2}".format(tatras.format, tatras.size, tatras.mode)) ``` 該示例使用 Pillow 打印有關圖像的基本信息。 ```py print("Format: {0}\nSize: {1}\nMode: {2}".format(tatras.format, tatras.size, tatras.mode)) ``` 我們打印圖像格式,大小和模式。 ```py $ ./basic_info.py Format: JPEG Size: (350, 232) Mode: RGB ``` 這是程序的輸出。 ## 圖像模糊 `ImageFilter`模塊包含一組預定義的過濾器的定義,可以與`filter()`方法一起使用。 `blur_image.py` ```py #!/usr/bin/python3 from PIL import Image, ImageFilter import sys try: img = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) blurred = img.filter(ImageFilter.BLUR) blurred.save("blurred.png") ``` 該程序加載圖像,從原始圖像創建模糊圖像,然后將新圖像保存在磁盤上。 ```py from PIL import Image, ImageFilter ``` 我們導入`Image`和`ImageFilter`模塊。 ```py blurred = img.filter(ImageFilter.BLUR) ``` 我們將`ImageFilter.BLUR`應用于原始圖像; 該操作將返回一個新的修改圖像。 ```py blurred.save("blurred.png") ``` 使用`save()`方法,我們將模糊的圖像保存在磁盤上。 ## 用 Pillow 轉換圖像 使用`save()`方法,我們可以將圖像轉換為其他格式。 `convert2png.py` ```py #!/usr/bin/python3 from PIL import Image import sys try: tatras = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) tatras.save('tatras.png', 'png') ``` 該程序讀取 JPG 圖像并將其轉換為 PNG 格式。 ```py tatras.save('tatras.png', 'png') ``` `save()`方法的第二個參數指定圖像格式。 ## 灰度圖像 使用`Image.convert()`方法,我們可以產生黑白圖像。 `grayscale.py` ```py #!/usr/bin/python3 from PIL import Image import sys try: tatras = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) grayscale = tatras.convert('L') grayscale.show() ``` 該程序讀取圖像并將其轉換為灰度圖像。 ```py grayscale = tatras.convert('L') ``` `convert()`方法的第一個參數是`mode`; `"L"`模式是黑白的。 ## 用 Pillow 裁剪圖像 `Image.crop()`裁剪圖像。 `crop_image.py` ```py #!/usr/bin/python3 from PIL import Image import sys try: tatras = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) cropped = tatras.crop((100, 100, 350, 350)) cropped.save('tatras_cropped.jpg') ``` 該程序將裁剪圖像。 裁剪后的圖像保存在磁盤上。 ```py cropped = tatras.crop((100, 100, 350, 350)) ``` `crop()`方法采用 4 個元組來定義左,上,右和下像素坐標。 ## 用 Pillow 旋轉圖像 `Image.rotate()`返回圖像的旋轉副本。 `rotate_image.py` ```py #!/usr/bin/python3 from PIL import Image import sys try: tatras = Image.open("tatras.jpg") except IOError: print("Unable to load image") sys.exit(1) rotated = tatras.rotate(180) rotated.save('tatras_rotated.jpg') ``` 該程序將圖像旋轉 180 度并將新創建的圖像保存在磁盤上。 ## 在 Tkinter 中顯示圖像 以下程序在 Tkinter 程序中顯示圖像。 `show_tkinter.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- from PIL import Image, ImageTk from tkinter import Tk from tkinter.ttk import Frame, Label import sys class Example(Frame): def __init__(self): super().__init__() self.loadImage() self.initUI() def loadImage(self): try: self.img = Image.open("tatrs.jpg") except IOError: print("Unable to load image") sys.exit(1) def initUI(self): self.master.title("Label") tatras = ImageTk.PhotoImage(self.img) label = Label(self, image=tatras) # reference must be stored label.image = tatras label.pack() self.pack() def setGeometry(self): w, h = self.img.size self.master.geometry(("%dx%d+300+300") % (w, h)) def main(): root = Tk() ex = Example() ex.setGeometry() root.mainloop() if __name__ == '__main__': main() ``` 該程序在 Tkinter 工具箱的`Label`小部件中顯示圖像。 ```py from PIL import Image, ImageTk ``` `ImageTk`是 Tkinter 兼容的照片圖像。 它可以在 Tkinter 需要圖像對象的任何地方使用。 ```py tatras = ImageTk.PhotoImage(self.img) ``` 我們創建照片圖像。 ```py label = Label(self, image=tatras) ``` 將照片圖像提供給標簽窗口小部件的`image`參數。 ```py label.image = tatras ``` 為了不被垃圾收集,必須存儲圖像引用。 ```py w, h = self.img.size self.master.geometry(("%dx%d+300+300") % (w, h)) ``` 窗口的大小適合圖像大小。 ## 從 URL 讀取圖像 下一個示例從 URL 讀取圖像。 `read_from_url.py` ```py #!/usr/bin/python3 from PIL import Image import requests import sys url = 'https://i.ytimg.com/vi/vEYsdh6uiS4/maxresdefault.jpg' try: resp = requests.get(url, stream=True).raw except requests.exceptions.RequestException as e: sys.exit(1) try: img = Image.open(resp) except IOError: print("Unable to open image") sys.exit(1) img.save('sid.jpg', 'jpeg') ``` 該示例從 URL 讀取圖像并將其保存在磁盤上。 ```py import requests ``` 我們使用`requests`庫下載圖像。 ```py resp = requests.get(url, stream=True).raw ``` 我們將圖像讀取為原始數據。 ```py img = Image.open(resp) ``` `Image`是從響應對象創建的。 ```py img.save('sid.jpg', 'jpeg') ``` 圖像被保存。 ## Pillow 繪制圖像 Pillow 具有一些基本的 2D 圖形功能。 `ImageDraw`模塊為`Image`對象提供簡單的 2D 圖形。 我們可以創建新圖像,注釋或修飾現有圖像,以及即時生成圖形以供 Web 使用。 `draw2image.py` ```py #!/usr/bin/python3 from PIL import Image, ImageDraw img = Image.new('RGBA', (200, 200), 'white') idraw = ImageDraw.Draw(img) idraw.rectangle((10, 10, 100, 100), fill='blue') img.save('rectangle.png') ``` 該示例創建一個新圖像,并在圖像上繪制一個藍色矩形。 ```py img = Image.new('RGBA', (200, 200), 'white') ``` 創建一個新的`Image`。 圖像模式為`"RGBA"`。 大小為`200x200`,背景為白色。 ```py idraw = ImageDraw.Draw(img) ``` 根據圖像,我們創建`ImageDraw`對象。 現在我們可以在圖像上執行一些繪制操作。 ```py idraw.rectangle((10, 10, 100, 100), fill='blue') ``` 使用`rectangle()`方法,我們在圖像上繪制了一個藍色矩形。 ## 用 Pillow 創建水印 以下示例創建一個水印。 `watermark.py` ```py #!/usr/bin/python3 from PIL import Image, ImageDraw, ImageFont import sys try: tatras = Image.open("tatras.jpg") except: print("Unable to load image") sys.exit(1) idraw = ImageDraw.Draw(tatras) text = "High Tatras" font = ImageFont.truetype("arial.ttf", size=18) idraw.text((10, 10), text, font=font) tatras.save('tatras_watermarked.png') ``` 我們使用`ImageDraw`模塊創建水印。 ```py font = ImageFont.truetype("arial.ttf", size=18) ``` 我們創建 18 大小的 Arial 字體。 ```py idraw.text((10, 10), text, font=font) ``` 水印是通過`text()`方法創建的。 文本的默認顏色是白色。 我們使用創建的字體。 ![High Tatras](https://img.kancloud.cn/25/9a/259a9ef05d920b528c07ffcb88028536_350x232.jpg) 圖:High Tatras 在本教程中,我們使用了 Python Pillow 庫。 您可能也對以下相關教程感興趣: [Tkinter 教程](/tkinter/), [Matplotlib 教程](/python/matplotlib/), [Python Arrow 教程](/python/arrow/), [PyQt5 教程](/gui/pyqt5/)和[ [Python 教程](/lang/python/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看