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.
Related
I am creating a project with python and Raspberry Pi. I am trying to use my Webcam, as I, unfortunately burned my Camera Module. I was following along: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/image_processing/
Everything is working fine, except for one problem. I am not able to save the image file that is captured. I would like to take the photo I have created and turn it into a .jpg image. Code I have currently:
from imgproc import *
import cv2
# open the webcam
my_camera = Camera(320, 240)
# grab an image from the camera
my_image = my_camera.grabImage()
# open a view, setting the view to the size of the captured image
my_view = Viewer(my_image.width, my_image.height, "ETSBot Look")
# display the image on the screen
my_view.displayImage(my_image)
# wait for 5 seconds, so we can see the image
waitTime(0)
Can someone please help me with this problem?
Thanks in advance!
-Saurish Srivastava
Custom Library: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/downloads/
UPDATE: It does not have to just use this type of code. You can give me an example with a different software. Just tell me how to use it properly so I don't mess up.
Adding the following in your code should save the image in the array my_image as picture.jpg
cv2.imwrite('picture.jpg', my_image)
For details on configuring raspberry pi-http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/
Here is a bit of my code:
from PIL import Image
image = Image.open('fall-foliage-1740841_640.jpg')
image.show()
The error is when the default photo viewer is started and shows the error
"It looks like the image was moved or renamed"
Restarting doesn't help. I am just starting using PIL and can't find a way round this.
I appreciate any help. Thanks!!!
You can just add a prompt to keep the python script from closing.
from PIL import Image
image = Image.open('fall-foliage-1740841_640.jpg')
image.show()
input()
Essentially, The problem is that when the image is opened, it's stored in temporary memory. So when the program closes, the image is not saved and is lost from memory. There for when the Photos app or whatever app you are using to view the image, searches for the image, it's already gone when the script is finished executing.
Hope this helps
I'm sending a stream of JPEG images from a Raspberry Pi to my MBP via a simple socket programme in Python 2.7.
When I read the image from the stream on my MBP, it opens up in Preview and opens a new Preview window for every separate image. I have an fps of about 2/3 and obviously 2/3 new windows per second is impossible to work with.
How can I go about only opening one Preview window and simply overwriting the displayed image? Would OpenCV be the best way to go? If so I am unsure how to.
Here is how I read the stream and display the images:
image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
if not image_len:
break
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
image = Image.open(image_stream)
image.show()
OS X Preview seems to automatically reload open images at intervals (always when the window receives focus), but Image.show saves a new temporary file each time you use it. I suggest saving each new frame to the same file and then using subprocess.call with the OS X open command.
This being said, the documentation notes that Image.show is primarily for debugging purposes. For a video with more than a few FPS, you probably want something else. One solution would be an HTML interface with WebSockets, perhaps using something like AutoBahn.
After many hours of searching online and in my python book I can't seem to find the answer to my question which is what do I add to my code so I can put in a timer that automatically closes the photo? It pulls itself up but then I have to manually close the photo to get back to my main program. Any help would be appreciated.
from PIL import Image
img = Image.open('battleship load screen.png')
img.show()
This is not possible using PIL alone - img.show() is just launching another program, it's intended for debugging really, not for presenting things to the user.
From the docs.
Displays an image. This method is mainly intended for debugging
purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it.
This method returns None.
If you want to display an image and have control over it, use a graphical toolkit and construct a UI for your purpose. I've linked there to an example using PySide, a set of QT bindings, but of course you could use any toolkit - each will be different.
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).