I'm sending a raw data array (RGBA8888) from an application (in C++) to a django server in the http's post data encoded in base64 like:
field1=value1&prof_img=KSsf134SF38u483y... more bytes ....sdknADdSIU7rb==
I've already managed to decode it back from base64 to raw data, I've made some tests and the data gets converted correctly.
Now i'm stuck trying to do something with this decoded data so I can save it in a model's ImageField.
How could I save the image back to a .png file, or any other image file format? (I don't need alpha/transparency)
You just need to save to disk the stream of bytes contained in the decoded base64 string. I got it working simply by just doing:
import base64
# assume 'x' is the variable where you have the base64 encoded image
# 'target.png' is the file name where you'll be saving it
# 'wb' is for a raw binary write operation
fout = open('target.png','wb')
# decode it and write it
fout.write(base64.b64decode(x))
# flush it
fout.flush()
Once you got it written in the disk, just add/edit the instance to the model in django with the file path.
That worked for me, hope it helps!
I'm pretty sure that you need PIL:
from PIL import Image
img_size = (500, 500)
img = Image.frombytes('RGB', img_size, raw_data)
img.save('mypic.png')
The rest will be Django job, right?
Related
I am able to encode a gif to base64 by saving the data first.
imageio.mimsave(output_fn, [img_as_ubyte(frame) for frame in gif], fps=original_fps)
with open(output_fn, "rb") as gif_file:
detect_base64 = 'data:image/gif;base64,{}'.format(base64.b64encode(gif_file.read()).decode())
I need to find a way to encode the gif above in the form an array of images, with the corresponding fps into base64 without the need of saving it into output_fn first.
The general approach is to use a BytesIO as a replacement for an open file, i.e.
gif_file = io.BytesIO()
imageio.mimsave(gif_file, [img_as_ubyte(frame) for frame in gif], 'GIF', fps=original_fps)
detect_base64 = 'data:image/gif;base64,{}'.format(base64.b64encode(gif_file.getvalue()).decode())
I am currently trying to store an image in the form of base64 string to the MongoDB using GridFS, here is my working solution so far:
def upload(image_string):
image_data = base64.b64decode(image_string)
image = Image.open(io.BytesIO(image_data))
image.save("foo.jpeg")
with open("foo.jpeg", "rb") as img:
storage = GridFS(mongo.mydb, "fs")
storage.put(img, content_type='image/jpeg')
I was wondering if there is a way to directly upload the image instead of saving the image as a file and read it again for the Gridfs to upload? (Google App Engine doesn't allow file storage)
I looked at the documentation of the put function of Gridfs, but it is quite unclear on the exact type of data type it is taking.
"data can be either an instance of str (bytes in python 3) or a file-like object providing a read() method."
How do I convert the base64 string to bytes that gridfs supports?
Gridfs put method accepts binaries.
# encode your image to binary text
with open("unnamed.jpg", "rb") as image:
# read the image as text and convert it to binary
image_string = base64.b64encode(image.read())
# create Gridfs instance
fs = gridfs.GridFS(db)
# add the image to your database
put_image = fs.put(image_string)
I'm trying to send json dict that should contain Pillow image as one of his fields, to do that I have to convert the image to string.
I tried to use pillow function:
image.toString()
but still got it as bytes, so I tried to encode it:
buff = BytesIO()
image.save(buff, format="JPEG")
img_str = base64.b64encode(buff.getvalue())
but still got it as bytes.
How can I convert Pillow images to format that can be saved in json file?
In the comments, Mark Setchell suggests calling .decode('ascii') on the result of your b64encode call. I agree that this will work, but I think base64encoding to begin with is introducing an unnecessary extra step that complicates your code.*
Instead, I suggest directly decoding the bytes returned by image.tostring. The only complication is that the bytes object can contain values larger than 128, so you can't decode it with ascii. Try using an encoding that can handle values up to 256, such as latin1.
from PIL import Image
import json
#create sample file. You don't have to do this in your real code.
img = Image.new("RGB", (10,10), "red")
#decode.
s = img.tobytes().decode("latin1")
#serialize.
with open("outputfile.json", "w") as file:
json.dump(s, file)
(*but, to my surprise, the resulting json file is still smaller than one made with a latin1 encoding, at least for my sample file. Use your own judgement to determine whether file size or program clarity is more important.)
I use the following to exchange Pillow images via json.
import json
from PIL import Image
import numpy as np
filename = "filename.jpeg"
image = Image.open(filename)
json_data = json.dumps(np.array(image).tolist())
new_image = Image.fromarray(np.array(json.loads(json_data), dtype='uint8'))
1) I have an image that I converted to a string. It looks like this:
bytesimage = b'iVBORw0KGgoAAAANSUhEUgA.... etc etc
2) I can convert it to an 'bytesimage.png' using:
def StringToImage(self, stringname, imageoutput):
imgdata = base64.b64decode(stringname)
imagename = imageoutput
with open(imagename, 'wb') as f:
f.write(imgdata)
3) But then I want to save that image or string to memory to use in wxpython interface without needing to save the file. I have seen several related questions where the solution is using io.BytesIO, but I just cant connect the steps and both wxpython or PIL don't seem to read the bytes properly.
So to clarify:
I have a image stored in a string DONE
I can convert that to an image (if needed) but dont want to save it DONE
I need that string OR image (whichever is best) saved to memory NEEDS SOLVING
Then I want to be able to use that image in wxpython (I can open in PIL first if required)
Any help would be fantastic!
StringIO seems to be the way to go. It allows you to pass the decoded string directly to PIL.
import base64
from PIL import Image
import StringIO
# Banana emoji (JPG) as a b64 string.
b64_img_str = '/9j/4AAQSkZJRgABAQEAYABgAAD/4QCKRXhpZgAATU0AKgAAAAgABVEAAAQAAAAIAAAASlEBAAMAAAABA+YAAFECAAEAAAAYAAAAalEDAAEAAAABAAAAAFEEAAEAAAABBgAAAAAAAAAAAAAKAAAACgAAAAoAAAAKAAAACgAAAAoAAAAKAAAACgAAAP//AP///8zMAP8AADBkAP8A/5iYAP/bAEMAAgEBAgEBAgICAgICAgIDBQMDAwMDBgQEAwUHBgcHBwYHBwgJCwkICAoIBwcKDQoKCwwMDAwHCQ4PDQwOCwwMDP/bAEMBAgICAwMDBgMDBgwIBwgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDP/AABEIACMAIQMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCk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3KW9o7gXEs6TTTl4/yfwZo5zLA18fxBSVHE1pJumk24RirQVSbblOq7tycm5JWVopKK9jPMyw+KxPLhoxjGKStFPlXpdtu+7u279dj7Gooor9nPHCv5YdU/bu+Jmvf8Fufhd+zjqGqeH9R+EXwT/aA03wT4E0u78K6TcX/AIW0fT/E1pbWtta6m9sdQTENjaxySfaPMnWICV5MtkooA6D/AILcf8FQPjV/wTu/4Lr/ALQUnwd8ReH/AAfqGqf2It1qn/CHaLqGqyxSeH9GL2/226tJbkW5a3hfyBIIg6bwm4lj/S98J/hboPwO+FnhnwT4Wsf7L8M+D9KtdE0iz86Sf7JZ20KQwReZIzSPtjRV3OzMcZJJyaKKAOgooooA/9k='
# Decode back to the original bytes
new_img_str = base64.b64decode(b64_img_str)
# Use StringIO to provide an in-memory buffer that we can use
# to pass the image string to PIL.
sio = StringIO.StringIO(new_img_str)
img = Image.open(sio)
# Display the image
img.show()
I need to convert compressed image column data from windows sql server to image file and save it to file system.
data is in github gist
I am using Python 2.7.2, Pillow on mac.
Thank you !
What I did was opening your gist in my browser, then save as... to a file named 'chenchi.txt'.
I then used this program to convert the hex-encoded string to raw bytes and load them into Pillow to make an image out of it:
from PIL import Image
import StringIO
import binascii
# In your case, 's' will be the string from the field
# in the database.
s = open("chenchi.txt").read()
# chop off the '0x' at the front.
s = s[2:]
# Decode it to binary.
binary = binascii.unhexlify(s)
# Wrap the bytes in a memory stream that can be read like a file.
bytes = StringIO.StringIO(binary)
# Use pillow to read the memory stream into an image (it autodetects the format).
im = Image.open(bytes)
# And show it. Or you could .save() it.
im.show()
Worked to me using b16decode.
My exported image from sql is something like that: 'FFD8FFE000104A46494600010101004800480000FFE13...'
So I had to convert the content and saved into a file.
source = 'data.dat'
destination = 'data.jpg'
with open(source, 'r') as f:
content = f.read()
content = base64.b16decode(content)
with open(destination, 'wb') as g:
g.write(content)