Display an image with Python on Linux Angstrom - python

I have a Python script that displays images fullscreen on a BeagleBoard with the GUI disabled. The script is started when the board boots. For this I use PyGame which works perfectly fine. Except for some reason the image qwality is scaled down. Because the images are stored in HQ I assume that PyGame resamples the image. I was unable to find out where this can be changed so I decided to replace PyGame, it also seems a bit much to "just" display an image.
I have the code below to display the image. According to documentation the default image viewer will show the image. (Which is supposed to be XV). But as soon as I run the code below where image is a filepath I get "sh: xv: not found".
from PIL import Image
im = Image.open(image)
im.show()
So I tried to install the XV package but can't find how to install it for Angstrom.
My question could either be "How to display images fullscreen with Python?" (To which the answer was supposed to be the code above). Or the question is "How can I get XV installed on Angstrom?" (What is the package name for opkg install)
I did search, but haven't found something that works...

Image.show() in PIL is more intended for debugging than actual, production use. It is hardcoded to call xv <temp-image-file-pil-creates>. You can hack around this (make a symbolic link called xv that will call some other image viewer), but it's still a rather bad way to go about it.
I don't know enough about the BeagleBoard to tell you the best/canonical way to display an image fullscreen, but if you got halfway there with PyGame, perhaps you can post your code and the community can help you fix the quality problem.
If the image is getting downscaled to fit the screen, you might look into using transform.smoothscale to scale the image manually (to avoid losing quality).

Related

Image from web not displaying pyqt5 but local works fine

I am attempting to add a background image to a label in pyqt5 using the background image.
label.setStyleSheet("background-image : url(https://www.tutorialkart.com/sample_image.jpg);")
but for some reason it won't load any images from web, and instead I get this error:
Could not create pixmap from https:\\www.tutorialkart.com\sample_image_small.jpg
I have tried different image types and the only thing that I tried that got it to work was using local files from my computer, which won't be a good fix for what I need it for.
Then I tried using the python Pixmap and setting it through the python code directly, but there was the same issue.
Anyone know a potential fix for this?

Python image recognition with pyautogui

When I try to recognize an image with pyautogui it just says: None
import pyautogui
s = pyautogui.locateOnScreen('Dark.png')
print s
When I ran this code the picture was on my screen but it still failed.
Pyautogui.locateOnScreen has a parameter that specifies the 'confidence' you have in the image you enter.
This way, pyautogui will deal with slight pixel deviations.
For example:
import pyautogui
s = pyautogui.locateOnScreen('Dark.png', confidence=0.9)
print(s)
For more information, see https://buildmedia.readthedocs.org/media/pdf/pyautogui/latest/pyautogui.pdf.
It's pixel perfect.
It can't find the image if it is not 100% match.
For example, I cropped an area with an Opera extension. Then I ran my script with Firefox, and pyautogui did not recognize it.
Don't let your image get resized or compressed by screen capture software or extensions.
Use the same window/screen (size, resolution) as where you saved your screenshot.
On my system, I get this if the picture is on a second monitor. If I move it to the main screen, the image is located successfully.
It looks like multiple-monitor functionality is not yet implemented:
From http://pyautogui.readthedocs.org/en/latest/roadmap.html
Future features planned (specific versions not planned yet):
Find a list of all windows and their captions.
Click coordinates relative to a window, instead of the entire screen.
Make it easier to work on systems with multiple monitors.
...

pyautogui.locateCenterOnScreen() returns None instead of coordinates

import pyautogui
print (pyautogui.locateCenterOnScreen("C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"))
Instead of returning coordinates, it returns None.
My problem is Solved when I took screenshot by pyautogui inbuilt function rather than taking WIN+Printscr because if we took screenshot by WIN+Printscr then pixel density and other image related data may be different in comparison to pyautogui inbuilt function.
Maybe this thing worked for you, for me it worked.
For Ex - wifi.png so first I took full screenshot and I cropped it from that full image then I put this in my code shown below
import pyautogui
print(pyautogui.locateCenterOnScreen('wifi.png'))
Seems like it couldn't find anything matching your image on the screen.
locateCenterOnScreen(image, grayscale=False) - Returns (x, y) coordinates of the center of the first found instance of the image on the screen. Returns None if not found on the screen.
The initial problem is quite simple - the library does not find the image passed represented on the screen and therefore returns None rather than the co-ordinates as it says it will in the docs.
However, there is a possible misunderstanding here, in particular from a user who posted a bounty on the question and posed a similar question here.. A comment was made
"The pictures are on my desktop"
When you use this function, you pass in a filename as a string. The library then loads the image file and looks for the picture on screen (not the filename). pyautogui.locatecentreonscreen() will look for the actual image if it is visible on the screen. It does not look for files on the desktop, or file icons with the same name as the image passed to it.
Example
Say you have a file with the name flower.jpg containing the following image, saved on your desktop.
With no other windows open, run:
coords = pyautogui.locateCenterOnScreen('C:\\Richard\\Users\\flower.jpg')
print(coords)
The result is None
This is because that image is not displayed on my screen even though an icon is on the desktop, with the name flower.jpg. This is true even if that icon is a small scale version of the flower.
However, if I leave the image visible (as I'm preparing this post) and do the same thing, I get co-ordinates - e.g.:
As you see - because the actual image is on the screen, the library finds it, with co-ordinates 524,621
In summary if the library doesn't find the image displayed to the user on the screen, it will return None. Note the image has to be visible to the user at the point at which the code is running. It won't find the icon on your desktop, or similar, or the image in a window that is "hidden" behind another. Is that what you're trying to do?
Are you sure that the image is of the same size as of the icon?
If not pyautogui.locateCenterOnScreen() will raise TypeError: 'NoneType' object is not iterable
Also make sure that the full icon is visible and looks the same as the image:"C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"
Hope the problem is solved!
Building off of what Don Kirby said, no matching image was found on the screen. You could open the image in, for example, Windows Photo Gallery, (or Tk) and then pyautogui would find it.
Good explanation, is there any library that work better than pyautogui? I mean it wants excatly the same picture on the screen. We need similar sometimes. – GLHF May 11 '16 at 15:45
Try using this code line:
pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
I believe confidence range from 0.1-0.9.
Unless you have several pictures looking almost alike, this might solve the exception.
If that doesn't work try making a second screenshot with more/less of the original image and write this code:
try:
pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
except TypeError:
pyautogui.locateCenterOnScreen("yourscreenshot2.PNG", confidence=0.9)
This will give it a second try with a slightly different picture, and hopefully not return a TypeError.
If you can't use pyautogui.locateCenterOnScreen() because of image problem , try using the snipping tool (if you are on Windows) to take screenshots.It works.
Also make sure that you have downloaded the "Pillow" module
Try this :
pip install opencv-contrib-python
It confused me a lot that I ran the same code:
coords =pyautogui.locateCenterOnScreen('C:\\test.jpg')
in two different virtual environment( X and Y, almost same) returned None and Point(x=1543, y=461).
I read Aleks's answer and guess it use the parameter confidence implicitly when opencv-contrib-python in current environment(which Y had but X hadn't).
I didn't dig in but just installed opencv-contrib-python in virtual environment X and solved my problem.

