2

I'm trying to convert some code in Python 2 to Python 3. I'm not too familiar with the changes in how encoding works between the two versions of Python, so was not exactly sure how to word the question.

Basically in Python 2 the code looks like this:

image_key = "image_3"
env = lmdb.open(some arguments here)

with env.begin(write=False) as txn:
    img_tmp = txn.get(image_key)
    img = Image.open(StringIO(img_tmp))

In Python 2, "img_tmp" would be a string object with unreadable characters (printing it gives me a mess: �PNGIHDR � �A�� gAMA �� �acHRMz&��� ��u0�`...). And the next line would open the image as a pillow image.

In Python 3, the line txn.get() would give me an error "TypeError: Won't implicitly convert Unicode to bytes; use .encode()" so I I followed the suggestion and converted the line to:

img_tmp = txn.get(img_key.encode())

However, img_tmp is now a bytes object that reads something like this: "b'\x89PNG\r\n\x1a\n\x00\ ..."

And the next line would no longer open the image. Any suggestions on how to change the code to get it to work?

1
  • I was able to generate a string object that looks similar to what I get in Python 2 using txn.get(image_key.encode()).decode("ascii", "replace") or txn.get(image_key.encode()).decode("utf-8", "replace") However i still can't open the pillow image. Commented Nov 9, 2018 at 15:08

1 Answer 1

1

You’re almost there: just use BytesIO instead of StringIO, since your binary data is a bytes and not a str.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.