我正在嘗試做一些影像解釋器并嘗試將它們直接存盤到 FTP 服務器。
但我的步驟是從本地檔案夾上傳影像,然后將其轉換為遮罩影像,然后將獲得最終輸出。但是在我的蒙版和最終輸出場景中,臨時影像被保存在本地,這是我不想要的。
但是如果不將影像存盤在本地,我就無法將檔案保存到 FTP。請幫我解決 output.save(mask_img_path) 如果沒有這一步如何將影像存盤在 FTP 中。
import errno
from flask import Flask,request
from rembg import remove
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import time
import random
import ftplib
import os
app = Flask(__name__)
input_img_path = "C:/Projects/Python/input/1.jpg"
output_img_path = "C:/Projects/Python/image-processing-2/image/output/"
mask_img_path = output_img_path 'mask.png'
mask_img = 'mask.png'
input_img = Image.open(input_img_path)
output = remove(input_img)
output.save(mask_img_path) // without this step unable to FTP the file below because this step storing the mask images in the folder.
ftp = ftplib.FTP('host', 'username', 'password')
# Store the mask files into FTP
with open(mask_img_path, "rb") as file:
ftp.storbinary('STOR %s' % mask_img, file)
if __name__ == '__main__':
app.run(debug=True,port=2000)
考慮到我所有的編碼步驟并嘗試通過 FTP 傳輸轉換后的影像。
uj5u.com熱心網友回復:
您可以
使用此代碼:
import io
import ftplib
from PIL import Image
from rembg import remove
image_filename = "1.jpg"
with Image.open(image_filename) as input_image:
with remove(input_image) as output:
with io.BytesIO() as temporary_file:
output.save(temporary_file, format='PNG')
temporary_file.seek(0) # Rewinding to the start of the file
with ftplib.FTP() as ftp_client:
remote_filename = 'mask.png'
ftp_client.connect(host='localhost', port=60000)
ftp_client.login(user='Manoj', passwd='Poosalingam')
ftp_client.storbinary(f'STOR {remote_filename}', temporary_file)
mask.png我在服務器上得到這張圖片:

那是你需要的嗎?
uj5u.com熱心網友回復:
我想你的問題真的是*“我如何在不需要ftplib.storbinary()磁盤檔案的情況下打電話。簡短的回答是通過io.BytesIO:
在這里我們制作一個PIL Image
from PIL import Image
from io import BytesIO
# Create dummy red PIL Image
output = Image.new('RGB', (320,240), 'red')
并將其保存在BytesIO:
# Create in-memory JPEG
buffer = BytesIO()
output.save(buffer, format="JPEG")
現在您應該可以將其傳遞給ftplib():
ftp.storbinary('STOR image.jpg', buffer)
在使用 FTP 之前,您可能需要執行以下操作:
buffer.seek(0)
我無法測驗,因為我沒有任何 FTP 服務器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534432.html
