<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                PIL:Python Imaging Library,已經是Python平臺事實上的圖像處理標準庫了。PIL功能非常強大,但API卻非常簡單易用。 由于PIL僅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基礎上創建了兼容的版本,名字叫[Pillow](https://github.com/python-pillow/Pillow),支持最新Python 3.x,又加入了許多新特性,因此,我們可以直接安裝使用Pillow。 ### 安裝Pillow 在命令行下直接通過pip安裝: ~~~ $ pip install pillow ~~~ 如果遇到`Permission denied`安裝失敗,請加上`sudo`重試。 ### 操作圖像 來看看最常見的圖像縮放操作,只需三四行代碼: ~~~ from PIL import Image # 打開一個jpg圖像文件,注意是當前路徑: im = Image.open('test.jpg') # 獲得圖像尺寸: w, h = im.size print('Original image size: %sx%s' % (w, h)) # 縮放到50%: im.thumbnail((w//2, h//2)) print('Resize image to: %sx%s' % (w//2, h//2)) # 把縮放后的圖像用jpeg格式保存: im.save('thumbnail.jpg', 'jpeg') ~~~ 其他功能如切片、旋轉、濾鏡、輸出文字、調色板等一應俱全。 比如,模糊效果也只需幾行代碼: ~~~ from PIL import Image, ImageFilter # 打開一個jpg圖像文件,注意是當前路徑: im = Image.open('test.jpg') # 應用模糊濾鏡: im2 = im.filter(ImageFilter.BLUR) im2.save('blur.jpg', 'jpeg') ~~~ 效果如下: ![PIL-blur](http://www.liaoxuefeng.com/files/attachments/001407671964310a6b503be6fcb4648928e2e4c522d04c7000) PIL的`ImageDraw`提供了一系列繪圖方法,讓我們可以直接繪圖。比如要生成字母驗證碼圖片: ~~~ from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 隨機字母: def rndChar(): return chr(random.randint(65, 90)) # 隨機顏色1: def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 隨機顏色2: def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 240 x 60: width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255, 255, 255)) # 創建Font對象: font = ImageFont.truetype('Arial.ttf', 36) # 創建Draw對象: draw = ImageDraw.Draw(image) # 填充每個像素: for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) # 輸出文字: for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 模糊: image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg') ~~~ 我們用隨機顏色填充背景,再畫上文字,最后對圖像進行模糊,得到驗證碼圖片如下: ![驗證碼](http://www.liaoxuefeng.com/files/attachments/0014076720724832de067ce843d41c58f2af067d1e0720f000) 如果運行的時候報錯: ~~~ IOError: cannot open resource ~~~ 這是因為PIL無法定位到字體文件的位置,可以根據操作系統提供絕對路徑,比如: ~~~ '/Library/Fonts/Arial.ttf' ~~~ 要詳細了解PIL的強大功能,請請參考Pillow官方文檔: [https://pillow.readthedocs.org/](https://pillow.readthedocs.org/) ### 小結 PIL提供了操作圖像的強大功能,可以通過簡單的代碼完成復雜的圖像處理。 ### 參考源碼 [https://github.com/michaelliao/learn-python3/blob/master/samples/packages/pil/use_pil_resize.py](https://github.com/michaelliao/learn-python3/blob/master/samples/packages/pil/use_pil_resize.py) [https://github.com/michaelliao/learn-python3/blob/master/samples/packages/pil/use_pil_blur.py](https://github.com/michaelliao/learn-python3/blob/master/samples/packages/pil/use_pil_blur.py) [https://github.com/michaelliao/learn-python3/blob/master/samples/packages/pil/use_pil_draw.py](https://github.com/michaelliao/learn-python3/blob/master/samples/packages/pil/use_pil_draw.py)
                  <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>

                              哎呀哎呀视频在线观看