Python Forum
[Tkinter] Saving images
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
Reply
#2
You need to use pillow (imported as PIL) see: https://gist.github.com/nakagami/3764702
Reply
#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?
Reply
#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.
Reply
#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.
[Image: choice.png]
Reply
#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?
Reply
#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
Reply
#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?
Reply
#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
Reply
#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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020