2014-05-10 4 views
12

के साथ काम नहीं कर रही है, किसी कारण से, जब मैं बाइट्सियो स्टीम से एक छवि बनाने की कोशिश करता हूं, तो यह छवि की पहचान नहीं कर सकता है।पीआईएल ओपन() विधि बाइट्सियो

from PIL import Image, ImageGrab 
from io import BytesIO 

i = ImageGrab.grab() 
i.resize((1280, 720)) 
output = BytesIO() 
i.save(output, format = "JPEG") 
output.flush() 
print(isinstance(Image.open(output), Image.Image)) 

और त्रुटि फेंक देता है की स्टैक ट्रेस: ​​

Traceback (most recent call last): 
    File "C:/Users/Natecat/PycharmProjects/Python/test.py", line 9, in <module> 
    print(isinstance(Image.open(output), Image.Image)) 
    File "C:\Python27\lib\site-packages\PIL\Image.py", line 2126, in open 
    % (filename if filename else fp)) 
IOError: cannot identify image file <_io.BytesIO object at 0x02394DB0> 

मैं जनहित याचिका की तकिया कार्यान्वयन उपयोग कर रहा हूँ यहाँ मेरी कोड है। एक फ़ाइल वस्तु के रूप में BytesIO की

उत्तर

20

Think, आप छवि लिख लेने के बाद, फ़ाइल के कर्सर फ़ाइल के अंत में है, इसलिए जब Image.open() की कोशिश करता output.read() कॉल करने के लिए, इसे तुरंत एक EOF हो जाता है।

आप Image.open() को output पार करने से पहले एक output.seek(0) जोड़ने की जरूरत है।