Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse Coordinates Do Not Update Correctly for Non-Square Images in PyQtGraph's ImageView #2940

Open
JosephDemarest opened this issue Feb 18, 2024 · 1 comment

Comments

@JosephDemarest
Copy link

JosephDemarest commented Feb 18, 2024

Short description

When using PyQtGraph's ImageView, mouse coordinates cease to update correctly when moving the cursor over an image with non-square dimensions (e.g., 4056x3040). Changing the image to a square dimension (e.g., 4056x4056) resolves the issue, suggesting a problem with handling non-square images.

Code to reproduce

import sys
import numpy as np
import pyqtgraph as pg
from PyQt5 import QtWidgets

class SimpleImageView(pg.ImageView):
    def __init__(self, image_data, *args, **kwargs):
        super(SimpleImageView, self).__init__(*args, **kwargs)
        self.setImage(image_data)
        self.scene.sigMouseMoved.connect(self.on_mouse_moved)

    def on_mouse_moved(self, pos):
        if self.imageItem.sceneBoundingRect().contains(pos):
            mouse_point = self.view.mapSceneToView(pos)
            x, y = int(mouse_point.x()), int(mouse_point.y())

            if 0 <= x < self.image.shape[1] and 0 <= y < self.image.shape[0]:
                print(f"Mouse coordinates: X: {x}, Y: {y}")
            else:
                print("Mouse is outside the image bounds.")

def main():
    app = QtWidgets.QApplication(sys.argv)
    image_data = np.random.rand(3040, 4056) * 255  # Square image
    image_data = image_data.astype(np.uint8)
    image_view = SimpleImageView(image_data)
    image_view.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Expected behavior

The application should continuously update and print the mouse coordinates as the cursor moves over different parts of the image, regardless of the image's aspect ratio.

Real behavior

The mouse coordinates stop updating and printing when the cursor moves beyond a certain point on non-square images. For example, with an image of size 4056x3040, the coordinates cease to update beyond 3040 in either dimension. However, when the image is resized to be square (e.g., 4056x4056), the full range of coordinates is correctly updated and printed.

Tested environment(s)

  • PyQtGraph version: 0.13.3
  • Qt Python binding: pyqtgraph.Qt PyQt5 5.15.9 Qt 5.15.8
  • Python version: 3.11.2
  • NumPy version: 1.24.2
  • Operating system: Linux-6.1.63-v8_16k+-aarch64-with-glibc2.36

AND

  • PyQtGraph version: 0.13.3
  • Qt Python binding: pyqtgraph.Qt PyQt5 5.15.10 Qt 5.15.11
  • Python version: 3.11.7
  • NumPy version: 1.26.4
  • Operating system: Linux-6.7.4-arch1-1-x86_64-with-glibc2.39

Additional context

The issue is not observed with square images, suggesting that the problem lies in how ImageView handles images with non-square dimensions. This could potentially be a limitation or bug within PyQtGraph's handling of such images.

@PhatCamel
Copy link

PhatCamel commented Feb 29, 2024

I am seeing similar issues but I don't know that it's exactly because the images are not square. I do find that when I do an image of 3000 x 3000 I get the proper cursor coordinate behavior, however when I do 3100 X 3000 I don't see the proper behavior. I also noticed though that with square images that are small, I tried 300 x 300 I see a similar issue as with the 3100 x 3000 image although this image is square. I also tried 1000 x 1000 and had the same issue (not sure where the cutoff point is).

EDIT:

I actually found the issue I was having was related to resizing the window of the widget, the coordinates do not update to where the image is after resizing for smaller images. I have verified though that having non-square images (even before any window resizing) seems to have the problem you described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants