the_prole
the_prole

Reputation: 8945

How to return height and width of a base 64 encoded graphic using tkinter

I am adding an image to my canvas background using a base 64 encoded graphic. My question is how to return the width and height of the encoded graphic image_png so I can set the width and height of the canvas to the same.

import tkinter as tk    

image_png = """
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai8P8A////
/////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva
0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJWuSCAQ30+vNTGcxnOIARj3eT
YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ+PjZUjaJOElKanqJyRrJyZgSKkokOs
NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs=
"""
master=tk.Tk()
master.geometry("100x100")

canvas=tk.Canvas(master)
canvas.grid()

photo=tk.PhotoImage(data=image_png)
canvas.create_image(0, 0, image=photo, anchor="nw")

master.mainloop()

I thought the solution could be something like the following (to make an example):

height, width = photo.height(), photo.width()
canvas.create_image(height = height, width = width, image=photo, anchor="nw")

Upvotes: 2

Views: 52

Answers (1)

falsetru
falsetru

Reputation: 368954

Use width, height method of the PhotoImage:

...
photo = tk.PhotoImage(data=image_png)
print(photo.width())  # => 32
print(photo.height())  # => 32
...

Upvotes: 3

Related Questions