国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python批量裁剪圖片

這篇具有很好參考價(jià)值的文章主要介紹了Python批量裁剪圖片。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

? 前兩天想要把連續(xù)的不同幀的靜態(tài)圖片拼成一個(gè)GIF圖片,但是原來(lái)的圖片需要裁剪,而且存在很多張,幸好這么多張的圖片裁剪的位置是一樣的,于是我便嘗試用Python優(yōu)雅地批量裁剪這些圖片。

? 首先介紹一下Python裁剪照片的原理。代碼的輸入是圖片的地址和兩個(gè)點(diǎn)的坐標(biāo),這兩個(gè)點(diǎn)的坐標(biāo)分別表示一個(gè)矩形的左上角頂點(diǎn)和右下角頂點(diǎn),這個(gè)矩形就是你的裁剪區(qū)域。

? 寫代碼前,先引入一下所需要的庫(kù)。

from PIL import Image, ImageDraw, ImageFont

? 那么你一定會(huì)有個(gè)疑問(wèn),怎么確定圖片矩形區(qū)域的頂點(diǎn)位置呢?下面貼出一個(gè)在原圖像上繪制邊界框的代碼。

def draw_bbox(image_path, bbox, output_path):
    """
    Draw bounding box on the image.

    Parameters:
        image_path (str): Path to the input image file.
        bbox (tuple): Bounding box coordinates (left, upper, right, lower).
        output_path (str): Path to save the image with bounding box.

    Returns:
        None
    """
    # Open image
    img = Image.open(image_path)

    # Draw bounding box
    draw = ImageDraw.Draw(img)
    draw.rectangle(bbox, outline="red", width=3)

    # Add text with coordinates
    font = ImageFont.truetype("arial.ttf", 20)
    draw.text((bbox[0], bbox[1]), f"{bbox}", fill="red", font=font)

    # Save image with bounding box
    img.save(output_path)

input_image_path = r"F:\Desktop\woman.jpg"
output_image_path = r"F:\Desktop\woman.jpg"
crop_box = (700, 550, 1850, 1000)  # Define crop box (left, upper, right, lower)
draw_bbox(input_image_path, crop_box, output_image_path)

? crop_box(x1, y1, x2, y2),其中左上角頂點(diǎn)表示為(x1, y1),右下角頂點(diǎn)表示為(x2, y2)。但是你只能通過(guò)不斷摸索crop_box的取值,根據(jù)原圖像上繪制的邊界框,逐漸確定你最后的裁剪區(qū)域。下面給出運(yùn)行draw_bbox代碼的可視化例子。

Python批量裁剪圖片

? 用draw_bbox拿到合適的crop_box以后,下面給出裁剪圖片的代碼。

def crop_image(input_image_path, output_image_path, crop_box):
    """
    Crop an image using the specified crop box.

    Parameters:
        input_image_path (str): Path to the input image file.
        output_image_path (str): Path to save the cropped image.
        crop_box (tuple): Crop box coordinates (left, upper, right, lower).

    Returns:
        None
    """
    # Open image
    img = Image.open(input_image_path)

    # Crop image
    cropped_img = img.crop(crop_box)

    # Save cropped image
    cropped_img.save(output_image_path)

    print("Image cropped and saved successfully.")

? 最后給出裁剪以后的可視化例子。

Python批量裁剪圖片

? 如果想要批量裁剪圖片的話,就在外面套一個(gè)循環(huán)就可以了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-843699.html

到了這里,關(guān)于Python批量裁剪圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包