如何使用Pyhton裁剪图像并将结果保存在另一个文件夹中



我有一个使用python裁剪图像的函数。现在它将处理1乘1的图像,但是,我想在1个文件夹中裁剪所有图像。我需要修改功能,以便能够处理1个文件夹中的所有图像,并将结果保存在excel中。

def send_request_croppingsegm(file_path, doctype):
url = 'http://52.77.70.50:8006/detectGeneralDocumentV3/single'
files=[ ('imageFile', (file_path.split('/')[-1], open(file_path, 'rb'), 'image/png')) ]
headers = {}
payload = {'documentType': doctype}
res = requests.request("POST", url, data=payload, files=files, verify=verify).text
res = json.loads(res)
cropped_data64 = base64.b64decode(res['document_image'])
cropped_img = Image.open(io.BytesIO(cropped_data64))    
return cropped_img
DEFAULT_DIR = 'C:/Users/API Test'
fn = 'ucf.png'
imgpath = os.path.join(DEFAULT_DIR, fn)
cropped = send_request_croppingsegm(imgpath, 2)
plt.imshow(cropped)
import os
# list all files in directory
for i in os.listdir(DEFAULT_DIR):
# if it ends with '.png'
if i.endswith(".png"):
imgpath = os.path.join(DEFAULT_DIR, i)
cropped = send_request_croppingsegm(imgpath, 2)
plt.imshow(cropped)

最新更新