Python 裁切裁剪圖片 crop |
您所在的位置:网站首页 › pil裁剪图片 › Python 裁切裁剪圖片 crop |
本篇 ShengYu 將介紹如何使用 Python 來裁切裁剪圖片,在做影像處理時常常會需要用裁切裁剪圖片的功能,這邊我們使用 python 的 PIL 模組來作圖片的裁切裁剪。 安裝 PIL基本上新版本的 Python 應該都有內建 PIL,如果還未安裝 PIL 的話請參考這篇。 使用範例以下範例是 ShengYu 將 lena.jpg 這張圖片(512x512)透過 crop() 函式裁切裁剪範圍從(0,0)位置到(256,512)位置,最後顯示並且存檔。 crop-image.py1234567891011#!/usr/bin/env python3# -*- coding: utf-8 -*-from PIL import Imageimg = Image.open("lena.jpg")print(img.size)img.show()new_img = img.crop((0, 0, 256, 512)) # (left, upper, right, lower)new_img.show()new_img.save("lena-crop.jpg")結果如下圖所示:左邊為原圖,右邊為裁切裁剪後的圖。 Image.crop 參數的詳細細節請參考這裡 相關主題Python 圖片模糊化 blurPython 旋轉圖片 rotatePython 縮放圖片 resizePython 在圖片上繪製文字 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |