使用Python進行圖像裁剪的多種方法及代碼示例
以下為您介紹使用 Python 進行圖像裁剪的多種方法及代碼示例:
使用 PIL 庫(Pillow):Pillow 是 Python 中一個強大的圖像處理庫,其截圖功能不僅可以獲取屏幕截圖,還能對截圖進行豐富的圖像處理操作。例如,您可以使用 Pillow 庫對截圖進行裁剪。
from PIL import Image
# 打開圖像
img = Image.open('image.jpg')
# 定義裁剪區(qū)域,例如左上角坐標為 (100, 100),右下角坐標為 (300, 300)
left = 100
top = 100
right = 300
bottom = 300
# 進行裁剪
cropped_img = img.crop((left, top, right, bottom))
# 保存裁剪后的圖像
cropped_img.save('cropped_image.jpg')
裁剪前:

裁剪后:

使用 OpenCV 庫:
import cv2
# 讀取圖像
img = cv2.imread('image.jpg')
# 定義裁剪區(qū)域,例如左上角坐標為 (100, 100),右下角坐標為 (300, 300)
cropped_img = img[100:300, 100:300]
# 保存裁剪后的圖像
cv2.imwrite('cropped_image.jpg', cropped_img)
此外,還有其他方式,如:
from PIL import Image
# 打開圖像
img = Image.open('image.jpg')
# 循環(huán)裁剪
for i in range(0, 350, 50):
region = img.crop((i, 0, i + 50, 50))
region.save(f'./test/{i}.png')
希望這些示例能夠幫助您使用 Python 進行圖像裁剪。在實際應用中,您可以根據(jù)具體需求調整裁剪區(qū)域的坐標和處理方式。
Python圖像裁剪的高級技巧
Python 提供了多種庫來實現(xiàn)圖像裁剪,例如 PIL(Pillow)和 OpenCV 等。高級技巧可能包括根據(jù)特定條件進行自適應裁剪,或者結合機器學習算法來智能識別裁剪區(qū)域。例如,通過分析圖像的像素分布和特征,確定最有價值的裁剪區(qū)域。另外,還可以使用多線程或分布式計算來加速大規(guī)模圖像的裁剪處理。
以下是一些更復雜的 Python 圖像裁剪代碼示例:
示例一:按比例裁剪圖像中心區(qū)域
from PIL import Image
def crop_center_image(image_path, output_path, percentage):
img = Image.open(image_path)
width, height = img.size
new_width = int(width * percentage)
new_height = int(height * percentage)
left = (width - new_width) // 2
top = (height - new_height) // 2
right = left + new_width
bottom = top + new_height
cropped_img = img.crop((left, top, right, bottom))
cropped_img.save(output_path)
crop_center_image('input_image.jpg', 'output_image.jpg', 0.8)
這個函數(shù)可以根據(jù)給定的百分比裁剪圖像的中心區(qū)域。
示例二:批量裁剪圖像并保存到不同文件夾
import os
from PIL import Image
def batch_crop_images(input_folder, output_folder, crop_size):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.endswith(('.jpg', '.png', '.jpeg')):
img = Image.open(os.path.join(input_folder, filename))
width, height = img.size
left = (width - crop_size[0]) // 2
top = (height - crop_size[1]) // 2
right = left + crop_size[0]
bottom = top + crop_size[1]
cropped_img = img.crop((left, top, right, bottom))
cropped_img.save(os.path.join(output_folder, filename))
batch_crop_images('input_folder', 'output_folder', (200, 200))
這個函數(shù)可以批量裁剪指定文件夾中的圖像,并將裁剪后的圖像保存到另一個文件夾中。
示例三:根據(jù)特定坐標范圍裁剪圖像
from PIL import Image
def crop_image_by_coordinates(image_path, output_path, x_range, y_range):
img = Image.open(image_path)
cropped_img = img.crop((x_range[0], y_range[0], x_range[1], y_range[1]))
cropped_img.save(output_path)
crop_image_by_coordinates('input_image.jpg', 'output_image.jpg', (100, 300), (50, 250))
這個函數(shù)可以根據(jù)給定的 x 和 y 坐標范圍裁剪圖像。
示例四:實現(xiàn)批量圖片裁剪
from PIL import Image
import os
def batch_crop_images(input_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.endswith(('.jpg', '.png', '.jpeg')):
img = Image.open(os.path.join(input_folder, filename))
# 定義裁剪區(qū)域,這里假設都是左上角坐標為 (100, 100),右下角坐標為 (300, 300)
left = 100
top = 100
right = 300
bottom = 300
cropped_img = img.crop((left, top, right, bottom))
cropped_img.save(os.path.join(output_folder, filename))
input_folder = 'input_images_folder'
output_folder = 'output_images_folder'
batch_crop_images(input_folder, output_folder)
把需要裁剪的圖片放到 ‘input_images_folder’ 文件夾,運行后進行裁剪并輸出到 ‘output_images_folder’ 文件夾中
在這個代碼中,首先定義了一個函數(shù)batch_crop_images,它接受輸入文件夾和輸出文件夾的路徑作為參數(shù)。函數(shù)首先檢查輸出文件夾是否存在,如果不存在則創(chuàng)建它。然后遍歷輸入文件夾中的所有文件,如果文件是圖片格式(.jpg、.png、.jpeg),則打開圖像,進行裁剪,并將裁剪后的圖像保存到輸出文件夾中。
你可以根據(jù)實際情況修改輸入文件夾和輸出文件夾的路徑以及裁剪區(qū)域的坐標。
總結
到此這篇關于使用Python進行圖像裁剪的多種方法及代碼示例的文章就介紹到這了,更多相關Python圖像裁剪內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Django REST框架創(chuàng)建一個簡單的Api實例講解
在本篇文章里小編給大家整理的是關于Django REST框架創(chuàng)建一個簡單的Api實例講解,有需要的朋友們可以學習下。2019-11-11
Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法示例
這篇文章主要介紹了Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法,涉及Python同時進行SQLServer與MongoDB數(shù)據(jù)庫的連接、查詢、讀取、寫入等相關操作實現(xiàn)技巧,需要的朋友可以參考下2018-06-06
Python讀取Pickle文件信息并計算與當前時間間隔的方法分析
這篇文章主要介紹了Python讀取Pickle文件信息并計算與當前時間間隔的方法,涉及Python基于pickle模塊操作文件屬性相關實現(xiàn)技巧,需要的朋友可以參考下2019-01-01
在pandas中遍歷DataFrame行的實現(xiàn)方法
這篇文章主要介紹了在pandas中遍歷DataFrame行的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10

