So, I need to resize an image in tkinter. Before you do anything - this is not a duplicate. I have gone through every other question on this site and none have helped me. The thing with me is - I don't wan't to save the image. I need to load the image, resize it, then display it with PhotoImage in a label. I tried to use ImageTk, but for some reason it won't work.
Here's my code with ImageTk:
from tkinter import *
import PIL
from PIL import Image, ImageTk
root = Tk()
left_nose_path_white = Image.open(r'C:\Users\User\Documents\Python stuff\Other apps\Veteris\Dog photos\Advanced\Sides\GIF\Nose\Nose (left) - White.gif')
def resize_image():
global left_nose_path_white
total_screen_width = root.winfo_screenwidth()
total_screen_height = root.winfo_screenheight()
frame_width, frame_height = left_nose_path_white.size
dog_new_width = int(frame_width / 3)
dog_new_height = int(frame_height / 3)
left_nose_path_white = left_nose_path_white.resize((dog_new_width, dog_new_height), Image.LANCZOS)
resize_image()
left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
label = Label(image = left_nose_img_white)
label.pack()
root.mainloop()
This returns the error:
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
My code should find the width and height of the original image, divide it by 3, and then show it. The reason I don't want to have to save the image is because the user will open the application several times.
I'm new to using PILL/Pillow, so the answer may be obvious.
I have Pillow installed using pip.
I only have one version of Python on my computer (Python 3.7)
Full Error:
Traceback (most recent call last):
File "C:\Users\User\Documents\Python stuff\Other apps\Veteris\Scripts\Veteris_program.py", line 230, in <module>
left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 95, in __init__
image = _get_image_from_kw(kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 64, in _get_image_from_kw
return Image.open(source)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2779, in open
prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x000002B0A8A49950>
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Thanks for the help!
Related
Goal : 1.Select PDF 2.OCR PDF 3.Write tabels to excel with onefile .exe
Script is working in Pycharm perfect but after compiling to exe i am getting this traceback.
Traceback (most recent call last):
File "OCR_Menu.py", line 26, in <module>
File "ocrmypdf\api.py", line 340, in ocr
File "ocrmypdf\_validation.py", line 240, in check_options
AttributeError: 'NoneType' object has no attribute 'languages'
[11220] Failed to execute script 'OCR_Menu' due to unhandled exception!
Any help to get this script working as a single executable file will be very appreciated!
Thank you!
import ocrmypdf
import camelot
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
# if filename is not None:
if __name__ == '__main__': # To ensure correct behavior on Windows and macOS
ocrmypdf.ocr (filename, 'output.pdf', deskew=True,)
file = "output.pdf"
tables = camelot.read_pdf(file, pages = "1-end", flavor='stream')
print(tables.n)
tables=camelot.read_pdf(file, pages='1-end', flavor='stream')
tables.export(filename + ".xlsx", f='excel')
if you're currently using googletrans==3.0.0, please switch to googletrans==3.1.0a0 for the temporary fix.
I am trying to create a pptx or powerpoint file using python-pptx and reading the image using python wand library but getting error like AttributeError: 'Image' object has no attribute 'seek'. Did you mean: 'seed'?
Note: All the files are in same folder starts with 'watermarked_'
from io import FileIO
import os
from wand.image import Image
from pptx.util import Inches
from pptx import Presentation
def create_slide()->FileIO:
# Creating presentation object
root = Presentation()
for file in os.listdir():
if file.startswith('watermarked_'):
# Creating slide layout
first_slide_layout = root.slide_layouts[1]
slide = root.slides.add_slide(first_slide_layout)
shapes = slide.shapes
#Adding title or heading to the slide
title_shape = shapes.title
title_shape.text = f" Created By python-pptx for Watermarking "
#Adding sub-title with border to the slide
body_shape = shapes.placeholders[1]
tf = body_shape.text_frame
tf.text = f"This is a watermarked image of {file}"
with Image(filename = file) as watermarked_image:
#Maintianing the aspect ratio of the image
width, height = watermarked_image.size
ratio = height/width
new_width = width / 2
new_height = int(new_width * ratio)
watermarked_image.resize(int(new_width), new_height)
# Add the watermarked image to the slide
slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3))
root.save("Output.pptx")
create_slide()
Traceback (most recent call last):
File "/Users/quantum/Desktop/image/project.py", line 60, in <module>
quantum#MacBook-Air image % python -u "/Users/quantum/Desktop/image/project.py"
Traceback (most recent call last):
File "/Users/quantum/Desktop/image/project.py", line 60, in <module>
create_slide()
File "/Users/quantum/Desktop/image/project.py", line 57, in create_slide
slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/shapes/shapetree.py", line 332, in add_picture
image_part, rId = self.part.get_or_add_image_part(image_file)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/parts/slide.py", line 39, in get_or_add_image_part
image_part = self._package.get_or_add_image_part(image_file)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/package.py", line 36, in get_or_add_image_part
return self._image_parts.get_or_add_image_part(image_file)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/package.py", line 151, in get_or_add_image_part
image = Image.from_file(image_file)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pptx/parts/image.py", line 168, in from_file
if callable(getattr(image_file, "seek")):
AttributeError: 'Image' object has no attribute 'seek'. Did you mean: 'seed'?
Any frequent help will be much appreciated
A wand.image.Image object is not a valid argument for Shapes.add_picture(). The first argument to that call needs to be the str path to an image file or a file-like object containing an image.
I suppose that means you'll need to save the modified image as a JPG or PNG or whatever and then provide the filename. You could also save it to a BytesIO object and pass that to .add_picture() as that would count as a file-like object and not require using the filesystem.
I am working on winows 7 and python 3.8.2. I have an error.
I wanted to upload an Image in a tkinter window. I tried importing a .gif file and I get an error as
"couldn't recognize data in image file". Though the file is in the correct path the error is happening.
Can you please help me?
Here is the Code
from tkinter import *
window = Tk()
window.title("try")
canvas = Canvas(window, height = 500, width = 500)
canvas.pack()
my_image = PhotoImage(file = "C:\\Users\\jeeva\\Pictures\\bulbon.gif")
canvas.create_image(0, 0, anchor = NW, image = my_image)
window.mainloop()
And here is the error:
Traceback (most recent call last):
File "C:\Users\jeeva\Desktop\Tanmay_new\python\Codes\Not Finished\tett.py", line 6, in <module>
my_image = PhotoImage(file = "C:\\Users\\jeeva\\Pictures\\bulbon.gif")
File "C:\Users\jeeva\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\jeeva\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:\Users\jeeva\Pictures\bulbon.gif"
Thank you,
Regards,
Tanmay
I am making a simple OpenGL 3.3 OBJ viewer in Python using the modules Pyglet and Ratcave. I want to load a textured sphere I made in Blender. While my Pyglet/Ratcave OBJ viewer loads untextured OBJ files, when I add textures to my project, I get this error and the program fails to load:
ValueError: could not convert string to float: 'textures'
What does this mean?
I got a different error when I tried to import textures from my OneDrive folder and use them in my Blender project. When I put the textures in the same folder as the script and OBJ/MTL files, it got rid of that error, but now I can't load the textures because, for whatever reason, Ratcave needs to convert the textures to a float and it can't do that.
import pyglet
import ratcave as rc
import time
window = pyglet.window.Window()
def update(dt):
pass
pyglet.clock.schedule(update)
# Insert filename into WavefrontReader
obj_filename = 'Textured Sphere Eevee.obj'
obj_reader = rc.WavefrontReader(obj_filename)
# Check which meshes can be found inside the Wavefront file, and extract it into a Mesh object for rendering
print(obj_reader.bodies.keys())
Sphere = obj_reader.get_mesh("Cube")
Sphere.position.xyz = 0, 0, -10
scene= rc.Scene(meshes=[Sphere])
#window.event
def on_draw():
with rc.default_shader:
scene.draw()
#scene.clear()
#time.sleep(10)
pyglet.app.run()
Traceback (most recent call last):
File "C:\Users\Jeffery\Desktop\Art\Pyglet Game Engine\scripts\Ratcave_OBJ_Loader_Test 2.py", line 15, in <module>
obj_reader = rc.WavefrontReader(obj_filename)
File "C:\Users\Jeffery\AppData\Local\Programs\Python\Python37-32\lib\site-packages\ratcave\wavefront.py", line 28, in __init__
self.bodies = read_wavefront(file_name)
File "C:\Users\Jeffery\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wavefront_reader\reading.py", line 114, in read_wavefront
materials = read_mtlfile(path.join(path.dirname(fname_obj), fname_mtl))
File "C:\Users\Jeffery\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wavefront_reader\reading.py", line 88, in read_mtlfile
material[prefix] = tuple(float(d) for d in split_data)
File "C:\Users\Jeffery\AppData\Local\Programs\Python\Python37-32\lib\site-packages\wavefront_reader\reading.py", line 88, in <genexpr>
material[prefix] = tuple(float(d) for d in split_data)
ValueError: could not convert string to float: 'textures'
This is a basic script that should insert a watermark image on the first page of a PDF and save it under a new name.
I could do the same with the same files in pdfrw, but I'm stuck with PyMuPDF (which I would prefer to use...).
The py file is in the same folder with the pdf and the png.
import fitz
input_file = "sample.pdf"
output_file = "sample_stamped.pdf"
stamp = "watermark.png"
doc = fitz.open(input_file)
rect = fitz.Rect(0, 0, 100, 100)
pix = fitz.Pixmap(stamp)
page = doc[0]
page.insertImage(rect, pixmap = pix, overlay = True)
doc.save(output_file)
I'm getting this error msg:
Traceback (most recent call last):
File "D:/Google Drive/Python/PDF/pdfstamp.py", line 14, in <module>
doc.save(output_file)
File "D:\Python\lib\site-packages\fitz\fitz.py", line 2411, in save
return _fitz.Document_save(self, filename, garbage, clean, deflate, incremental, ascii, expand, linear, pretty, decrypt)
RuntimeError: not a dict (array)
Thanks in advance for any help.