Issue
I want to save PIL image.
I tried to do it by pickle and I could make it.
For example, I want to save it by QtCore.QDataStream
.
Can I do it?
Here is the original code:
from PIL import Image
import numpy as np
import pickle
filename = 'Any_Image.png'
im = Image.open(filename)
data = np.array(im)
f = open("test_file.dat","wb")
print(type(data))
dumps = pickle.dump(data,f)
f = open("test_file.dat","rb")
tumps = pickle.load(f)
array = Image.fromarray(tumps)
array.show()
Solution
You can do similar thing with QDataStream, just that you need to convert the image numpy array to bytes
Here's a simple implementation:
from PIL import Image
import numpy as np
from PyQt5 import QtCore
filename = 'Goku.jpg'
im = Image.open(filename)
data = np.array(im)
file_ = QtCore.QFile("test_file.dat")
file_.open(QtCore.QIODevice.WriteOnly)
qdatastream = QtCore.QDataStream(file_)
bytedata = QtCore.QByteArray(data.tobytes())
print(qdatastream)
qdatastream << bytedata
Answered By - Nishant Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.