Can I save a tkinter PhotoImage as .gif (or any other image extension) using only Python and tkinter?
Assuming photo is the variable holding your image, you should be able to just say:
photo.write('some_name.gif', format='gif')
Although it doesn't say explicitly, it supports viewing PGM, PPM, GIF, PNG formats, so I assume it can save to these formats as well.
Looks like the method write can be used to save a PhotoImage to a file.
>>> help(Tkinter.PhotoImage.write)
Help on method write in module Tkinter:
write(self, filename, format=None, from_coords=None) unbound Tkinter.PhotoImage method
Write image to file FILENAME in FORMAT starting from
position FROM_COORDS.
Sample usage would be something like:
my_photo_image.write("output.gif")
Related
I have a chart function that saves the end figure as a file. After I run the function, I also want it to display the figure at the end. So, I use this:
from PIL import Image
filepath = 'image.png'
img = Image.open(filepath)
img.show()
It works just fine, but when the file opens, it opens with a random file name, not the actual file name.
This can get troublesome as I have a lot of different chart functions that work in a similar fashion, so having logical names is a plus.
Is there a way I can open an image file with Python and have it display it's original file name?
EDIT
I'm using Windows, btw.
EDIT2
Updated the example with code that shows the same behaviour.
Instead of PIL you could use this:-
import os
filepath = "path"
os.startfile(filepath)
Using this method will open the file using system editor.
Or with PIL,
import Tkinter as tk
from PIL import Image, ImageTk # Place this at the end (to avoid any conflicts/errors)
window = tk.Tk()
#window.geometry("500x500") # (optional)
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()
The function img.show() opens a Windows utility to display the image. The image is first written to a temporary file before it is displayed. Here is the section from the PIL docs.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show
Image.show(title=None, command=None)[source] Displays this image. This
method is mainly intended for debugging purposes.
This method calls PIL.ImageShow.show() internally. You can use
PIL.ImageShow.register() to override its default behaviour.
The image is first saved to a temporary file. By default, it will be
in PNG format.
On Unix, the image is then opened using the display, eog or xv
utility, depending on which one can be found.
On macOS, the image is opened with the native Preview application.
On Windows, the image is opened with the standard PNG display utility.
Parameters title – Optional title to use for the image window, where
possible.
"
The issue is that PIL uses a quick-and-dirty method for showing your image, and it's not intended for serious application use.
I want to be able to do this:
#Reference widget
my_widget = self.ids.my_widget_kv
#Extract texture or graphics
drawing = my_widget.texture (or something that works)
#TODO convert to drawing .png in binary
#TODO edit in PIL
TODO save or attach to email
At the moment I get my_widget.texture as None, and my_widget.canvas as canvas object.
I want to extract widgets looks as is and convert it to .png to attach to email or edit.
timestr = time.strftime("%Y%m%d_%H%M%S")
self.ids.export_to_png("IMG_{}.png".format(timestr))
This code saves drawing_zone looks to storage. I can't edit it in some in between steps.
If you just open and read the PNG file you'll get the bytes which you can then manipulate to your liking.
canvas_png = open("IMG.png","r+b").read()
canvas_data = (canvas_png.getvalue())
print(canvas_data)
You should see the 'binary' you need stored in this variable.
I'm resizing images in python using Pillow
image = Image.open("image_file.jpg")
print(image.format) # Prints JPEG
resized_image = image.resize([100,200],PIL.Image.ANTIALIAS)
print(resized_image.format) # Prints None!!
Why does resized_image.format Hold a None Value?
And How can i retain the format when resizing using pillow?
Because Image.resize creates a new Image object (resized copy of the image) and for any images when creating by the library itself (via a factory function, or by running a method on an existing image), the "format" attribute is set to None.
If you need the format attribute you still can to do this:
image = Image.open("image_file.jpg") #old image object
resized_image = image.resize([100,200],PIL.Image.ANTIALIAS)
resized_image.format = image.format # original image extension
Read the docs
As stated in the documentation:
The file format of the source file. For images created by the library itself (via a factory function, or by running a method on an existing image), this attribute is set to None.
You can specify the format on save:
image.save(fp, 'JPEG')
You can save the resized images with save comments
resized_image.save("New_image.png")
It will save into your current directory.
If you want to see in the python console itself you have to run
resized_image.show()
I want to generate a barcode image. So, I used elaphe package. It works correctly but it returns PIL.EPSImageFile instance. I don't know how I can convert it to image format like SVG, PNG or JPG.
The code I have written is:
barcode('code128', 'barcodetest')
And it returns:
<PIL.EpsImagePlugin.EpsImageFile image mode=RGB size=145x72 at 0x9AA47AC>
How can I convert this instance to image?
Actually I think my question is wrong but I don't know how to explain it well!
Simply save that file object to something with a .png or .jpg filename:
bc = barcode('qrcode',
'Hello Barcode Writer In Pure PostScript.',
options=dict(version=9, eclevel='M'),
margin=10, data_mode='8bits')
bc.save('yourfile.jpg')
or state the format explicitly:
bc.save('yourfile.jpg', 'JPEG')
PIL will then convert the image to the correct format.
Note that the PIL EPS module uses the gs command from the Ghostscript project to do it's conversions, you'll need to have it installed for this to work.
More specifically, I want to change the filetype of an image uploaded through a Django ImageField.
My current thinking is to created a custom ImageField and overwrite the save method to manipulate the file.
I've having trouble getting an in memory file to because a PIL Image instance.
Thanks for the help.
Have you tried StringIO ?
see the docs http://effbot.org/imagingbook/introduction.htm#more-on-reading-images
#Reading from a string
import StringIO
im = Image.open(StringIO.StringIO(buffer))
Note that Django's ImageField inherits the open method from FieldFile. This returns a stream object that can be passed to PIL's Image.open (the standard factory method for creating Image objects from an image stream):
stream = imagefield.open()
image = Image.open(stream)
stream.close()
# ... and then save image with: image.save(outfile, format, options)
See PIL Image documentation.