从服务器将图像流另存为jpeg到本地目录

时间:2019-01-13 23:35:44

标签: python image server raspberry-pi

我想从客户端向服务器发送图像流,服务器获取它,现在我想将其作为jpeg文件保存到运行服务器的PC上的本地目录中。

客户端在树莓派上运行,在树莓派上用picam拍摄照片,并将其作为流发送到在另一台PC上运行的服务器。我现在要做的就是将此图像另存为.jpeg文件

到本地目录中

服务器端代码:     进口io     进口插座     导入结构     从PIL导入图像

# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
#path = 'c:/Users/addic_000/Desktop/'
server_socket = socket.socket()
server_socket.bind(('', 5540))
server_socket.listen(0)

# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')
try:
    while True:
        # Read the length of the image as a 32-bit unsigned int. If the
        # length is zero, quit the loop
        image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
        if not image_len:
            break
        # Construct a stream to hold the image data and read the image
        # data from the connection
        image_stream = io.BytesIO()
        image_stream.write(connection.read(image_len))

        # Rewind the stream, open it as an image with PIL and do some
        # processing on it

        image_stream.seek(0)
        image = Image.open(image_stream)
        #till this it work but give an error on the next line
        image.save('/Users/addic_000/Desktop/'+image, "jpeg")



finally:
    connection.close()
    server_socket.close()

这是客户端代码

import io
import socket
import struct
import time
import picamera

client_socket = socket.socket()
client_socket.connect(('172.18.0.26', 5540))


# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        # Start a preview and let the camera warm up for 2 seconds
        camera.start_preview()
        time.sleep(2)

        # Note the start time and construct a stream to hold image data
        # temporarily (we could write it directly to connection but in 
        # case we want to find out the size of each capture first to keep
        # our protocol simple)
        start = time.time()
        stream = io.BytesIO()
        for foo in camera.capture_continuous(stream, 'jpeg'):
            # Write the length of the capture to the stream and flush to
            # ensure it actually gets sent
            connection.write(struct.pack('<L', stream.tell()))
            connection.flush()
            print ("1")
            # Rewind the stream and send the image data over the wire
            stream.seek(0)
            connection.write(stream.read())
            # If we've been capturing for more than 30 seconds, quit
            if time.time() - start > 30:
                break
            # Reset the stream for the next capture
            stream.seek(0)
            stream.truncate()
    # Write a length of zero to the stream to signal we're done
    connection.write(struct.pack('<L', 0))
finally:
    connection.close()
    client_socket.close()

要保存到“ / Users / addic_000 / Desktop”的图片

0 个答案:

没有答案