p5
p5 copied to clipboard
Error when trying to access image width and height
This is the original image rendered with
image(img, 10,20)
when I try to acess the width and height using this code
from p5 import *
from components.shuffler import Shuffler
from components.debugger import Debugger
from game_logic.constants import ITEMS
# Intellisense can't find these on its own for some reason
global mouse_is_pressed, mouse_x, mouse_y
global shuffler, debugger
global width, height
SHUFFLED_ITEMS = list(ITEMS.keys())
img = None
def start():
"""
Start the UI. This function takes over the main thread (never returns)!
"""
run(renderer="skia", sketch_draw=draw, sketch_setup=setup)
def setup():
size(1024, 600) #touchscreen size
background(200)
global shuffler, debugger, img
shuffler = Shuffler()
debugger = Debugger()
img = loadImage("kart_ui/images/no-item.png")
def draw():
background(255)
print(type(img))
img_width = img.width()
img_height = img.height()
img_width = int(img_width/2)
img_height = int(img_height/2)
image(img, 10,20, img_width, img_height)
def mouse_pressed():
if mouse_x < width/2:
shuffler.shuffle(SHUFFLED_ITEMS[int(random_uniform(0, len(SHUFFLED_ITEMS)))])
else:
shuffler.use_item()
if __name__ == "__main__":
start()
i get this error
Traceback (most recent call last):
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/kart_ui/__init__.py", line 47, in <module>
run()
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/p5/sketch/userspace.py", line 226, in run
app.run()
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/app/_default_app.py", line 60, in run
return default_app.run()
^^^^^^^^^^^^^^^^^
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/app/application.py", line 160, in run
return self._backend._vispy_run()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/app/backends/_glfw.py", line 189, in _vispy_run
self._vispy_process_events()
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/app/backends/_glfw.py", line 178, in _vispy_process_events
timer._tick()
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/app/backends/_glfw.py", line 512, in _tick
self._vispy_timer._timeout()
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/app/timer.py", line 160, in _timeout
self.events.timeout(
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/util/event.py", line 453, in __call__
self._invoke_callback(cb, event)
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/util/event.py", line 471, in _invoke_callback
_handle_exception(self.ignore_callback_errors,
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/vispy/util/event.py", line 469, in _invoke_callback
cb(event)
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/.venv/lib/python3.11/site-packages/p5/sketch/Vispy2DRenderer/base.py", line 114, in on_timer
self.draw_method()
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/kart_ui/__init__.py", line 34, in draw
img_width = img.width()
^^^^^^^^^^^
**TypeError: 'int' object is not callable**
however when I switch the lines accessing width and height to the following
img_width = img.width
img_height = img.height
then I get the error
File "/Users/tytodd/Desktop/Projects/kai_kartsV2/v2/kart_ui/__init__.py", line 36, in draw
img_width = int(img_width()/2)
^^^^^^^^^^^
TypeError: 'int' object is not callable
So I'm confused am I supposed to access width and height as functions or attributes?
Thank you for submitting your first issue to p5py