1

Ok so I'm currently writing a simple image viewer and I have enough code to be able to view those images, but the only problem is to view the images you want. You have to put them in the same directory as the script and rename them. I want the user to be able to click something like file-open and then import those images. I'm currently using Tkinter as my Gui and PIL for displaying the images. here is my latest code:

from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style
import os
import PIL
import Tkinter 

filename = "test.jpg"
filename2 = "test1.jpg"
filename3 = "test2.jpg"
filename4 = "test3.jpg"
basewidth = 300
img = Image.open(filename)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize.jpg')

basewidth = 300
img = Image.open(filename2)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize2.jpg')

basewidth = 300
img = Image.open(filename4)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize4.jpg')
class Example(Frame):



    def __init__(self, parent):
            Frame.__init__(self, parent)   

            self.parent = parent

            self.initUI()

    def initUI(self):

            self.parent.title("Picture")
            self.pack(fill=BOTH, expand=1)

            Style().configure("TFrame", background="")

            image1 = Image.open("resize.jpg")
            bardejov = ImageTk.PhotoImage(image1)
            label1 = Label(self, image=bardejov)
            label1.image = bardejov
            label1.place(x=5, y=5)

            image2 = Image.open("resize2.jpg")
            bardejov = ImageTk.PhotoImage(image2)
            label1 = Label(self, image=bardejov)
            label1.image = bardejov
            label1.place(x=5, y=250)

            image3 = Image.open("resize3.jpg")
            bardejov = ImageTk.PhotoImage(image3)
            label1 = Label(self, image=bardejov)
            label1.image = bardejov
            label1.place(x= 350, y=5)

            image3 = Image.open("resize4.jpg")
            bardejov = ImageTk.PhotoImage(image3)
            label1 = Label(self, image=bardejov)
            label1.image = bardejov
            label1.place(x= 350, y=250)

def main():

     root = Tk()
     root.geometry("660x488")
     app = Example(root)
     root.mainloop()  

if __name__ == '__main__':
     main()  
2
  • What don't you understand about the problem? Are you aware that Tkinter has built-in file dialogs? Or, do you know about it but don't understand how to use it? Or is there something else you don't understand about it? Commented Jul 17, 2013 at 20:43
  • I know tkinter has a built in file dialog ive spent all day trying to fiqure it out but i fianlly hit a dead end and posted here i need someone to show me how i would add a file dialog to the define all of the img = what ever goes here to open the file dialog. and then i just dont get how to open it. Commented Jul 17, 2013 at 21:01

1 Answer 1

1

Here you go - I do it for one image, you do it for all others similarly

import tkFileDialog
from Tkinter import *
from PIL import Image
import os

root= Tk()

def resizeIt():
    filename = tkFileDialog.askopenfilename()
    basewidth = 300
    img = Image.open(filename)
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    img = img.resize((basewidth, hsize), Image.ANTIALIAS)
    img.save('resize.jpg')
    os.remove(filename) # deletes the original image after you have got the resized image


Button(text='add image', command=resizeIt).pack()

root.mainloop()

Edit after your question in comment

Yes you can delete a file using the os module. First you import os in the current namespace and then after you have saved the resized image, you add a line os.remove(filename). I have done that in the above code.

Sign up to request clarification or add additional context in comments.

7 Comments

something wierd is going on after you select the four images you have to click x to open it how do i add a text to tell the user to do that or do u have a fix by the way thanks
you can select only one image in one dialog. select a single image from within the file dialog and click 'open'. Thats it. You will get an image by the name resize.jpg in the same location where you have saved this python script
do i have to have the button four times because i have it repeated four times but all under the def rezimg and then have button(ect) once
depends on what you want to do. If you have 4 buttons, add 4 new commands like resizeIt1 to resizeIt4 and for each of the methods and also be careful to save each one by a different name. most importantly read some book on python and/or tkinter
ive gone through learn python the hard way im in the process of learning tkiner
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.