python等比例壓縮圖片大小並添加指定背景-數據集準備

from PIL import Image
import os
import random
img_path =r"H:\數碼相機數據\3"#轉換數據地址
num =0
for filename in os.listdir(img_path):
    num+=1    
    img =Image.open(os.path.join(img_path,filename))    
    a=random.randint(0,255) #背景顏色  
    b=random.randint(0,255)
    c=random.randint(0,255)
    board =Image.new("RGB",(416,416),(a,b,c))
    img1 =img.convert("RGB")
    img1.thumbnail((416,416))
    board.paste(img1)
    # board.show()
    board.save(r"C:\U\video{}.jpg".format(num))#存儲地址

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章