1

This block of code is to save the image data read from a byte array to the Image img

val = bytearray(message.msg)
size = re.split(r',', message.messageSize)
img = Image.new("L", (int(size[0]), int(size[1])), 0)
pix = img.load()
counter = 0
for y in range(int(size[1])):
  for x in range(int(size[0])):
    pix[x, y] = val[counter]
    counter = counter + 1

Then if I img.save('test.png', 'PNG') and QPixmap('test.png'), it will display the image normally. But if I use the following method, the image would be twisted.(screenshot and image are attached to the hyperlinks below)

self.imgQ = ImageQt(img) # img is PIL Image type
img.save('test.png', 'PNG') # this will be success
pixMap = QtGui.QPixmap.fromImage(self.imgQ)
self.scene1.clear()
self.scene1.addPixmap(pixMap)
self.scene1.update()
self.viewMessage.fitInView(QRectF(0, 0, int(size[0]), int(size[1])), Qt.KeepAspectRatio)

after I implemented the code above, the image is shown twisted. But if I saved the image, the image looks correct.

[update] even if I use the PNG that I saved, it still twisted.

im = Image.open('hehe.png')

I have uploaded the PNG file here.

BadEggX
  • 195
  • 2
  • 13
  • I can't reproduce this. Please post a complete, minimal example that demonstrates the problem. – ekhumoro Dec 09 '14 at 01:01
  • I have just uploaded the PNG file that cause error. – BadEggX Dec 09 '14 at 01:23
  • There seems to be some issue with the image that PIL cannot handle. If the png is loaded directly into the QPixmap, there is no problem. It could be that some information is lost or corrupted during conversion to the QImage, but I can't see how to verify that at the moment... – ekhumoro Dec 09 '14 at 02:30
  • That's fine. Because I created the `Image im` from some image data myself. Currently, I saved the image to .png and read it from QPixmap to temporarily solve the problem. Maybe it was caused by some operation I did to the `Image im` before. But I still don't know how to debug this. Still thank you for taking time to help me! – BadEggX Dec 09 '14 at 10:14
  • What is the `message` object, and how is it created? Can you post the original raw image data on [pastebin](http://pastebin.com/)? – ekhumoro Dec 09 '14 at 17:41
  • I did `f = open('raw.txt', 'w')` and `f.write(val)`. `val` is the value that I saved to the image. And I have uploaded the [raw.txt](https://onedrive.live.com/redir?resid=89555F28C523FC84!56377&authkey=!AFv5Xj8a6VIFWNE&ithint=file%2ctxt) – BadEggX Dec 09 '14 at 19:57
  • In addition the variable `size = ['166', '150']` – BadEggX Dec 09 '14 at 20:03
  • If the raw data is loaded directly into a QImage (i.e. without using PIL at all), it produces the same twisted image. So there must be something wrong with the original data itself that somehow gets fixed by saving it. Is this the only image you've found that that has this problem? Where did you get it from? – ekhumoro Dec 10 '14 at 21:18

0 Answers0