I have been trying to create a CanvasImage item in python using goocanvas however when I try to use the CanvasImage function it gives me an error.
pb = GdkPixbuf.Pixbuf.new_from_file_at_scale("image2.jpg", 1920, 1080, True)
image = GooCanvas.CanvasImage(pb, 200, 200)
TypeError: GObject.init() takes exactly 0 arguments (3 given)
Am I missing something or am I using a wrong function to create an image on the canvas.
I am referring to this as there is no proper documentation for goocanvas bindings in python: https://developer.gnome.org/goocanvas/stable/GooCanvasImage.html
If Goocanvas is not suitable for python, please suggest a different canvas that I could integrate and use in python.
Thank You
Perhaps it's the call.
This is from the goocanvas github : https://github.com/GNOME/goocanvas/blob/33694b33cc337be86d8c848825a463e52bb1a2c0/bindings/python/demo.py#L190
Also the project's github states that it is no longer supported.
An actively developed and better documented library is PyGObject.
https://pygobject.readthedocs.io/en/latest/index.html
To display images: https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Image.html#Gtk.Image
Related
I'm working on a little project on a Raspberry Pi, and playing with PygObject for the first time.
I'm trying to scale an image, and every example I find says I need to use a flag named INTERP_BILINEAR, but I can't find that anywhere within PygObject/Gtk. I've grep'ed the code base and can't seem to find any reference to INTERP_BILINEAR. I'm sure I'm missing something really obvious, but I don't know what.
pixbuf = GdkPixbuf.Pixbuf.new_from_file(random.choice(pics))
pixbuf = pixbuf.scale_simple(100, 100, <some_package>.INTERP_BILINEAR)
img = Gtk.image_new_from_pixbuf(pixbuf)
INTERP_BILINEAR is GdkPixbuf.InterpType.BILINEAR. I searched for INTERP_BILINEAR in the results of calling help("GdkPixbuf"). Often, Gtk and related modules use this type of formatting when referencing their objects.
I'm creating a python program that is supposed to streamline the process of setting up a computer. I want this python program to change the screen resolution of the computer and scaling of it. I'm not sure what the best approach is however, or how to approach it.
I've tried using an example pywin32 program, but it only outputted an array of resolution sizes
I had a look how to change screen resolution using C++ and then translated it to Python:
import win32api
import win32con
import pywintypes
devmode = pywintypes.DEVMODEType()
devmode.PelsWidth = 1366
devmode.PelsHeight = 768
devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettings(devmode, 0)
We needed a DEVMODE object to pass to the ChangeDisplaySettings function. The pywintypes module which is also part of pywin32 has a function to create objects of type DEVMODE.
We then set the PelsWidth and PelsHeight fields and also the Fields field to tell the API which field's values we want to use.
To change back to the previous resolution, simply call:
win32api.ChangeDisplaySettings(None, 0)
Thanks for asking the question. I've learned something.
I'm trying to implement Object Tracker using OpenCV and I'm new to Python. I'll call it from C# code via IronPython. What I'm trying to do, I want to set a custom rectangle as a parameter to Tracker instead of selecting it by mouse.
(Tracker code is the common example you can find on the internet)
Here is the problematic part :
This is how I set and create a rectangle
initBB = cv2.rectangle(frame ,(154, 278),(173,183), (0, 255, 00),1)
This is Tracker's init method
tracker.init(frame, initBB)
and this is the error
SystemError: new style getargs format but argument is not a tuple
If I wanted to use "normal" way, initBB set would be like
initBB = cv2.selectROI("Frame", frame, fromCenter=False,
showCrosshair=False)
I couldn't see which part I'm doing wrong, am I trying to set the wrong type of object to initBB or setting it in wrong way?
Thanks! Have a nice day!
Your error comes from a misunderstanding of what cv2.rectangle does.
It doesn't return a rectangle as you imagine. It is actually a drawing function. It draws the rectangle on the image you pass as argument and returns None.
A rectangle is just a tuple in Python with the following coordinates: (start_col, start_row, width, height). You can create it without using an OpenCV function.
I'm trying to open a vtk window using vtk_show, but my Ipython console crashes every time i do this, apparently this is because Ipython can't display an external window, which is exactly what vtk_show does. I searched on google for a solution, but it's written for python2 (i'm using python 3.6.3). Here's the solution i found:
import vtk
from IPython.display import Image
def vtk_show(renderer, width=400, height=300):
"""
Takes vtkRenderer instance and returns an IPython Image with the
rendering.
"""
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetOffScreenRendering(1)
renderWindow.AddRenderer(renderer)
renderWindow.SetSize(width, height)
renderWindow.Render()
windowToImageFilter = vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWindow)
windowToImageFilter.Update()
writer = vtk.vtkPNGWriter()
writer.SetWriteToMemory(1)
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.Write()
data = str(buffer(writer.GetResult()))
return Image(data)
I'm getting an error while trying to use the buffer built-in function of python2, but as this function doesn't exist on python3+ i'm stuck.. If anyone could help me with this i would be very appreciated. Thanks in advance!
At least these two points must be modified on your code to have the same behavior with Python 3:
The buffer(...) built-in function in Python 2 has been replaced by memoryview(...) in Python 3: What is Python buffer type for?. Replace the buffer call by memoryview
the str(...) built-in function has to replaced by a bytes(...) call to get a bytes object: https://docs.python.org/2/howto/pyporting.html#text-versus-binary-data
So the data = ... line should read:
data = bytes(memoryview(writer.GetResult()))
To clarify, I believe this example was an adaptation of a very informative blog example showing how to extract surfaces from medical images using VTK's marching cubes algorithm. The accompanying Jupyter notebook was intended for Python 2.7, and as mentioned for it to be used in Python 3.6+, the data=... portion needs to be changed.
import vtk
from IPython.display import Image
def vtk_show(renderer, width=400, height=300):
"""
Takes vtkRenderer instance and returns an IPython Image with the
rendering.
"""
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetOffScreenRendering(1)
renderWindow.AddRenderer(renderer)
renderWindow.SetSize(width, height)
renderWindow.Render()
windowToImageFilter = vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(renderWindow)
windowToImageFilter.Update()
writer = vtk.vtkPNGWriter()
writer.SetWriteToMemory(1)
writer.SetInputConnection(windowToImageFilter.GetOutputPort())
writer.Write()
data = memoryview(writer.GetResults()).tobytes()
return Image(data)
Credit for the solution definitely goes to #MafiaSkafia and #jcgiret, but I wanted to post a full and final solution.
I'm coding using Python and OpenCV on Ubuntu 14.04. When I click on the right button of the mouse, the associated mouse event cv2.EVENT_RBUTTONDOWN does not work and I rather get context menu ("actions"). Is there a way to disable the context menu popup?
A user gave me a hint and I am sure the solution is somewhere there. He asked me to add CV_GUI_NORMAL as shown on here.
So I run: cv2.namedWindow("Window",CV_GUI_NORMAL) but I got this error:
NameError: global name 'CV_GUI_NORMAL' is not defined
When I try cv2.CV_GUI_NORMAL as on the below user's comment instead, I get this error:
AttributeError: 'module' object has no attribute 'CV_GUI_NORMAL'
Note that similar question was asked here but I do not want to change OpenCV code.
How to fix this ?
.
You can use cv2.WINDOW_GUI_NORMAL as follows:
cv2.namedWindow('desired_name_of_window', flags= cv2.WINDOW_GUI_NORMAL)
Allowed windows values in cv2 are:
WINDOW_AUTOSIZE = 1
WINDOW_FREERATIO = 256
WINDOW_FULLSCREEN = 1
WINDOW_GUI_EXPANDED = 0
WINDOW_GUI_NORMAL = 16
WINDOW_KEEPRATIO = 0
WINDOW_NORMAL = 0
WINDOW_OPENGL = 4096
WND_PROP_ASPECT_RATIO = 2
WND_PROP_AUTOSIZE = 1
WND_PROP_FULLSCREEN = 0
WND_PROP_OPENGL = 3
WND_PROP_VISIBLE = 4
The official documentation says:
Python:
cv.NamedWindow(name, flags=CV_WINDOW_AUTOSIZE) → None
Parameters:
name – Name of the window in the window caption that may be used as a window identifier.
flags –
Flags of the window. The supported flags are:
WINDOW_NORMAL If this is set, the user can resize the window (no constraint).
WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image (see imshow() ), and you cannot change the window size manually.
WINDOW_OPENGL If this is set, the window will be created with OpenGL support.
Only some implementations with Qt backend support CV_GUI_NORMAL. It seems you have no choice than to install cv2 with Qt support or use other variables.
In that case you'll be using cv2.CV_WINDOW_NORMAL.
For a starter you could build without Qt support if you do not need it. It seems to do more harm than good in many cases. So it is better set the flag WINDOW_OPENGL: That way you disable the QT support and get the OpenGL one.