Home > other >  What are those two representations of the same jpg image and how to convert one to another?
What are those two representations of the same jpg image and how to convert one to another?

Time:01-21

First representation

First representation

Second representation

Second representation

Those are the same image. The first representation was given to me when I read the image in python, but my goal is to get to the second representation. How can I convert one to another? Is it possible?

EDIT1: It is a jpg image. I'm building a simple web server and trying to insert this image in a HTTP content to send to a web browser, but the browser doesn't seem to recognize the first representation (the one I've got with python), however, when I go to a site that has the same image and I analyze (using Wireshark) the HTTP packet, it sends me the second representation, so I thought I had to use the second one in order to send it through HTTP to a web browser.

CodePudding user response:

The first seems to be a byte string. It's essentially just a bunch of numbers in hexadecimal notation (16 digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) So \xff is 0xFF = 255 one after another. And those numbers can be used by various programs to mean different things, like you could assume they are indexes to be looked up in an ascii table or something like that or they could be information about the pixels or the structure and format of your file (would need to look up the documentation).

The second representation apparently converted them to some sort of text, essentially resulting in gibberish. Here are some ways to decode it: Convert bytes to a string

But unless you know what kind of data you actually have at hand, you probably will just produce nonsensical gibberish.

CodePudding user response:

When decoding the bytearray values (first representation) I've used latin-1 in python:

with open(path, "rb") as image_file:
    response_content = image_file.read().decode("latin-1")

Then, when I sent to the http server I also encoded using latin-1:

reply = http_handle(request)
    connection_socket.send(reply.encode('latin-1'))

The browser was able to recognize the image.

  •  Tags:  
  • Related