2016-07-30 2 views
3

내 rbpi에서 내 서버로 이미지 스트림을 설정하고 싶습니다.베개 속성 오류

그래서 네트워크 스트림을 http://picamera.readthedocs.io/en/release-1.12/recipes1.html#streaming-capture에 기록하도록 설정하고 싶습니다.

잘되었지만 캡처 된 이미지를 저장하려고합니다.

-> (수정 된 서버 스크립트)

import io 
import socket 
import struct 
from PIL import Image 

# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means 
# all interfaces) 
server_socket = socket.socket() 
server_socket.bind(('0.0.0.0', 8000)) 
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) 
     print('Image is %dx%d' % image.size) 
     image.verify() 
     print('Image is verified') 
     im = Image.new("RGB", (640,480), "black") #the saving part 
     im = image.copy() 
     im.save("./img/test.jpg","JPEG") 
finally: 
    connection.close() 
    server_socket.close() 

그러나 그것은 나에게 다음과 같은 에러 코드를 반환

Traceback (most recent call last): 
    File "stream.py", line 33, in <module> 
    im = image.copy() 
    File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 781, in copy 
    self.load() 
    File "/usr/lib/python2.7/dist-packages/PIL/ImageFile.py", line 172, in load 
    read = self.fp.read 
AttributeError: 'NoneType' object has no attribute 'read' 

어떻게이 문제를 해결할 수 있습니까?

+1

'image.copy()'전에'image.load()'를 호출해야 할 수도 있습니다. 문서는 그 소리를 불필요하게 만들지 만 베개는 버그가있는 것으로 알려져 있습니다 ... – martineau

+0

781 줄의 오류를 줄입니다. 그러나 두 번째 오류는 여전히 있습니다. – Meitoasty

답변

2

나는 라스베리 파이가 없지만 어쨌든 문제를 재현 할 수 있는지를 결정했습니다. 또한, 입력을 위해 나는 모든 소켓 물건을 제거하기 위해 디스크에 이미지 파일을 만들었습니다. 과연 내가 만난 것과 똑같은 오류가 발생했다. (참고 :는 IMO이 단순화 자신을 수행하고 MCVE 문제합니다 (SO 도움말 센터에서 How to create a Minimal, Complete, and Verifiable example 참조) 설명 게시해야

내가 바로 image.load() 방법에 대한 호출을 추가 멀리 갈 문제를 얻으려면. . Image.open() 문과 상황이 일을 시작 후뿐만 아니라 오류가 사라했지만, 출력 파일도 잘 보였다

여기 수정 내 간단한 테스트 코드가 표시입니다 :.

import io 
import os 
from PIL import Image 

image_filename = 'pillow_test.jpg' 

image_len = os.stat(image_filename).st_size 
image_stream = io.BytesIO() 
with open(image_filename, 'rb') as imagefile: 
    image_stream.write(imagefile.read(image_len)) 
image_stream.seek(0) 
image = Image.open(image_stream) 
image.load() ########## ADDED LINE ########## 
print('Image is %dx%d' % image.size) 
image.verify() 
print('Image is verified') 
im = Image.new("RGB", (640,480), "black") #the saving part 
im = image.copy() 
im.save("pillow_test_out.jpg","JPEG") 
print('image written') 

단서가되었다 PIL.Image.open() 함수의 pillow documentation으로부터이 통로 :

이것이 지연 동작이고; 이 기능은 파일을 식별하지만 파일 는 열린 상태로 유지하고 데이터 (또는   전화     부하()   방법)을 처리하기 위해 을 시도 할 때까지 실제 이미지 데이터를 파일에서 읽을 수 없습니다.

(강조 광산) 당신은 그것이 "파일"을 확인하기 위해 데이터를로드 필요로 확인하는 것 같아 이후 image.verify()이 불필요 할 것이라고 생각합니다. 내 짐작으로 이것은 버그 일 가능성이 높습니다.