Python Forum
[Tkinter] Saving images - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Saving images (/thread-21093.html)

Pages: 1 2


Saving images - Christina - Sep-13-2019

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


RE: Saving images - Larz60+ - Sep-13-2019

You need to use pillow (imported as PIL) see: https://gist.github.com/nakagami/3764702


RE: Saving images - Christina - Sep-13-2019

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?


RE: Saving images - Larz60+ - Sep-14-2019

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.


RE: Saving images - Christina - Sep-14-2019

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]


RE: Saving images - Larz60+ - Sep-14-2019

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?


RE: Saving images - Larz60+ - Sep-14-2019

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


RE: Saving images - Christina - Sep-15-2019

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?


RE: Saving images - Larz60+ - Sep-16-2019

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


RE: Saving images - buran - Sep-16-2019

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