您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python PIL圖片怎么按比例裁剪”,在日常操作中,相信很多人在Python PIL圖片怎么按比例裁剪問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python PIL圖片怎么按比例裁剪”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
如圖片比例為 1:1 裁剪為 4:3
1.jpg
from PIL import Image def image_clip(filename, savename, width_scale, height_scale): """圖像裁剪 :param filename: 原圖路徑 :param savename: 保存圖片路徑 :param width_scale: 寬的比例 :param height_scale: 高的比例 """ image = Image.open(filename) (width, height), (_width, _height) = image.size, image.size _height = width / width_scale * height_scale if _height > height: _height = height _width = width_scale * height / height_scale image.crop((0, 0, _width, _height)).save(savename) # 左上角 # image.crop((0, height - _height, _width, height)).save(savename) # 左下角 # image.crop((width - _width, 0, width, _height)).save(savename) # 右上角 # image.crop((width - _width, height - _height, width, height)).save(savename) # 右下角 if __name__ == '__main__': filename = '1.jpg' savename = 'result.jpg' image_clip(filename, savename, width_scale=4, height_scale=3) # image_clip(filename, savename, width_scale=3, height_scale=4)
效果
使用 PIL 在圖片比例不變的情況下修改圖片大小。
Image.resize
def resize(self, size, resample=BICUBIC, box=None, reducing_gap=None): """ Returns a resized copy of this image. 返回此圖像的大小調整后的副本。 :param size: The requested size in pixels, as a 2-tuple: (width, height). param size: 請求的大小(以像素為單位),是一個二元數組:(width, height) :param resample: An optional resampling filter. This can be one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`, :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`, :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`. Default filter is :py:attr:`PIL.Image.BICUBIC`. If the image has mode "1" or "P", it is always set to :py:attr:`PIL.Image.NEAREST`. See: :ref:`concept-filters`. param resample: 一個可選的重采樣過濾器。 :param box: An optional 4-tuple of floats providing the source image region to be scaled. The values must be within (0, 0, width, height) rectangle. If omitted or None, the entire source is used. param box: 可選的4元浮點數,提供要縮放的源映像區域。 :param reducing_gap: Apply optimization by resizing the image in two steps. First, reducing the image by integer times using :py:meth:`~PIL.Image.Image.reduce`. Second, resizing using regular resampling. The last step changes size no less than by ``reducing_gap`` times. ``reducing_gap`` may be None (no first step is performed) or should be greater than 1.0. The bigger ``reducing_gap``, the closer the result to the fair resampling. The smaller ``reducing_gap``, the faster resizing. With ``reducing_gap`` greater or equal to 3.0, the result is indistinguishable from fair resampling in most cases. The default value is None (no optimization). param reducing_gap: 通過兩個步驟調整圖像大小來應用優化。 :returns: An :py:class:`~PIL.Image.Image` object. returns: 返回一個 PIL.Image.Image 對象 """
from PIL import Image image = Image.open('圖片路徑') # 調整圖片大小,并保持比例不變 # 給定一個基本寬度 base_width = 50 # 基本寬度與原圖寬度的比例 w_percent = base_width / float(image.size[0]) # 計算比例不變的條件下新圖的長度 h_size = int(float(image.size[1]) * float(w_percent)) # 重新設置大小 # 默認情況下,PIL使用Image.NEAREST過濾器進行大小調整,從而獲得良好的性能,但質量很差。 image = image.resize((base_width, h_size), Image.ANTIALIAS)
到此,關于“Python PIL圖片怎么按比例裁剪”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。