Python | 八個(gè)圖片自動(dòng)化腳本,一定有你用得上的!
這次和大家分享8個(gè)實(shí)用的Python圖片處理腳本,包括重命名、裁剪、調(diào)整大小、添加水印、格式轉(zhuǎn)換、圖片合并、添加邊框和生成縮略圖。
如果本文對(duì)你有幫助,煩請(qǐng)給個(gè)一鍵三連(點(diǎn)贊、轉(zhuǎn)發(fā)、在看),這對(duì)我很重要!
1. 腳本一:批量重命名圖片
1.1 使用場(chǎng)景
你是不是遇到過(guò)這樣的情況,下載了一大堆圖片,卻都是毫無(wú)意義的默認(rèn)名字,比如DSC001, DSC002等。如果能按照某個(gè)規(guī)律批量重命名,那將會(huì)方便很多。這個(gè)腳本將展示如何使用Python批量重命名文件夾中的圖片。
1.2 示例代碼
import os
def batch_rename(path, new_name):
for count, filename in enumerate(os.listdir(path)):
file_ext = filename.split('.')[-1]
new_filename = f"{new_name}_{count + 1}.{file_ext}"
src = os.path.join(path, filename)
dst = os.path.join(path, new_filename)
os.rename(src, dst)
print("重命名完成!")
# 調(diào)用函數(shù)
batch_rename('path/to/your/images', 'new_image_name')
2. 腳本二:裁剪圖片
2.1 使用場(chǎng)景
有時(shí)候我們只需要圖片中的一部分,比如頭像、標(biāo)識(shí)等。這時(shí)候使用Python腳本進(jìn)行批量裁剪,可以幫你快速得到想要的圖片部分。
2.2 示例代碼
from PIL import Image
def crop_image(input_path, output_path, crop_area):
image = Image.open(input_path)
cropped_image = image.crop(crop_area)
cropped_image.save(output_path)
print(f"裁剪完成,保存為{output_path}")
# 調(diào)用函數(shù)
crop_image('path/to/your/image.jpg', 'path/to/save/cropped_image.jpg', (100, 100, 400, 400))
3. 腳本三:調(diào)整圖片大小
3.1 使用場(chǎng)景
在制作網(wǎng)頁(yè)或者需要上傳圖片到某些平臺(tái)時(shí),通常要求圖片大小一致。這個(gè)腳本能幫你批量調(diào)整圖片的尺寸,省時(shí)又省力。
3.2 示例代碼
from PIL import Image
def resize_image(input_path, output_path, size):
image = Image.open(input_path)
resized_image = image.resize(size)
resized_image.save(output_path)
print(f"調(diào)整大小完成,保存為{output_path}")
# 調(diào)用函數(shù)
resize_image('path/to/your/image.jpg', 'path/to/save/resized_image.jpg', (800, 800))
4. 腳本四:添加水印
4.1 使用場(chǎng)景
為了保護(hù)自己的圖片版權(quán)或者想顯示一些標(biāo)識(shí),你可能需要為圖片添加水印。使用這個(gè)腳本,可以方便地在圖片上加上你想要的水印。
4.2 示例代碼
from PIL import Image, ImageDraw, ImageFont
def add_watermark(input_path, output_path, watermark_text, position):
image = Image.open(input_path)
watermark = Image.new('RGBA', image.size)
font = ImageFont.truetype("arial.ttf", 36)
draw = ImageDraw.Draw(watermark, 'RGBA')
draw.text(position, watermark_text, font=font, fill=(255, 255, 255, 128))
watermarked_image = Image.alpha_composite(image.convert('RGBA'), watermark)
watermarked_image.save(output_path)
print(f"添加水印完成,保存為{output_path}")
# 調(diào)用函數(shù)
add_watermark('path/to/your/image.jpg', 'path/to/save/watermarked_image.png', 'Watermark', (10, 10))
5. 腳本五:批量轉(zhuǎn)換圖片格式
5.1 使用場(chǎng)景
當(dāng)需要統(tǒng)一圖片格式,比如從JPEG轉(zhuǎn)換為PNG,以適應(yīng)某些平臺(tái)或減少文件大小時(shí),可以使用這個(gè)腳本。
5.2 示例代碼
from PIL import Image
import os
def batch_convert_format(input_folder, output_folder, target_format):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.lower().endswith(('jpeg', 'jpg', 'png', 'bmp')):
img = Image.open(os.path.join(input_folder, filename))
base = os.path.splitext(filename)[0]
new_filename = f"{base}.{target_format}"
img.save(os.path.join(output_folder, new_filename))
print(f"轉(zhuǎn)換 {filename} 為 {new_filename}")
# 調(diào)用函數(shù)
batch_convert_format('path/to/your/images', 'path/to/save/converted_images', 'png')
6. 腳本六:圖片合并
6.1 使用場(chǎng)景
有時(shí)候我們需要將多個(gè)圖片合并為一張圖片,比如制作拼圖或者報(bào)告封面。這個(gè)腳本可以幫助你實(shí)現(xiàn)這些需求。
6.2 示例代碼
from PIL import Image
def merge_images(image_paths, output_path, direction='horizontal'):
images = [Image.open(image) for image in image_paths]
if direction == 'horizontal':
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_image = Image.new('RGB', (total_width, max_height))
x_offset = 0
for img in images:
new_image.paste(img, (x_offset, 0))
x_offset += img.width
else:
widths, heights = zip(*(i.size for i in images))
max_width = max(widths)
total_height = sum(heights)
new_image = Image.new('RGB', (max_width, total_height))
y_offset = 0
for img in images:
new_image.paste(img, (0, y_offset))
y_offset += img.height
new_image.save(output_path)
print(f"圖片合并完成,保存為{output_path}")
# 調(diào)用函數(shù)
merge_images(['path/to/image1.jpg', 'path/to/image2.jpg'], 'path/to/save/merged_image.jpg', 'horizontal')
7. 腳本七:為圖片添加邊框
7.1 使用場(chǎng)景
在設(shè)計(jì)和展示圖片時(shí),加上合適的邊框可以使圖片看起來(lái)更加精美和專(zhuān)業(yè)。這個(gè)腳本可以為圖片添加不同顏色和寬度的邊框。
7.2 示例代碼
from PIL import ImageOps
def add_border(input_path, output_path, border_size, color):
image = Image.open(input_path)
bordered_image = ImageOps.expand(image, border=border_size, fill=color)
bordered_image.save(output_path)
print(f"添加邊框完成,保存為{output_path}")
# 調(diào)用函數(shù)
add_border('path/to/your/image.jpg', 'path/to/save/bordered_image.jpg', border_size=10, color='black')
8. 腳本八:生成圖像縮略圖
8.1 使用場(chǎng)景
為了快速瀏覽和管理大量圖片,我們常常需要生成縮略圖。這個(gè)腳本可以幫你生成指定大小的縮略圖。
8.2 示例代碼
from PIL import Image
def create_thumbnail(input_path, output_path, thumbnail_size):
image = Image.open(input_path)
image.thumbnail(thumbnail_size)
image.save(output_path)
print(f"生成縮略圖完成,保存為{output_path}")
# 調(diào)用函數(shù)
create_thumbnail('path/to/your/image.jpg', 'path/to/save/thumbnail_image.jpg', (150, 150))
9. 總結(jié)
掌握了這些基礎(chǔ)的圖片處理腳本后,你可以嘗試學(xué)習(xí)更多圖像處理的進(jìn)階技術(shù),比如圖像識(shí)別、分類(lèi)、增強(qiáng)等。