4
_f = open("c:/go-next.png", "rb")
data = _f.read()
_f.close()
data.encode("utf-8")

# Error: UnicodeDecodeError: file <maya console> line 1: ascii # 

As you see I open a image file, and the data is type. But I have to convert it to utf-8. Maybe binary data has some extra char (or not), it conflict with conversion. Is there any way to solve it?

Hyun-geun Kim
  • 919
  • 3
  • 22
  • 37
  • 1
    I don't mean to be blunt, but converting a PNG to UTF8 doesn't make any sense. PNG is an image format. UTF8 is a text encoding. Can you explain more what it is you are trying to do? – Ned Batchelder Feb 07 '13 at 04:10
  • I try to POST my data to SERVER within HTTP. To do that, I refer to [http://www.doughellmann.com/PyMOTW/urllib2/#uploading-files](http://www.doughellmann.com/PyMOTW/urllib2/#uploading-files) and make it well. But, to POST special character (like Korean), I have to send it in "UTF-8" format. Server administrator tell me to do that :) – Hyun-geun Kim Feb 07 '13 at 09:18
  • 1
    @Hyun-geunKim using code in that link, you would `add_file` for the image, and `add_field` for text. `.add_file("my_image", "go-next.png", open("c:/go-next.png", "rb"), "image/png")` and for text `.add_field("key", "text")` – Esailija Feb 07 '13 at 10:18

5 Answers5

7

You can always map a str to unicode using the latin-1 codec. Once you have a unicode, you can always encode it in utf-8:

data.decode('latin-1').encode("utf-8")
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    While this is true, the result has no meaning. – Ignacio Vazquez-Abrams Feb 07 '13 at 04:09
  • 7
    Without meaning? A supercomputer was once asked, "What is the meaning of life?" I'm pretty sure it answered, `'42'.decode('latin1').encode('utf-8')`. :-) – unutbu Feb 07 '13 at 13:02
  • @IgnacioVazquez-Abrams The `latin-1` codec maps codepoints "0-255 to the bytes 0x0-0xff": https://docs.python.org/2/library/codecs.html#encodings-and-unicode So, this does exactly what you'd expect. – yingted Dec 31 '14 at 06:33
  • @yingted: I'm *pretty* sure that converting 0xd0 to 0xc3 0x90 atc. will corrupt the image, which is hardly what you'd want. – Ignacio Vazquez-Abrams Dec 31 '14 at 06:54
  • @IgnacioVazquez-Abrams I just hit this. I have to transfer a blob across a UTF-8 channel, but I don't have a base64 decoder on the receiving side. This would be the correct answer if the question made any sense. – yingted Dec 31 '14 at 06:58
  • @unutbu i was really struggling to solve an error and your comment gave me a good laugh :D – Pavithra B Nov 20 '22 at 17:39
4

Text encodings only apply to text. Do not attempt to use them on binary data.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

What you're trying to accomplish can probably be achieved by base64 encoding it.

 import base64
 encoded = base64.b64encode(image_binary_data)
timeartist
  • 304
  • 2
  • 5
0

Encoding means converting strings to storable bytes.
And Decoding means converting bytes to readable strings.

The data in your code is already encoded.

mitnk
  • 3,127
  • 2
  • 21
  • 28
0

Image cannot be converted into something like charters in utf8.

Amit Bhat
  • 13
  • 6