Unable to show an image using python PIL Image.show

I'm using the Python Imaging Library and I am unable to open an image successfully in Windows Live Photo Gallery. There is a message that shows up saying "There are no photos or videos selected" instead of the image.
This is what I've tried:
import Image
img = Image.open(r"C:\Users\User\Pictures\image.jpg")
img.show()
This is pretty much the same as in the PIL handbook tutorial, so I'm not sure where I'm going wrong.
The documentation says:
On Windows, it [show()] saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.
Problem is that your program exits immediately somehow, the temporary file is deleted upon exit and Windows etc. cannot find it. As a temporary solution, try adding:
import time
# Your code as above
time.sleep(30)
This will make the program wait 30 seconds before exiting. If you prefer, you could make it wait the user to press a key.
EDIT: it seems like you are experiencing problems with temporary files. As a workaround, save the image somewhere on the disk using, say, img.save("C:\Users\User\Pictures\test.jpg") and open it with your favorite image viewer. Whenever you want to show the processed image, call save again and reload the picture in the image viewer.

Python: OSX Library for fast full screen jpg/png display

Frustrated by lack of a simple ACDSee equivalent for OS X, I'm looking to hack one up for myself. I'm looking for a gui library that accommodates:
Full screen image display
High quality image fit-to-screen (for display)
Low memory usage
Fast display
Reasonable learning curve (the simpler the better)
Looks like there are several choices, so which is the best? Here are some I've run across:
PyOpenGL
PyGame
PyQT
wxpython
I don't have any particular experience with any of these, nor any strong desire to become an expert - I'm looking for the simplest solution.
What do you recommend?
[Update]
For those not familiar with ACDSee, here's what it does that I care about:
Simple list/thubmnail display of images in a directory
Sort by name/size/type
Ability to view images full screen
Single-key delete while viewing full screen
Move to next/previous image while viewing full screen
Ability to select a group of images for:
move to / copy to directory
delete
resize
ACDSee has a bunch of niceties as well, such as remembering directories you've moved images to in the past, remembering your resize settings, displaying the total size of the images you've selected, etc.
I've tried most of the options I could find (including Xee) and none of them quite get there. Please keep in mind that this is a programming/library question, not a criticism of any of the existing tools.
I will recommend using wxPython to create such a viewer, wxPython is easy to learn, free, cross platform and blends well in OSX. Even if you want to use pyopengl, wxPython would be good with pyopengl.
see such tutorials http://showmedo.com/videotutorials/video?name=1790000&fromSeriesID=179
and there is already cornice written in wxpython/PIL, may be you can modify that. It has been inspired by the famous Windows-only ACDSee :)
it's not an answer to your coding question but for (a big part of) the lack of ACDsee equivalent (requires OSX 10.5+):
Simple list/thubmnail display of images in a directory: Finder.app
Sort by name/size/type: Finder.app will do name & type, not image size (but does file size)
Ability to view images full screen: quick preview (spacebar / eye icon)
Single-key delete while viewing full screen: command-backspace while viewing in quickpreview, both windowed and fullscreen
Move to next/previous image while viewing full screen: both quickprewiew (after selecting a group of images or whole directory with cmd-a) and Preview.app
Ability to select a group of images for[...]: Finder.app will does all but resize
seems like you have everything except resize just pressing the spacebar while in finder.
Preview.app will resize both a single image or multiple ones in one batch.
Use an App like Picasa (now available on mac). Use AppleScript through Python to control it from your application.
Failing that, use PyObjC to create Cocoa image display component and dialogs, and so on.
I ended up using PyGame, has been pretty good so far.

Categories

Resources