Python PIL圖片如何按比例裁剪
PIL圖片如何按比例裁剪
問(wèn)題描述
如圖片比例為 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調(diào)整圖片大小
使用 PIL 在圖片比例不變的情況下修改圖片大小。
介紹
Image.resize
def resize(self, size, resample=BICUBIC, box=None, reducing_gap=None):
"""
Returns a resized copy of this image.
返回此圖像的大小調(diào)整后的副本。
:param size: The requested size in pixels, as a 2-tuple:
(width, height).
param size: 請(qǐng)求的大小(以像素為單位),是一個(gè)二元數(shù)組:(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: 一個(gè)可選的重采樣過(guò)濾器。
: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元浮點(diǎn)數(shù),提供要縮放的源映像區(qū)域。
: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: 通過(guò)兩個(gè)步驟調(diào)整圖像大小來(lái)應(yīng)用優(yōu)化。
:returns: An :py:class:`~PIL.Image.Image` object.
returns: 返回一個(gè) PIL.Image.Image 對(duì)象
"""看代碼吧
from PIL import Image
image = Image.open('圖片路徑')
# 調(diào)整圖片大小,并保持比例不變
# 給定一個(gè)基本寬度
base_width = 50
# 基本寬度與原圖寬度的比例
w_percent = base_width / float(image.size[0])
# 計(jì)算比例不變的條件下新圖的長(zhǎng)度
h_size = int(float(image.size[1]) * float(w_percent))
# 重新設(shè)置大小
# 默認(rèn)情況下,PIL使用Image.NEAREST過(guò)濾器進(jìn)行大小調(diào)整,從而獲得良好的性能,但質(zhì)量很差。
image = image.resize((base_width, h_size), Image.ANTIALIAS)以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于python,Matplotlib繪制函數(shù)的等高線與三維圖像
這篇文章主要介紹了基于python,Matplotlib繪制函數(shù)的等高線與三維圖像,函數(shù)的等高線及其三維圖像的可視化方法,下面一起來(lái)學(xué)習(xí)具體內(nèi)容吧,需要的小伙伴可以參考一下2022-01-01
利用Python實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼處理
這篇文章主要介紹了利用Python實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼處理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
使用Python標(biāo)準(zhǔn)庫(kù)中的wave模塊繪制樂(lè)譜的簡(jiǎn)單教程
這篇文章主要介紹了使用Python標(biāo)準(zhǔn)庫(kù)中的wave模塊繪制樂(lè)譜,涉及到了numpy模塊和坐標(biāo)的可視化運(yùn)用,用到了需要的朋友可以參考下2015-03-03
快速實(shí)現(xiàn)基于Python的微信聊天機(jī)器人示例代碼
本篇文章主要介紹了快速實(shí)現(xiàn)基于Python的微信聊天機(jī)器人示例代碼,基于itchat開(kāi)發(fā),可以用它做一個(gè)微信聊天機(jī)器人,有興趣的可以了解一下。2017-03-03
Python利用字典樹(shù)實(shí)現(xiàn)獵詞游戲
獵詞(word hunt)是一類(lèi)很常見(jiàn)的游戲,給你一張字母組成的表,然后讓你在這些字母中盡可能多的去尋找單詞。這類(lèi)游戲用字典樹(shù)就能輕松完成,本文就來(lái)具體講講實(shí)現(xiàn)步驟,需要的可以參考一下2022-06-06
Python實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的示例代碼
本文主要介紹了Python實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證的示例代碼,當(dāng)用戶(hù)輸入身份證號(hào),按下檢查按鈕,即可判斷身份證號(hào)是否正確,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
python socket多線程通訊實(shí)例分析(聊天室)
這篇文章主要介紹了python socket多線程通訊方法,以聊天室程序?qū)嵗治隽薖ython基于Socket實(shí)現(xiàn)多線程通信的相關(guān)技巧,需要的朋友可以參考下2016-04-04

