I just want to know the syntax. How do I load an image using pygame.image.load() ?
Let's take an example, I want to load an image called cat.png - and type this
pygame.image.load('cat.png')
Then, where should the image cat.png be saved?
By default, Python only searches in your current directory for files, however, if you wish to load images from a separate directory, you may find this useful:
Importing images from a directory (Python)
For the syntax, I reccomend you refer to the documentation, here:
https://www.pygame.org/docs/ref/image.html#pygame.image.load
pygame.image.load()¶
load new image from a file
load(filename) -> Surface
load(fileobj, namehint="") -> Surface
Load an image from a file source. You can pass either a filename or a Python file-like object.
Any images loaded directly with
pygame.image.load('Image Name')
should be saved in the same folder as the python script that is using the image
Related
I am wanting to compress an image I am receiving from a user before I send it off to an S3 bucket to be stored. Unfortunately for me, I haven't had to deal with images a lot. The image comes over as a FileStorage object and this is where my issue starts. I'm trying to use the PIL library to compress the image using the optimize option when you save your file using PIL. The only issue is I don't have a proper path to save this file. I tried to fake it out to no avail. Is there a proper way to generate a temporary directory/path to save the image just so I can compress it before I send it to the S3 bucket?
Here is the current simple code
file = request.files['filepond']
# file = Image.open(file)
# file = file.save('TestDirectory', file.format, optimize=True, quality=30)
print(file.filename)
print(file.content_type)
Thanks all!
I am writing a small python package for interactive data visualisation. We would like to include gifs with the package so users can run an example and learn how to interact with the figure.
Here is an example structure:
<package>
+-->some_module/
+--> static/
+--> foo.gif
+--> spam.py
+-->examples/
+--> example.ipynb
We have several classes (e.g., spam.py may contain the class Spam), and these classes will have a method .show_gif() which will access the gif (foo.gif) within static and show it to the user.
The code is wrote to be used within a Jupyter notebook. In the package we include some examples (e.g., example.ipynb), these will import spam and then call the .show_gif() method on the relevant class.
Currently we display the gifs using:
from IPython.core.display import Image
Image(filename=path_to_gif)
which works fine when the gif is in a sub-directory of the folder the jupyter notebook is in, but not when the gif is within a sibling directory of the package (as in the above example).
EDIT:
I believe I can access the .gif but cannot display it in a jupyter notebook (see below)
There are similar stack overflow questions (This question) which suggest using importlib.resources module from the standard library. However, they return a BinaryIO instance I don't know how to deal with see:
import some_module.static as static
…
def show_gif(self):
image = pkg_resources.open_binary(static, 'foo.gif')
return Image(data=image)
this throws an error because I am giving Image a wrong type for data. How can I open this .gif and display it in a jupyter notebook?
Use any method to find the path to your gif file and open it for reading; pkg_resources.open_binary() will do, though as this is an actual file on disk there are simpler methods. But now you have an open filehandle, not the content itself-- same as when you call open() on a file. To get the image data, you need to read() from the filehandle.
You then have a bytes object, not a text string. That's not a problem since Image() accepts bytes in the constructor:
with pkg_resources.open_binary(some_module, 'foo.gif') as src:
imagedata = src.read()
image = Image(data=imagedata)
I have a folder for image data set , and I wanna load this folder to be a training data set using python this image of my folder and it`s images
If there's only one directory then you can use os.listdir() or if you want to lookup recursively then look up os.walk().
I'm currently working on a Qt program that works with images that are supplied by the users. One problem I've run into is that a lot of images are saved with the wrong extension - e.g. an image is saved in JPG format but has a PNG extension.
Qt doesn't seem to deal well with this. When I load an image like this into a QImage, the QImage fails to load. I've been looking through the docs, but I haven't come across anything relating to this.
Other programs I've used are able to correctly identify the image as a JPG despite the PNG extension, so there should be no reason for Qt to be unable to do this, but I'm not having any luck.
Any suggestions?
I solved this by using a QImageReader. An example is shown below using PySide.
First I created an instance of QImageReader and set it to read the format from the content.
image_reader = QtGui.QImageReader()
image_reader.setDecideFromContent(True)
This setting tells the reader to only look at the image's data to determine its format and not the extension.
Then I set the filename to the filename of the image I wanted to load and called read().
image_reader.setFileName(file_path_here)
image = image_reader.read()
Read returns a QImage object, so I proceeded with the rest of my code from there.
I am using sikuli to load IE and from that i am opening the .flv records from adobe connect.In that i have capture images dynamically and have to store that images in a list object or any data structure. Is it possible to capture images from a window dynamically in sikuli?
Please help me.
Thanks.
def CaptureScreen(path):
#Take Screen shot and move it from temp folder to new location <path>
shutil.move(capture(Screen()), path)
http://doc.sikuli.org/screen.html#Screen.capture
http://www.jython.org/docs/library/shutil.html