tkinter photoimage get image from another file - python

In my program, there any many images that are placed in a tkinter window, and i'm using the following code to place one image:
imgCapa = tk.PhotoImage(file='capa.png')
self.canvasTitulo.create_image(800,300,
image=imgCapa,
anchor='se')
The problem is that there are many images and i would like to place all of them inside a folder instead of placing inside the same folder of the python code, and read them from there.

just make a folder and add you path to file
imgCapa1 = tk.PhotoImage(file='my_folder/capa1.png')
imgCapa2 = tk.PhotoImage(file='my_folder/capa2.png')
self.canvasTitulo.create_image(800,300,
image=imgCapa1,
anchor='se')

Related

How do I display a PDF page in pygame without having to save a file in a folder in between?

I have created an app that runs with PyGame, which loads a page of a PDF, creates a Pixmap with getPixmap and saves into a jpg file. Then, the image is displayed with pygame.image.load().
The program works perfectly fine, but it requires to save a .jpg file in the folder, which substitutes and therefore deletes the previous .jpg file. When converting it into an exe and distributing the program, the users get a security message error, saying that the program does not have the permission to delete the .jpg file.
How could I skip the part of saving the photo in the folder, and directly showing from the PDF into pygame?
I think it can be done by creating a file-like object with Bitmap.IO(), but I don't know how to use it. I'm relatively new in python, so I would appreciate if you could write the lines of code you would use.
zoom = 2
doc = fitz.open(pdf_path)
mat = fitz.Matrix(zoom, zoom)
page = doc.loadPage(photo_num)
pix = page.getPixmap(matrix = mat)
photo_output = "photo.jpg"
pix.writePNG(photo_output)
load_photo = pygame.image.load(photo_output)

Opening an image with its original file name on display using Python?

I have a chart function that saves the end figure as a file. After I run the function, I also want it to display the figure at the end. So, I use this:
from PIL import Image
filepath = 'image.png'
img = Image.open(filepath)
img.show()
It works just fine, but when the file opens, it opens with a random file name, not the actual file name.
This can get troublesome as I have a lot of different chart functions that work in a similar fashion, so having logical names is a plus.
Is there a way I can open an image file with Python and have it display it's original file name?
EDIT
I'm using Windows, btw.
EDIT2
Updated the example with code that shows the same behaviour.
Instead of PIL you could use this:-
import os
filepath = "path"
os.startfile(filepath)
Using this method will open the file using system editor.
Or with PIL,
import Tkinter as tk
from PIL import Image, ImageTk # Place this at the end (to avoid any conflicts/errors)
window = tk.Tk()
#window.geometry("500x500") # (optional)
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()
The function img.show() opens a Windows utility to display the image. The image is first written to a temporary file before it is displayed. Here is the section from the PIL docs.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show
Image.show(title=None, command=None)[source] Displays this image. This
method is mainly intended for debugging purposes.
This method calls PIL.ImageShow.show() internally. You can use
PIL.ImageShow.register() to override its default behaviour.
The image is first saved to a temporary file. By default, it will be
in PNG format.
On Unix, the image is then opened using the display, eog or xv
utility, depending on which one can be found.
On macOS, the image is opened with the native Preview application.
On Windows, the image is opened with the standard PNG display utility.
Parameters title – Optional title to use for the image window, where
possible.
"
The issue is that PIL uses a quick-and-dirty method for showing your image, and it's not intended for serious application use.

Python Pillow Image.show() Path Issue?

Honest warning—I'm a total beginner.
I'm using Python 3.8.2 in IDLE and sometimes in Spyder.
My goal: to open an image (located in a folder) in Preview using Pillow
My code:
from PIL import Image
my_image = Image.open("flower.jpg")
my_image.show(r"/Users/User/Desktop/flower.jpg")
I run this, and it works! But it only works if the jpg is on the Desktop. I want to put the image in a folder. So I changed my last line of code to something like this:
my_image.show(r"/Users/User/Desktop/folder/flower.jpg")
I put the image in the folder, run the program, and get this error:
**FileNotFoundError: [Errno 2] No such file or directory: 'flower.jpg'**
Oddly, if I put the jpg back onto the Desktop and still use the path with "folder" in it, the program runs successfully and shows me the image.
Is this a problem with the path? How can I use Image.show() to open an image that is located somewhere other than the Desktop?
Thank you!
If you want to open and view an image using an absolute path, you'll have to change this line:
my_image = Image.open(path_to_image_dir)
You've incorrectly passed a string to PIL.Image.show. This doesn't throw an error, since PIL.Image.show happens to take an optional string parameter title, which it will use as a title in the image window. Don't pass any parameters to show, and change "flower.jpg" in the line above to the actual path.

Python doesn't recognize jpg image file/directory

I can't get this image to be displayed in a tkinter gui because it doesn't recognize the image file or directory, here is my code:
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
path = "krebs.jpg"
img = ImageTk.PhotoImage(Image.open(path))
img.pack()
root.mainloop()
and here is the picture in question: Krebs security pic
I bet your current working directory isn't what you think it is. Try:
import os
os.getcwd()
Make sure that directory (printed by os.getcwd()) has your image in it (i.e. go to that folder and check to see if your image is there). If not, specify the absolute path to your image:
path = C:/Users/<user_name>/... .../krebs.jpg
or whatever the full path to the image is on your OS.
Alternatively, you could move the image to the folder indicated by os.getcwd().
Sometimes I make a typo when defining paths in python, because very simple IDEs (like IDLE) don't have in-line auto-complete. Here's a trick I use all the time to make sure my absolute path doesn't have a typo:
import tkinter.filedialog as fd
path = fd.askopenfilename()
It will open a familiar file opening dialog. Use the file dialog window to find your file, then click "Open", and you'll have the full path saved to variable "path".
Well I believe there is a simpler solution and one that is better overall. Python can use . to tell the code that the file you are looking for is in your same directory as your python code. This way you do not need to select a file manually or use a complete file path name.
2nd I noticed you are trying to pack() an image into the window however you cannot do this. Instead add it to a label or canvas. Just make sure your image file is in the same folder as your .py file.
Try this.
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
path = "./krebs.jpg"
img = ImageTk.PhotoImage(Image.open(path))
lbl = tk.Label(root, image=img)
lbl.pack()
root.mainloop()
I used a simple small red square image I use for testing but here is the results:

Using Python and OpenCv to sequentially display all the images in a directory

I'm trying to use OpenCV's python bindings to sequentially display all of the images in a directory.
This code prints all the files in my directory but only displays the first image it opens:
for file in os.listdir(path):
print(file)
image = cv2.imread(os.path.join(my_dir, 'attachments',
file))
cv2.imshow("Image", image)
cv2.waitKey(550)
What do I need to change so that every image in my directory is displayed by cv2.imshow? My print statement shows me that I am looping through all of my images but imshow is not being redrawn through any but the first iteration of my loop.
EDIT:
If you want to see all your images in different windows:
The other thing that occurred to me is that you may want to give each of your cv2.imshow() commands a different name other than "Image". With different names, you can have multiple imshow windows. So you could do:
for indx, f in enumerate(os.listdir(path), start=1):
image = cv2.imread(os.path.join(my_dir, 'attachments', f))
cv2.imshow("Image-{}".format(indx), image)
cv2.waitKey(600)
EDIT 2:
I went back to double check my answer and realized that your code seems to work just fine for me... I'm using opencv '3.0.0'

Categories