I am writing an open configurable taskbar using PyQt4, and I want to add a start orb. I want the orb to support the common image format of start orbs, which is usually three 54x54 squares for a 54x162 image.
My program takes those images, cuts them in thirds using PIL, and I try to load them using .setIcon(QIcon(icon)). Unfortunately, this isn't working. The image is loaded, calling .show() displays the image perfectly.
For various reasons, I did not choose style sheets, including that I would have trouble loading the cut images. Instead, (as the code shows) I had the QPushButton change icons on hover, press, and release. None of them work. Not even the default image.
My code is on this github. Note the warning in the readme! The program quite literally replaces (and removes) the taskbar (but you can get it back).
tl;dr
I have the code (see link below). It won't load using .setIcon(), and I don't know why. I thought it would work, used ImageQt, but no.
EDIT:Unfortunately, going from a PIL object to a Imagqt object to a QImage didn't solve the problem.
EDIT2: as #Trilarion requeted here is a link to a gist of the slimmed down code: https://gist.github.com/IronManMark20/85b00104c9bb52b78add
Related
I have an svgwrite.Drawing into which I wish to insert a JPG image as the background image. Here's the code to what I have so far. It is using computer vision for a machine learning task. The essential part to help me begin understanding how I can insert an image, is on line 147 where I create a drawing using svgwrite. The problem I'm running into is that in the documentation, if you scroll down to where the factory methods are, it says that you can use Drawing.image and set an href for an image that you want to use. I did further down notice that in the factory methods it also has Drawing.saveas in the form svg_canvas.saveas, which does end up working for me so I know that I am using this Drawing method to create an svgwrite.image.Image object correctly, but it doesn't seem to be working. For reference, I insert the following code right after this line:
svg_canvas.image(href="/home/mendel/project/bg/bg.jpg", size = src_size)
I triple checked that my href was correctly written out and that my bg image was in fact in the folder. src_size is already defined before as well so I'm pretty sure it's all working. But I'm not sure why the above line isn't working for me at all to give me a background image for the canvas. Please let me know if I'm using it incorrectly I really appreciate it!
I already tried the above solutions using the Python API but it isn't working. I also tried looking into other stack posts, none have worked for me so far.
I have an extremely simple application for a Raspberry Pi. (It's an educational kiosk for a children's museum if anyone cares.) In python, I have an infinite loop in a thread reading a line from a serial port. Based on the input, I display one of 14 different jpg images. I am not putting all the code here, but it's a very bare-bones GDK application. I have an Arduino feeding the serial port the information to cycle through all the images for debug purposes. In response to the input, I do the following:
self.CurrentImage.set_from_file("image.jpg") # the name here is one of 14
Not to anyone's great surprise, this works. But as I let the Arduino hammer at the input, the screen would randomly show a white image and nothing again after. I checked the standard out window and the data was still coming and the images still being read. And when I say random I mean that at some point in the input-and-display process, it stops displaying. There are no errors being reported. Sometimes I might get 4-5 images in sequence before it dies, or I might make it through the list twice. It's simply not deterministic. My mind wandered to thinking maybe I'm not clearing first and having a memory leak. I made the following amendment:
self.CurrentImage.clear()
self.CurrentImage.set_from_file("image.jpg")
The problem persisted. I decided to scrap the method and go for something that didn't involve reloading images. At startup I created a separate GTK Image widget for each file. Then in response to the input data, I did this:
self.CurrentImage.hide()
self.CurrentImage = self.AlphaImage # or one of the other 13 Images I created
self.CurrentImage.show()
The nice thing about this method is that the image displays much faster. The first method had the screen briefly go white as the image was loaded. However, once again, after a random number of image switches, the window goes white. Diagnostic output shows that the loop is happily reading data and selecting images.
In the original version where I loaded images as needed, there was exactly one widget on the window. So it's not possible that another widget is covering it. The second version has an Image widget for each jpg file. If one is covering another, I should still at least see that image.
I'm good at thinking outside the box, but I admit that Linux is a weak area for me. Nothing is occurring to me to try to make this work. I'd whinge that I'm under time pressure here and children will be disappointed... but it was supposed to be done before Christmas and I only got the final art yesterday. That reminds me that there's one final note and the reason I thought my first method was failing: I created temporary graphics of my own that was one word of black text on a white background. Those images displayed without problem until the screensaver kicked in.
I'm open to any suggestion as to how to track this down and fix it.
Thanks to Sylvester, I figured this one out. The problem isn't how I was updating the images, it was where I was doing it. In the thread catching the serial input was not the place to do it. I reduced the thread to simply reading the line, then did the following:
GLib.idle_add( self.updateImage, lineInput )
then in the function updateImage I did the business logic of selecting the correct image and updating. Problem solved.
I wanted to use Python to create animations (video) containing text and simple moving geometric objects (lines, rectangles, circles and so on).
In the book titled "Python 2.6 Graphics Cookbook" I found examples using Tkinter library. First, it looked like what I need. I was able to create simple animation but then I realized that in the end I want to have a file containing my animation (in gif or mp4 format). However, what I have, is an application with GUI running on my computer and showing me my animation.
Is there a simple way to save the animation that I see in my GUI in a file?
There is no simple way.
The question Programmatically generate video or animated GIF in Python? has answers related strictly to creating these files with python (ie: it doesn't mention tkinter).
The question How can I convert canvas content to an image? has answers related to saving the canvas as an image
You might be able to take the best answers from those two questions and combine them into a single program.
I've accomplished this before, but not in a particularly pretty way.
Tl;dr save your canvas as an image at each step of the iteration, use external tools to convert from image to gif
This won't require any external dependencies or new packages except having imagemagick already installed on your machine
Save the image
I assume that you're using a Tkinter canvas object. If you're posting actual images to the tk widgets, it will probably be much easier to save them; the tk canvas doesn't have a built-in save function except as postcript. Postscript might actually be fine for making the animation, but otherwise you can
Concurrently draw in PIL and save the PIL image https://www.daniweb.com/software-development/python/code/216929/saving-a-tkinter-canvas-drawing-python
Take a screenshot at every step, maybe using imagegrab http://effbot.org/imagingbook/imagegrab.htm
Converting the images to to an animation
Once the images are saved, I used imagemagick to dump them into either a gif, or into a mpg. You can run the command right from python using How to run imagemagick in the background from python or something similar. It also means that the process is implictely run on a separate thread, so it won't halt your program while it happens. You can query the file to find out when the process is done.
The command
convert ../location/*.ps -quality 100 ../location/animation.gif
should do the trick.
Quirks:
There are some small details, and the process isn't perfect. Imagemagick reads files in order, so you'll need to save the files so that alphabetical and chronological line up. Beware that the name
name9.ps
Is alphabetically greater than
name10.ps
From imagemagick's point of view.
If you don't have imagemagick, you can download it easily (its a super useful command-line tool to have) on linux and mac, and cygwin comes with it on windows. If you're worried about portability... well... PIL isn't standard either
There is a way of doing that, with the "recording screen method", this was explained in other question: "how can you record your screen in a gif?".
Click the link -->LICEcap : https://github.com/lepht/licecap
They say that it's free software for Mac (OS X) and Windows
You could look at Panda3D, but it could be a little over killed for what you need.
I would say you can use Blender3d too but i'm not really sure of how it works. Someone more experimented then me could tell you more about this.
I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.
I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.
The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.
What I need is the code method I would use to search for the image on screen and send back the cords to a variable.
Image explanation/example:
Reference image of rifle:
PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html
Edit:
I assume you are using windows, as your example image looks like window. In this case you can use:
from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)
then use opencv to process the image to find sub image you are looking for.
If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532
Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you.
You have to just go to this webpage and read the locate function topic completely
and you'll be able to solve your problem.
I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.
Is it possible to take screenshots of a running program (with GUI) from another python program ?
If so, what could be the steps and libraries that I could use ? (On Windows)
For example, let's say I have calc.exe running. I'd want to take screenshots of what is displayed to the user from myprogram.py.
My goal is to analyze what's displayed on the monitored program.
If it's not possible to isolate the screenshot to a running predefined program, I think I will have to take screenshots of the fullscreen but it's not very practical.
Capturing an screenshot is easy. Just install the Python Imaging Library and use the ImageGrab.grab() function to return an Image instance with the screenshot.
Capturing an specified window is a little more complicated, because you need the window coordinates. I recommend you to install the win32api modules and use a little module called winGuiAuto.py. Once you do that, you can do something like this:
hwnd = winGuiAuto.findTopWindow(title)
rect = win32gui.GetWindowPlacement(hwnd)[-1]
image = ImageGrab.grab(rect)
However, capturing the screen is the easy part. If you want to analyze the contents from screenshots, you're in for a lot of complications. This is probably the wrong approach for doing what you want and should be left as a last resort.
In most cases, it's easier to use the windows api to read the contents of a window's elements directly, but that won't work with some 3rd party GUI toolkits. That's not within the scope of your question so I'm not detailing it here, but you should read the source of the winGuiAuto.py module mentioned above for examples on how to do that, as well as checking the pywinauto library.
The ImageGrab Module, works on Windows only. The pyscreenshot module, is a better replacement for that, can be used to copy the contents of the screen to a PIL or Pillow image memory. Read more at link below.
https://pypi.python.org/pypi/pyscreenshot