QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed
-
@gerrard87
I'm just not sure with Python's reference counting and garbage collection whether yourimg.data
stays in existence, as required by thatQImage
constructor. Why don't you try something like:img = QImage(width, height, QImage.Format_RGB888) img.loadFromData(img.data, len(img.data))
which I believe manages the data for you to see whether it makes any differenece?
If your app crashes after a while check computer's free memory available to see if it's endlessly allocating.
@JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
img.loadFromData(img.data, len(img.data))
i get a Error when i using:
img = QImage(img.data, width, height, step, QImage.Format_RGB888) img.loadFromData(img.data, len(img.data))
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
-
@JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
img.loadFromData(img.data, len(img.data))
i get a Error when i using:
img = QImage(img.data, width, height, step, QImage.Format_RGB888) img.loadFromData(img.data, len(img.data))
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
Whats wrong?
The error message already told you what is wrong.
Also easy to find out that there is no data property in QImage (read its docu).
What you need is rather https://doc.qt.io/qt-5/qimage.html#bits-1But i don't understand what you are doing.
You create a QImage from another one (first line), then you call loadFromData from exact same image (second line) - what is the point? -
@JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
img.loadFromData(img.data, len(img.data))
i get a Error when i using:
img = QImage(img.data, width, height, step, QImage.Format_RGB888) img.loadFromData(img.data, len(img.data))
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
But I copied my
img.data
from yourimg = QImage(img.data, width, height, step, QImage.Format_RGB888)
. I did say "something like", you are supposed to figure it yourself....Oh, I see, you are using the same-named local
img
variable for two different types of image object! Just why Python is so meh....You are using
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
. So use a different variable name for theQImage
now. -
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
But I copied my
img.data
from yourimg = QImage(img.data, width, height, step, QImage.Format_RGB888)
. I did say "something like", you are supposed to figure it yourself....Oh, I see, you are using the same-named local
img
variable for two different types of image object! Just why Python is so meh....You are using
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
. So use a different variable name for theQImage
now.@JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
But I copied my
img.data
from yourimg = QImage(img.data, width, height, step, QImage.Format_RGB888)
. I did say "something like", you are supposed to figure it yourself....Oh, I see, you are using the same-named local
img
variable for two different types of image object! Just why Python is so meh....You are using
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
. So use a different variable name for theQImage
now.mmh so just change this?
def cap_img(self, cap): ret, img = cap.read() img = cv2.flip(img,1)#flip cam img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) height, width, channel = img.shape step = channel * width img_new= QImage(img.data, width, height, step, QImage.Format_RGB888) if height != HEIGHT and width != WIDTH: img = img.scaled(WIDTH, HEIGHT) return img
would that be enough? its not working because scaled is not found (object has no attribute 'scaled')
-
@JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
But I copied my
img.data
from yourimg = QImage(img.data, width, height, step, QImage.Format_RGB888)
. I did say "something like", you are supposed to figure it yourself....Oh, I see, you are using the same-named local
img
variable for two different types of image object! Just why Python is so meh....You are using
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
. So use a different variable name for theQImage
now.mmh so just change this?
def cap_img(self, cap): ret, img = cap.read() img = cv2.flip(img,1)#flip cam img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) height, width, channel = img.shape step = channel * width img_new= QImage(img.data, width, height, step, QImage.Format_RGB888) if height != HEIGHT and width != WIDTH: img = img.scaled(WIDTH, HEIGHT) return img
would that be enough? its not working because scaled is not found (object has no attribute 'scaled')
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
img_new= QImage(img.data, width, height, step, QImage.Format_RGB888) if height != HEIGHT and width != WIDTH: img = img.scaled(WIDTH, HEIGHT)
Don't you think there's something wrong here?
And you forgot the copy - just saying it for the third time...
-
@JonB said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
AttributeError: 'QImage' object has no attribute 'data' ? Whats wrong?
But I copied my
img.data
from yourimg = QImage(img.data, width, height, step, QImage.Format_RGB888)
. I did say "something like", you are supposed to figure it yourself....Oh, I see, you are using the same-named local
img
variable for two different types of image object! Just why Python is so meh....You are using
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
. So use a different variable name for theQImage
now.mmh so just change this?
def cap_img(self, cap): ret, img = cap.read() img = cv2.flip(img,1)#flip cam img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) height, width, channel = img.shape step = channel * width img_new= QImage(img.data, width, height, step, QImage.Format_RGB888) if height != HEIGHT and width != WIDTH: img = img.scaled(WIDTH, HEIGHT) return img
would that be enough? its not working because scaled is not found (object has no attribute 'scaled')
@gerrard87 said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
its not working because scaled is not found (object has no attribute 'scaled')
Why do you think that is? You should understand what you are doing/changing, not just copy & paste.
-
I already told them on so that he simply should use QImage::copy() to make sure the image data is copied. But ...
@Christian-Ehrlicher said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
I already told them on so that he simply should use QImage::copy() to make sure the image data is copied. But ...
sorry i just started with python and im still learning. I got this code from a friend and now i must work with it.
Can you help me how the line with Qimage::copy() must look like? Still dont know how to make it work. -
Hi,
I would recommend that you do all image manipulation with OpenCV since you have the original data in it.
Then
return QImage(img.data, width, height, step, QImage.Format_RGB888).copy()
-
Hi,
I would recommend that you do all image manipulation with OpenCV since you have the original data in it.
Then
return QImage(img.data, width, height, step, QImage.Format_RGB888).copy()
@SGaist said in QImage(img.data,...) out of scope - img.data gets deleted when QImage is destroyed:
return QImage(img.data, width, height, step, QImage.Format_RGB888).copy()
ok thank you, but what you mean with doing all the stuff with OpenCV? What kind of function in particular? I thought i was already doing everything with OpenCV?!
THank You all. -
You are not doing the resizing with OpenCV.