Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:47:40+00:00 2026-06-12T04:47:40+00:00

I am having a problem reading png images from STDIN using PIL. When the

  • 0

I am having a problem reading png images from STDIN using PIL. When the image is written by PIL it is all scrambled, but if I write the file using simple file open, write and close the file is saved perfectly.

I have a program that dumps png files to stdout in a sequence, with no compression, and I read that stream using a python script which is suposed to read the data and do some routines on almost every png. The program that dumps the data writes a certain string to delimiter the PNGs files, the string is "{fim:FILE_NAME.png}"

The script is something like:

import sys
import re
from PIL import Image

png = None

for linha in sys.stdin: 
    if re.search('{fim:', linha):
        fname = linha.replace('{fim:','')[:-2]

        # writes data directly to file, works fine
        #f = open("/tmp/%s" % fname  , 'w')
        #f.write(png)
        #f.close()

        # create a PIL Image from data and writes to disk, fails fine
        im = Image.frombuffer("RGB",(640,480),png, "raw", "RGB", 0, 1)
        #im = Image.fromstring("RGB",(640,480),png)
        im.save("/tmp/%s" % fname)

        png = None

    else:
        if png is None:
            png = linha
        else:
            png+= linha

imagemagick identify from a wrong image:

/tmp/1349194042-24.png PNG 640×480 640×480+0+0 8-bit DirectClass 361KiB 0.010u 0:00.019

imagemagick identify from a working image:

/tmp/1349194586-01.png PNG 640×480 640×480+0+0 8-bit DirectClass 903KiB 0.010u 0:00.010

Does any one have an idea of what is happening? Is it something about little/big endians? I have tried Image.frombuffer, Image.fromstring, different modes, but nothing. It seems that there is more information on the buffer that the PIL expects.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:47:42+00:00Added an answer on June 12, 2026 at 4:47 am

    If the png variable contains the binary data from a PNG file, you can’t read it using frombuffer; that’s used for reading raw pixel data. Instead, use io.StringIO and Image.open, i.e.:

    import io
    from PIL import Image
    
    img = Image.open(io.StringIO(png))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem reading dates from a database using JPA. I'm using EclipseLink
I'm having a problem where instead of reading a text file from the location
I am reading XOR linked list (from Wikipedia).But I am having some problems in
I'm having a problem reading data from a text file into ms sql. I
I'm having a problem reading and processing data from my server. I've spent hours
I'm having some problem reading the date from an xlsx file, what I mean
I am having a problem reading back from windows azure table storage: Here is
I'm having a problem reading a value from an NSDictionary. Here is my setup:
I'm having a problem. I'm reading data from a socket, and when I read
I'm having problem reading from a plist file I have the Settings.plist file saved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.