0

As per the title says, I have inserted into my postgresql db a bunch of images that added into an array and used cursor to send to the db. The column in the db is bytea[], so it worked for me.

I used this code snippet to conver the image bytestream into binary, put it into an array and send to db

somearray.append(psycopg2.Binary(image.content))

But when running a select statement to get that array from the database, what i got back was a memoryview, which I'm not sure at this point how to convert it back to an array.

I'm really lost at this point after researching, as most posts tells me to use toByte() to convert the memoryview to some byte string, but my goal is to convert the memoryview into an array or binary items to convert back the binaries into images.

4
  • When you say you want to convert them back into images, is your goal to write the images to files? If so you can use open('target_file.jpg','wb+).write(memory_view_obj) to do that (file.write can take a memory view object, it doesn't need to be converted into bytes first). What do you plan on doing with the bytes? Commented Apr 5, 2020 at 16:24
  • Apologies but I think you misundertood me, what i did was create a bytea array array. As in when u look at the datatype in postgresql it says byea[] not bytea. What i did to send such data was to convert images into binary using pscopg2 add it into an array. I then sent it to postgresql as an array filled with binary images. Now getting it from postgresql (the bytea [] - not bytea), I get a memoryview, which i need to convert back to an array of binary items. This is where I'm stuck now Commented Apr 7, 2020 at 4:29
  • Ah, you're right I didn't see that part where you're using a bytea[]. Are you inserting multiple images into each byte array where each element in the array represents an image or does each bytea[] represent a single image? If it's the first one then you can use the following code to get the images. cursor.execute("select byte_array from test"); image_sets = [row[0] for row in cursor.fetchall()]; images_as_bytes = [bytes(image) for image_array in image_sets for image in image_array]; Commented Apr 7, 2020 at 11:18
  • 1
    Omg that solved it! Thank you! Can you post that as an answer and let me credit you for this solution! Commented Apr 8, 2020 at 10:39

1 Answer 1

0

For future-readers' reference the question refers to inserting multiple images into each byte array where each element in the array represents an image. You can use the following code to get the images in as bytes.

cursor.execute("select byte_array from test")
image_sets = [row[0] for row in cursor.fetchall()]
images_as_bytes = [bytes(image) for image_array in image_sets for image in image_array]
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.