I am converting a pdf to a bunch of images and displaying it to a tkinter window, but after the pdf conversion I get this error from Tkinter:
Traceback (most recent call last):
File "C:\Users\karee\PycharmProjects\Workspace\pdf_viewer.py", line 25, in <module>
pdf.image_create(END, image=p)
File "C:\Users\karee\AppData\Local\Programs\Python\Python39-32\lib\tkinter\__init__.py", line 3728, in image_create
return self.tk.call(
_tkinter.TclError: image "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1700x2200 at 0x5386C10>" doesn't exist
Here is my code:
from tkinter import *
from PIL import Image, ImageTk
from PyPDF2 import PdfFileWriter, PdfFileReader
import pdf2image
root = Tk()
pdf1 = 'pdf_path.pdf'
pil_images = []
inputpdf = PdfFileReader(open(pdf1, "rb"))
pdf_frame = Frame(root).pack(fill=BOTH,expand=1)
scrol_y = Scrollbar(pdf_frame, orient=VERTICAL)
pdf = Text(pdf_frame, yscrollcommand=scrol_y.set, bg="grey")
scrol_y.pack(side=RIGHT, fill=Y)
scrol_y.config(command=pdf.yview)
maxPages = inputpdf.numPages
for page in range(1, maxPages, 100):
pil_images.append(pdf2image.convert_from_path(pdf1, dpi=200, first_page=page,
last_page=min(page + 100 - 1, maxPages), fmt='jpg',
thread_count=1, userpw=None,
use_cropbox=False, strict=False, poppler_path="poppler-21.03.0/Library/bin"))
for photo in pil_images:
for p in photo:
pdf.image_create(END, image=p)
pdf.insert(END, '\n\n')
mainloop()
When I debugged this program, there were PIL images so I'm not sure what's causing these errors. Any help would be greatly appreciated!
Related
I know that in CSS you can set the maximum size of an image by using max-width and max-height. I want to do the same thing with tkinter. I've already tried using Image.open("/path/to/file").resize(500), but I got the error TypeError: 'int' object is not iterable. Here is my code:
from tkinter import *
from PIL import Image, ImageTk
root=Tk()
current_image=0
images=[ImageTk.PhotoImage(Image.open("/users/27cadem/documents/display.png").resize(500))]
panel=Label(root,image=images[current_image])
panel.pack()
root.mainloop()
The code can be something like this:
from tkinter import Tk, Label
from PIL import Image, ImageTk
root = Tk()
file = 'img-path.png'
image = Image.open(file)
max_width = 500
pixels_x, pixels_y = tuple([int(max_width/image.size[0] * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((pixels_x, pixels_y)))
label = Label(root, image=img)
label.image = img
label.pack()
root.mainloop()
I am able to get the image to display in the GUI with Tkinter however I can not get this part of the code to work as I am trying to keep it all either under one .py file because it is easier for me to keep everything in one singular window.
The error I am getting is:
Traceback (most recent call last):
File "C:/Users/Holcomb Family/PycharmProjects/webscrapeTKINTER/venv/main4.py", line 32, in <module>
run = MainWin(root)
pil_img = Image.open(my_picture)
AttributeError: type object 'Image' has no attribute 'open'
My code:
import io
from PIL import Image, ImageTk
from tkinter import *
from urllib.request import urlopen
root = tk.Tk()
root.title("New Window")
class MainWin:
def __init__(self, master):
self.master = master
url1 = "https://img.finalfantasyxiv.com/lds/pc/global/images/itemicon/"
url2 = "da/dad2c1875e87e7a7f1d02fce24f0bd0351cdda11.png?n5.45"
pic_url = url1 + url2
my_page = urlopen(pic_url)
my_picture = io.BytesIO(my_page.read())
pil_img = Image.open(my_picture)
tk_img = ImageTk.PhotoImage(pil_img)
self.label = tk.Label(root, image=tk_img)
self.label.pack(padx=5, pady=5)
root = Tk()
run = MainWin(root)
root.mainloop()
Hello I am having issues with resizing my picture. I am trying to resize the image to fit the blue drawing. However the way I am doing it, returns an error.
File "gui.py", line 42, in fileDialog
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
AttributeError: 'PhotoImage' object has no attribute 'resize
I am just testing it to see if it fits by doing 800,600 I really don't know.
def fileDialog(self):
self.filename = filedialog.askopenfilename(title="Select")
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column=1, row=2)
self.label.configure(text=self.filename)
self.photo= ImageTk.PhotoImage(file = self.filename)
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
self.display.grid(row=0)
Is there something that I am doing incorrectly? Please advise.
You need to resize the image, not the photoimage.
import tkinter as tk
from PIL import Image, ImageTk
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root = tk.Tk()
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage = tk.Label(root, image=root.photoimg)
labelimage.pack()
To address the new question, you do not have to know the filename at the time of label creation. The following code produces the same result:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
labelimage = tk.Label(root)
labelimage.pack()
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage.configure(image=root.photoimg)
There is an image which I'd like to draw in different colors, so I converted it to a bitmap, but when trying to create it on the canvas I get an error.
This is the code:
import PIL.Image
from PIL import ImageTk
from tkinter import *
im = PIL.Image.open("lightbulb.gif")
small_im = im.resize((20,20), resample=PIL.Image.NEAREST).convert('1');
root = Tk()
canvas = Canvas(root,width=100,height=100,bg='black')
canvas.pack()
bitmap = ImageTk.BitmapImage(small_im)
bitmap_id = canvas.create_bitmap(3,3,background='', foreground='gray', bitmap=bitmap,
anchor=NW)
root.mainloop()
I get the following error:
Traceback (most recent call last):
File "/Users/ronen/Dropbox/trycanvas/bitmaps.py", line 13, in <module>
bitmap_id = canvas.create_bitmap(3,3,background="", foreground="gray", bitmap=bitmap, anchor=NW)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2486, in create_bitmap
return self._create('bitmap', args, kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bitmap "pyimage2" not defined
What am I doing wrong?
The tkinter canvas.create_bitmap() method expects its bitmap= option to be a string containing either the name of one of the standard bitmaps (which are 'error', 'gray75', 'gray50', 'gray25', 'gray12', 'hourglass', 'info', 'questhead', 'question', and 'warning') and which look like this:
Or the pathname of a file with one of your own in it in .xbm file format prefixed with an # character.
Below is how to modify your code so it saves the image you want to display in a temporary .xbm format file and then tells tkinter to use that:
import os
import PIL.Image
from PIL import ImageTk
from tempfile import NamedTemporaryFile
import tkinter as tk
im = PIL.Image.open("lightbulb.gif")
small_img = im.resize((20,20), resample=PIL.Image.NEAREST).convert('1');
with NamedTemporaryFile(suffix='.xbm', delete=False) as temp_img:
small_img.save(temp_img.name)
root = tk.Tk()
canvas = tk.Canvas(root, width=100, height=100, bg='black')
canvas.pack()
bitmap_id = canvas.create_bitmap(3, 3, background='', foreground='gray',
bitmap='#'+temp_img.name, anchor=tk.NW)
root.mainloop()
try: # Cleanup
os.remove(temp_img.name) # Get rid of named temporary file.
except FileNotFoundError:
pass
Okay, I now understand what's going on. ImageTk.BitmapImage actually return an image, not a bitmap, but it can be used to change the colors. So intead of:
bitmap = ImageTk.BitmapImage(small_im)
bitmap_id = canvas.create_bitmap(3,3,background='', foreground='gray', bitmap=bitmap,
anchor=NW)
I should have coded:
from_bitmap = ImageTk.BitmapImage(small_im, background='', foreground='gray')
bitmap_id = canvas.create_image(3,3, image=from_bitmap, anchor=NW)
I can't seem to get my PIL Image to work on canvas. Code:
from Tkinter import*
import Image, ImageTk
root = Tk()
root.geometry('1000x1000')
canvas = Canvas(root,width=999,height=999)
canvas.pack()
image = ImageTk.PhotoImage("ball.gif")
imagesprite = canvas.create_image(400,400,image=image)
root.mainloop()
Error:
Traceback (most recent call last):
File "C:/Users/Mark Malkin/Desktop/3d Graphics Testing/afdds.py", line 7, in <module>
image = ImageTk.PhotoImage("ball.gif")
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 109, in __init__
mode = Image.getmodebase(mode)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 245, in getmodebase
return ImageMode.getmode(mode).basemode
File "C:\Python27\lib\site-packages\PIL\ImageMode.py", line 50, in getmode
return _modes[mode]
KeyError: 'ball.gif'
I need to use PIL images not PhotoImages because I want to resize my images. Please don't suggest switching to Pygame because I want to use Tkinter.
Try creating a PIL Image first, then using that to create the PhotoImage.
from Tkinter import *
import Image, ImageTk
root = Tk()
root.geometry('1000x1000')
canvas = Canvas(root,width=999,height=999)
canvas.pack()
pilImage = Image.open("ball.gif")
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(400,400,image=image)
root.mainloop()
(An old question, but the answers so far are only half-complete.)
Read the docs:
class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)
image – Either a PIL image, or a mode string. [...]
file – A filename to load the image from (using Image.open(file)).
So in your example, use
image = ImageTk.PhotoImage(file="ball.gif")
or explicitly
image = ImageTk.PhotoImage(Image("ball.gif"))
(And remember – as you did correctly: Keep a reference to the image object in your Python program, otherwise it is garbage-collected before you seee it.)
You can import multiple image formats, and resize with this code. "basewidth" sets the width of your image.
from Tkinter import *
import PIL
from PIL import ImageTk, Image
root=Tk()
image = Image.open("/path/to/your/image.jpg")
canvas=Canvas(root, height=200, width=200)
basewidth = 150
wpercent = (basewidth / float(image.size[0]))
hsize = int((float(image.size[1]) * float(wpercent)))
image = image.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
item4 = canvas.create_image(100, 80, image=photo)
canvas.pack(side = TOP, expand=True, fill=BOTH)
root.mainloop()
I was banging my head against the wall for a while on this issue until I found the following:
http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm
Apparently, Python's garbage collector can trash the ImageTk object. I imagine apps using alot of widgets (like mine) are more susceptible to this behavior.