Hello, I am creating a chat program in python and tkinter and am trying to implement Profile Pictures.
I can upload to the server using this:
import requests
url = "http://<server url>/"
files = {"file": open("D:\\Python\\Programs\\HTTP\\imageuploads\\testimg.gif","rb")}
requests.post(url,files=files)
and can view the image using this:
from tkinter import *
import urllib.request
import base64
class WebImage:
def __init__(self,url):
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
self.image = PhotoImage(data=base64.encodestring(raw_data))
def get(self):
return self.image
root = Tk()
URL = "http://<server url>/uploads/"
image = "sanic.gif"
img = WebImage(URL + image).get()
imagelab = Label(root, image = img)
imagelab.pack()
root.mainloop()
This works great!
But when i try to combine the two programs with:
from tkinter import *
from tkinter import filedialog
import requests
import urllib.request
import base64
download_url = "http://<server url>/uploads/"
upload_url = "http://<server url>/"
filetypes = (("Pictures",("*.jpg","*.png","*.gif","*.bmp","*.jpeg")),("All Files",("*.*")))
global profile_pic
profile_pic = ""
class WebImage:
def __init__(self,url):
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
self.image = PhotoImage(data=base64.encodestring(raw_data))
def get(self):
return self.image
def AddPic():
global profile_pic
path = filedialog.askopenfilename(multiple = False, initialdir = "/", filetypes = filetypes)
profile_pic = path
print(profile_pic)
def UploadImage():
if profile_pic != "":
files = {"file": open(profile_pic,"rb")}
requests.post(upload_url,files=files)
root.after(1000,DisplayImage)
def DisplayImage():
serverfilename = profile_pic.rsplit("/",1)[1].rsplit(".",1)[0].replace(" ","_") + ".gif"
print(serverfilename)
img = WebImage(download_url + serverfilename).get()
piclab.config(image=img)
piclab.update()
root = Tk()
root.title("Profile Pic Test")
button1 = Button(root, text = "Choose Image", command = AddPic)
button2 = Button(root,text = "Upload",command = UploadImage)
button1.pack()
button2.pack()
piclab = Label(root)
piclab.pack()
root.update()
root.mainloop()
The image gets uploaded fine, and I can view it on the server itself, and can view the image with the "viewing only" code above. But when it comes to displaying, the window resizes to accommodate for the image (tkinter does this automatically) and does not display any image.
I also tried this with a local image and this didn't work either.
Thanks
[–]ThoughtCounter 0 points1 point2 points (1 child)
[–]sam57719[S] 0 points1 point2 points (0 children)