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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # PIL PIL:Python Imaging Library,已經是Python平臺事實上的圖像處理標準庫了。PIL功能非常強大,但API卻非常簡單易用。 ### 安裝PIL 在Debian/Ubuntu Linux下直接通過apt安裝: ``` $ sudo apt-get install python-imaging ``` Mac和其他版本的Linux可以直接使用easy_install或pip安裝,安裝前需要把編譯環境裝好: ``` $ sudo easy_install PIL ``` 如果安裝失敗,根據提示先把缺失的包(比如openjpeg)裝上。 Windows平臺就去[PIL官方網站](http://pythonware.com/products/pil/)下載exe安裝包。 ### 操作圖像 來看看最常見的圖像縮放操作,只需三四行代碼: ``` import Image # 打開一個jpg圖像文件,注意路徑要改成你自己的: im = Image.open('/Users/michael/test.jpg') # 獲得圖像尺寸: w, h = im.size # 縮放到50%: im.thumbnail((w//2, h//2)) # 把縮放后的圖像用jpeg格式保存: im.save('/Users/michael/thumbnail.jpg', 'jpeg') ``` 其他功能如切片、旋轉、濾鏡、輸出文字、調色板等一應俱全。 比如,模糊效果也只需幾行代碼: ``` import Image, ImageFilter im = Image.open('/Users/michael/test.jpg') im2 = im.filter(ImageFilter.BLUR) im2.save('/Users/michael/blur.jpg', 'jpeg') ``` 效果如下: ![PIL-blur](https://box.kancloud.cn/2016-01-15_56988a25ea6da.png) PIL的`ImageDraw`提供了一系列繪圖方法,讓我們可以直接繪圖。比如要生成字母驗證碼圖片: ``` 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'); ``` 我們用隨機顏色填充背景,再畫上文字,最后對圖像進行模糊,得到驗證碼圖片如下: ![驗證碼](https://box.kancloud.cn/2016-01-15_56988a260f830.png) 如果運行的時候報錯: ``` IOError: cannot open resource ``` 這是因為PIL無法定位到字體文件的位置,可以根據操作系統提供絕對路徑,比如: ``` '/Library/Fonts/Arial.ttf' ``` 要詳細了解PIL的強大功能,請請參考PIL官方文檔: [http://effbot.org/imagingbook/](http://effbot.org/imagingbook/)
                  <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>

                              哎呀哎呀视频在线观看