Fixing high aspect ratio ImageView's by streching the image

270 views
Skip to first unread message

Oier Arcelus

unread,
Aug 22, 2022, 9:47:35 AM8/22/22
to pyqtgraph
Dear all,

I have an image to be shown using pyqtgraph. For this I use a numpy 2D array, which has a many elements along axis 1, but less along axis 0. As a result, I obtain ImageView's with very high aspect ratio, and I would like to stretch it such that it fits to the original view of the window that its shown in the screen.

I cannot find any entries about this in the forum and any help about this would be greatly appreaciated.

Jerzy Karczmarczuk

unread,
Aug 22, 2022, 10:12:23 AM8/22/22
to pyqt...@googlegroups.com

Le 22/08/2022 à 15:47, Oier Arcelus a écrit :

I have an image to be shown using pyqtgraph. For this I use a numpy 2D array, which has a many elements along axis 1, but less along axis 0. As a result, I obtain ImageView's with very high aspect ratio, and I would like to stretch it such that it fits to the original view of the window that its shown in the screen.

I cannot find any entries about this in the forum and any help about this would be greatly appreaciated.

Use (e.g.) PIL (Pillow...) and numpy, say:

from PIL import Image
import numpy as np
img = Image.open('yourimage.png')

nimg = img.resize((200, 100))  # for example
nimg.show() # or not.

tbl=np.array(nimg)

This is all. Unless I misunderstood your problem. Do you need some specific interpolation policy? You don't even say what is the colour depth of your image, so I chose anything...

Jerzy Karczmarczuk



 width=Sans virus.www.avast.com

Oier Arcelus

unread,
Aug 22, 2022, 10:29:32 AM8/22/22
to pyqtgraph
This does not cut it, I am trying to use pytqgraph for this task, as I am building this functionality as part of an GUI built in PyQt. My plan after this is to edit some of the events to customize the behavior of the images.

Thanks for the answer.

Patrick

unread,
Aug 22, 2022, 10:19:40 PM8/22/22
to pyqtgraph
Hi,

What you describe should actually be the default behaviour when an ImageItem is placed in a plot when autoscale is enabled. If you have other items in the plot then they will affect the plot limits when autoscale is active, in which case you may want to set the position and scaling of the image to better match your "real world" plot units. The easiest is using the setRect() method:https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/imageitem.html#pyqtgraph.ImageItem.setRect

If you'd like to restrict the plot zoom/pan limits so that the image always fills the view, then there are methods for the PlotItem (which get passed to the underlying ViewBox) to restrict scale, mouse interaction etc. Eg:


Patrick
Reply all
Reply to author
Forward
0 new messages