Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Saving images
#1
Hello,
I'm trying to create a image viewer in tkinter. I have problem with saving the images. On canvas I created an image. For save I don't use PhotoImage class but first version (photo = Image.open(somefile)). I use the following commands to save it:
 a = photo.filename = asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (('JPEG', ('*.jpg','*.jpeg','*.jpe','*.jfif')),('PNG', '*.png'),('BMP', ('*.bmp','*.jdib')),('GIF', '*.gif')))
photo.save(a)
User can select what type of image he wants (jpg, png,...) but python doesn't save the type and throws out error.
Error:
ValueError: unknown file extension:
I must write name of image like photo123.png to entry and then the image is saved. But users don't want to write .jpg,.png,... at the end of name of image.
What I have to add to the code? Some other options or function to save selected type?
Thank you, Christina
Quote
#2
You need to use pillow (imported as PIL) see: https://gist.github.com/nakagami/3764702
Quote
#3
I imported PIL at the begining of my code: from PIL import Image, ImageTk .
I have longer code (it is only part which I use to save photo) unfortunately I wrote it in different language so I must rewrite it to english before I sent it here.
image = Image.open(filename)
photo = ImageTk.PhotoImage(image)
a = image.filename = asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (('JPEG', ('*.jpg','*.jpeg','*.jpe','*.jfif')),('PNG', '*.png'),('BMP', ('*.bmp','*.jdib')),('GIF', '*.gif')))
image.save(a)
I save image (class Image not PhotoImage). Should I use PhotoImage for save? Like:
 a = photo.filename..., photo.save(a)
? But I think PhotoImage has no module to save it. Where else could there be the problem?
Quote
#4
If the image buffer is formatted properly,
just open the image file in mode 'wb' and save using write, giving the file a proper image name.
example:
with open('imagename.png', 'wb') as fp:
    fp.write(imagebuffer)
use proper suffix for image type as well.
Quote
#5
The mode 'wb' does not work with image = Image.open(filename, 'wb'). It gives me error:
Error:
ValueError: bad mode 'wb'
I want to user could write name of image and select file (using the function asksaaveasfilename) and suffix was saved according to the user's choice. Now suffix has to be write by user.
İmage
Quote
#6
don't know why you would get this error, it's a perfectly good mode. Been using 'wb' both in python and in C for many years.
did you put it in quotes?
Quote
#7
Quote:I want to user could write name of image and select file (using the function asksaaveasfilename) and suffix was saved according to the user's choice. Now suffix has to be write by user.
You will need to do this using graphics, the built in tkinter package is capable of this, you can use tkinter filedialog, ttk.treeview or ListBox.
Use google to search for examples
Quote
#8
Maybe I didn't make it clear. I'm writing my app in tkinter and I used tkinter filedialog (from tkinter.filedialog import asksaveasfilename). My code is like this (I wrote it in my first post):
a = photo.filename = asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (('JPEG', ('*.jpg','*.jpeg','*.jpe','*.jfif')),('PNG', '*.png'),('BMP', ('*.bmp','*.jdib')),('GIF', '*.gif')))
photo.save(a)
User can select suffix (in GUI mode - Windows) JPEG, PNG, BMP or GIF but the selected suffix is not saved and app throws out error. In variable a is saved for example C:\Photos\Photo012 but I need save to a suffix. In variable a should be for example C:\Photos\Photo012.png
Please do you know some function or options which can save selected suffix (in my examle PNG .png) and add it to variable a?
Quote
#9
check out this tutorial: https://www.youtube.com/watch?v=Aim_7fC-inw
It's totally about filedialog, and seems to have good coverage of images
Quote
#10
note that a = photo.filename = asksaveasfilename(...) is actually
a = (photo.filename = asksaveasfilename(...))

so value of a is either True or False if you don't get arror
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)