How to change bytes of array into image?


How to change bytes of array into image?



I have a csv file, which was extracted from MS SQL database with image column included. My task is to convert the contents of each row in image column into image and save it as image file.



Here is the content of a row-



https://drive.google.com/open?id=1J_jz5vN8ATxJ5Qw_c8l0qqKFrIee8kHF



As the contents length exceeds the limited word count, I uploaded it to google drive.



I changed it to bytearray and try to write it to new file and read it as an image but I haven't got the image.



https://drive.google.com/open?id=1h-7cRiTWErZzmMJYmc6XmLmeLK982WZR



How could I convert that bytearray to the image?? I have tried writing it to new file and reading it with io. I have spent 8+ hours on this and have tried every solutions found on stackoverflow. Please help me find the solution.



Here is my code -


df = (pd.Series(df['image']))
df = df.dropna()

x = df[3]
print (x)
bytes = base64.b64decode(x)
print (bytes)
image = open("new.jpeg","wb")
image.write(bytearray(bytes))
image.close()





.jpeg is a compressed format. Try .bmp. You'll still need to write the proper headers so your viewer knows its dimensions.
– gilch
Jun 30 at 17:20






just tried .bmp. Still haven't got the image. I tried it creating new image with "RGB"(width,height) but didn't work.
– Kyaw Soe Hein
Jun 30 at 17:26





The headers for .bmp are more complex than that. Do you know the binary format used by the database? If it already includes headers the imghdr module might be able to tell you what it is.
– gilch
Jun 30 at 17:32





I am not sure about the binary format but the first file provided in the link is the contents of the first row in image column, which is an image string. After some googling, I assumed that it is encoded with base 64 and decoded it. Then I got the second byte file. I believe the header is included in it but still don't know how to fully convert it to an image.
– Kyaw Soe Hein
Jun 30 at 17:51




1 Answer
1



The row is in hexadecimal format. You can strip the leading 0x and then convert to bytes with


0x


import codecs
data = codecs.decode(x[2:], 'hex')



Or, equivalently, with


data = bytes.fromhex(x[2:])



(Hexadecimal is such a common format that bytes has an alternate constructor for it.)


bytes



Then imghdr reports it's a jpeg.


imghdr


import imghdr
print(imghdr.what(None, h=data))



If you save those bytes in a .jpeg file you should be able to open it.


.jpeg


with open('foo.jpg', 'wb') as foo:
foo.write(data)



enter image description here





Thank you so much sir, you just saved my life.
– Kyaw Soe Hein
Jun 30 at 17:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API