{"id":4690,"date":"2024-10-04T14:04:29","date_gmt":"2024-10-04T13:04:29","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4690"},"modified":"2025-04-30T11:33:07","modified_gmt":"2025-04-30T10:33:07","slug":"image","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/gui\/tkinter-tk-gui-toolkit\/gui-objects\/image","title":{"rendered":"Image"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Show image object<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import tkinter as tk\nfrom PIL import ImageTk, Image\n\n    #Create an image\n    #img_image1 = ImageTk.PhotoImage(Image.open(\"myimage.jpg\"))\n    img_image1 = ImageTk.PhotoImage(Image.open(\"myimage.jpg\").resize((50, 50)))        #&lt;&lt;&lt;Set the desired image size (if you want your image displaed at a different size you have to resize it first like this)\n    label_image1 = tk.Label(root, image=img_image1)\n    label_image1.image = img_image1               #Keep a reference to the image so its not destoyed\n    label_image1.place(x=5, y=50)                #Top left corner of the image placement<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Update an image<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>#Make the image and its label holder global\n    global img_image1\n    global label_image1\n\n    #Update it to a new image:\n    img_image1 = ImageTk.PhotoImage(Image.open(\"myimage2.jpg\").resize((250, 141)))      #&lt;&lt;&lt;Set the desired image size (if necessary)\n    label_image1.configure(image=img_image1)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Image is blank<\/h4>\n\n\n\n<p>When you add an image object to a Tkinter widget, you must keep your own reference to the image object. If you don\u2019t, the image won\u2019t always show up. This is because the Tkinter\/Tk interface doesn\u2019t handle references to Image objects properly; the Tk widget will hold a reference to the internal object, but Tkinter does not. When Python\u2019s garbage collector discards the Tkinter object, Tkinter tells Tk to release the image. But since the image is in use by a widget, Tk doesn\u2019t destroy it. Not completely. It just blanks the image, making it completely transparent\u2026<\/p>\n\n\n\n<p>The solution is either to make a global image object, or to make sure to keep a reference to the Tkinter object, for example by attaching it to a widget attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>label = tk.Label(image=photo)\nlabel.image = photo     #Keep a reference to the image so its not destoyed\nlabel.pack()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Update image from a different formatted source image type<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    img = ap_opencv.cv2.cvtColor(ap_opencv.annotated_frame, ap_opencv.cv2.COLOR_BGR2RGB)\n    img = Image.fromarray(img)\n    img = img.resize((250, 141))      #&lt;&lt;&lt;Set the desired image size\n    img_camera_image = ImageTk.PhotoImage(img)\n    label_camera_image.configure(image=img_camera_image)\n    label_camera_image.image = img_camera_image               #Keep a reference to the image so its not destoyed<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Show image object Update an image Image is blank When you add an image object to a Tkinter widget, you must keep your own reference to the image object. If you don\u2019t, the image won\u2019t always show up. This is because the Tkinter\/Tk interface doesn\u2019t handle references to Image objects properly; the Tk widget will [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[],"class_list":["post-4690","post","type-post","status-publish","format-standard","hentry","category-gui-objects"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4690"}],"version-history":[{"count":15,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4690\/revisions"}],"predecessor-version":[{"id":4907,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4690\/revisions\/4907"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}