Python Forum

Full Version: NameError: name 'self' is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
I am trying to open .tif images, but I get this error:

Error:
cv2.error: OpenCV(4.4.0) /tmp/pip-req-build-h2062vqd/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
Any idea what this is? I searching it however..
Thank you again for the code!!
Have you tried several tif?

You can try to check file exists and frame = cv2.imread(path, 1) (1 for color, 0 for Gray)

Line 482 and following

    def openFile(self):
        print("open File Dialog")
        path, _ = qt.QFileDialog.getOpenFileName(self, "Open File", self.image_file_path,"Image Files (*.tif *.tiff *.png *.jpg)")
        if path:
            if os.path.isfile(path):
                self.image_file_path = path
                print("file exists:",self.image_file_path)
                isTest = True
                self.updateFrame()
            else:
                print("file not exists")
 
 
    def getFrame(self, path):
        if isTest:
            if os.path.isfile(path):
                frame = cv2.imread(path, 1)
                # OpenCV uses BGR as its default colour order for images
                self.img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            else:
                self.openFile()
 
        else:
        ........
Since I only use it to save image sections, I made a lite version.

It has a file selector.

https://github.com/Axel-Erfurt/OrthoViewLite

[Image: screenshot.png]
Yes many different .tif where tried.

I changed this line:
self.img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
with this line (that seems to work on a code without loading button)

self.img = cv2.imdecode(np.fromfile(frame, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
but I get this error:

Error:
self.img = cv2.imdecode(np.fromfile(frame, dtype=np.uint8), cv2.IMREAD_UNCHANGED) AttributeError: 'numpy.ndarray' object has no attribute 'flush'
I am trying with hours of google searching to figure out how to fix it...! Any ideas?

update: Ok, I will take a look. Thanks!!
(I posted the question and then I saw your last post with OrthoViewLite).
my numpy Version is 1.17.4
Options for imread

Quote:/* 8 bit, color or gray - deprecated, use CV_LOAD_IMAGE_ANYCOLOR */
#define CV_LOAD_IMAGE_UNCHANGED -1
/* 8 bit, gray */
#define CV_LOAD_IMAGE_GRAYSCALE 0
/* 8 bit unless combined with CV_LOAD_IMAGE_ANYDEPTH, color */
#define CV_LOAD_IMAGE_COLOR 1
/* any depth, if specified on its own gray */
#define CV_LOAD_IMAGE_ANYDEPTH 2
/* by itself equivalent to CV_LOAD_IMAGE_UNCHANGED
but can be modified with CV_LOAD_IMAGE_ANYDEPTH */
#define CV_LOAD_IMAGE_ANYCOLOR 4
Your OrthoViewLite code is fantastic!!! Many thanks!! So I tested many .tif images. Some open, some others not. When .tif images do not open, the terminal shows me this message:

Error:
[ WARN:0] global /tmp/pip-req-build-h2062vqd/opencv/modules/imgcodecs/src/grfmt_tiff.cpp (457) readData OpenCV TIFF: TIFFRGBAImageOK: Sorry, can not handle images with 32-bit samples no image!
you can try

frame = cv2.imread(path, cv2.IMREAD_UNCHANGED)
Mr. Axel_Erfurt it works! Amazing!!!! Thank you very much!!!
Where in the code can I read the label/filename (not the whole path) of the .tif file that I import?
Pages: 1 2 3 4