Python Forum
NameError: name 'self' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'self' is not defined
#11
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!!
Reply
#12
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:
        ........
hobbyist likes this post
Reply
#13
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]
hobbyist likes this post
Reply
#14
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).
Reply
#15
my numpy Version is 1.17.4
hobbyist likes this post
Reply
#16
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
hobbyist likes this post
Reply
#17
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!
Reply
#18
you can try

frame = cv2.imread(path, cv2.IMREAD_UNCHANGED)
hobbyist likes this post
Reply
#19
Mr. Axel_Erfurt it works! Amazing!!!! Thank you very much!!!
Reply
#20
Where in the code can I read the label/filename (not the whole path) of the .tif file that I import?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Kivy] NameError: name 'App' is not defined nengkya 4 13,646 Apr-02-2017, 07:15 PM
Last Post: metulburr
  [WxPython] NameError: global name 'PyAssertionError' is not defined merlem 5 6,280 Jan-28-2017, 01:59 PM
Last Post: merlem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020