-1

enter image description here

I want to view the original Image file.

But all Images are converted into the spotted,corrupted image.

It seems that the previous image is not equal to the behind image.

Please change the image size in the QImage's constructor into your arbitrary sample image's size.

If I excute this code, I caught the corrupted Image. Why?

enter image description here

I tried to change Format.Format_ARGB32_Premultiplied into various patterns.

But all patterns didn't go well.

from PIL import Image
import numpy as np
from PySide import QtCore
from PySide import QtGui
import sys
#the original file
filename = 'Any_Data.png'

im = Image.open(filename)
data = np.array(im)

file_ = QtCore.QFile("test_file.img")
file_.open(QtCore.QIODevice.WriteOnly)
qdatastream = QtCore.QDataStream(file_)


bytedatas = QtCore.QByteArray(data.tobytes())
#print(bytedatas)
#print(len(data.tobytes()),type(data))

qdatastream << bytedatas

output_file_ = QtCore.QFile("test_file.img")
output_file_.open(QtCore.QIODevice.ReadOnly)
qdatastream = QtCore.QDataStream(output_file_)
#the behind file
bytedata = QtCore.QByteArray()

qdatastream >> bytedata


Image = QtGui.QImage(220,133,QtGui.QImage.Format.Format_ARGB32_Premultiplied)
Image.fromData(bytedata)
def main():
    try:
        QtGui.QApplication([])
    except Exception as e:
        print(e)

    widget = QtGui.QLabel()

    pixmap = QtGui.QPixmap()
    pixmap.convertFromImage(Image)
    widget.setPixmap(pixmap)

    widget.show()
    sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
    main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Haru
  • 1,884
  • 2
  • 12
  • 30
  • Oh, really?Image is corrupted in any time. I don't know why it is. – Haru Jun 26 '18 at 06:23
  • It's strange to me, I tried again , I made it. – Haru Jun 26 '18 at 06:24
  • great , I also made it. I catched True. But Why can't I view the original Image... ? I should reedit my question. – Haru Jun 26 '18 at 06:29
  • @eyllanesc so,,,This is not main problem,,,but, do you know why I received -1 point? – Haru Jun 26 '18 at 06:38
  • I see , I thought It would be clear if the answer-persons executed my code. I received Three points shoot. Is it OK I delete my question? I'm very sorry you were kind to take after me, though. – Haru Jun 26 '18 at 06:47
  • ×take after 〇look after – Haru Jun 26 '18 at 06:55
  • @eyllanesc It is on the way from PIL to QImage, at the begenning of it, I started the image handling by PIL.But I use PySide, so I want to use QImage. – Haru Jun 26 '18 at 07:08
  • @eyllanesc I want to save Image Information as bytes. If the original Image is deleted by any reason, as long as I have the bytes information, I can reconstruct the Image. – Haru Jun 26 '18 at 07:18
  • yes, I sucessed saving it as bytes. But I recatch it and reshow it as the same image. I failed to do it.The question is there. – Haru Jun 26 '18 at 07:24

1 Answers1

1

When you convert and save data by bytes you are losing the information of the dimension. If your goal is to save the image information it is best to convert the PIL to QImage, and save this QImage in the file, then you can simply recover it as shown below:

import sys

from PIL import Image
import numpy as np

from PySide import QtCore, QtGui

filename = 'Any_Data.png'
filename_out = "data.bin"

im = Image.open(filename)
data = np.array(im)
qimagein = QtGui.QImage(data.data, data.shape[1], data.shape[0], data.strides[0], QtGui.QImage.Format_RGB888)

# save data
filewrite = QtCore.QFile(filename_out)
filewrite.open(QtCore.QIODevice.WriteOnly)
out_datastream = QtCore.QDataStream(filewrite)
out_datastream << qimagein

# read data
fileread = QtCore.QFile(filename_out)
fileread.open(QtCore.QIODevice.ReadOnly)
qimageout = QtGui.QImage()
in_datastream = QtCore.QDataStream(fileread)
in_datastream >> qimageout

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QLabel()
    pix = QtGui.QPixmap.fromImage(qimageout)
    w.setPixmap(pix)
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I must go out, I can't respond your comments soon , I'm sorry. – Haru Jun 26 '18 at 07:50
  • @user9402680 I have already corrected my code, you let me know if it works for you. – eyllanesc Jun 26 '18 at 07:51
  • Great,I appreciate you very much. I successed! – Haru Jun 26 '18 at 07:53
  • I think it is very important the Image's bit depth because it depends that the Image is clear or not.So I think I should switch every time when I select formats for any image.thank you. – Haru Jun 27 '18 at 03:42