Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read and view image.png ?
#11
(Mar-01-2021, 05:07 PM)JohnnyCoffee Wrote:
(Mar-01-2021, 03:48 PM)ndc85430 Wrote: You haven't really said what the problem is with that code. All that does is read the bytes. Does it fail in some way? Is something else not working? You don't show what you do after the read, so it's pretty hard to help. Don't make us guess!

I didn't get it right, but that's okay, follow the complete code:

from wsgiref.simple_server import make_server

def hello_world_app(environ, start_response):

    o_file = open("cloud.png", "rb")
    o_file = o_file.read()

    status = '200 OK'  # HTTP Status
    headers = [("Content-type", "image/png; charset=utf-8")]
    start_response(status, headers)

    # The returned object is going to be printed
    return [str(o_file).encode("utf-8")]

with make_server('', 8000, hello_world_app) as httpd:

    # Function -> Imprime no terminal reference
    print(
        'Running Kosmos Application\n'
        'Browser Access - http://127.0.0.1:8000\n'
        'Crl+c for flow command or Crl+z for stop'
    )
    # Serve until process is killed
    httpd.serve_forever()

You seem to be severely confused about what character encodings are useful for.

Probably read Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) before you try to read the rest of this answer.

You are replacing the binary PNG data with mojibake when you encode it. You should not be manipulating the data at all; just read it into a buffer, and send it to the client. End of story.

def hello_world_app(environ, start_response):
    with open("cloud.png", "rb") as i_file:
        o_file = i_file.read()

    status = '200 OK'  # HTTP Status
    headers = [("Content-type", "image/png")]  # no ; charset here!
    start_response(status, headers)

    return [o_file]
Credits : https://stackoverflow.com/users/874188/tripleee
Reply
#12
Your assumption has always been that there is an error reading the image. I don't think that is the problem. I think that the code used to convert the image bytes into an image is where the error occurs. You should definitely not be encoding or decoding the file. An image file is not Unicode. But you still haven't shown how the image data is converted into an image.
Reply
#13
(Mar-02-2021, 04:20 PM)deanhystad Wrote: Your assumption has always been that there is an error reading the image. I don't think that is the problem. I think that the code used to convert the image bytes into an image is where the error occurs. You should definitely not be encoding or decoding the file. An image file is not Unicode. But you still haven't shown how the image data is converted into an image.

That would be a topic for another topic, thankful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read from image with several languages using inage_to_string()? EvilSnail 0 2,096 Nov-13-2021, 03:11 PM
Last Post: EvilSnail

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020