I made a Tkinter GUI, added text-widget to it, and added a save button but when I click the save button it save as a text (.txt file) and I want it to save the text as a picture which would be read-only(.png) can anybody help?
in simple words
whats happening=user save the file - it is saved as a txt file
what I want=user save the file - it is saved as a png Image
It is a text widget not a canvas
(Actually, first i wanted it to Do like because I was having an option that can change the color of the text according to the user's choice and save it as a pdf but that didn't work if you can do that would also work) :)
I think this is help you
from PIL import Image, ImageDraw
img = Image.new('RGB', (100, 30))
d = ImageDraw.Draw(img)
d.text((10,10), "Hello", fill=(255,255,0))
img.save('text.png')
This project use Pillow.First we create a new image.Then we add text to this image and change fill for this text.And Finally we save this image.
Write your cods in txt file and go to file menu and click on (save as) and save file with .png format
like (cod.png)
Related
I have created an app that runs with PyGame, which loads a page of a PDF, creates a Pixmap with getPixmap and saves into a jpg file. Then, the image is displayed with pygame.image.load().
The program works perfectly fine, but it requires to save a .jpg file in the folder, which substitutes and therefore deletes the previous .jpg file. When converting it into an exe and distributing the program, the users get a security message error, saying that the program does not have the permission to delete the .jpg file.
How could I skip the part of saving the photo in the folder, and directly showing from the PDF into pygame?
I think it can be done by creating a file-like object with Bitmap.IO(), but I don't know how to use it. I'm relatively new in python, so I would appreciate if you could write the lines of code you would use.
zoom = 2
doc = fitz.open(pdf_path)
mat = fitz.Matrix(zoom, zoom)
page = doc.loadPage(photo_num)
pix = page.getPixmap(matrix = mat)
photo_output = "photo.jpg"
pix.writePNG(photo_output)
load_photo = pygame.image.load(photo_output)
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 am trying to embed a text in a background image using python. The background image is 1.jpg and the text image is 2.jpg. The background image contains a wall on which I want to embed the text region 2.jpg which contains scene text "Poppins". Can anybody help me out with the code?
The sample input is given
The sample output is somewhat like this:
Though I have doe it with paint!! Originally the sample output should look like this where natural scene text is embedded in the background of the image
Sample Text Image is:
Which is an extracted scene text
If you want to paste one image on top of another, your text file should be a .png file (so that the overlay file contains no background data), then you can use the following code, importing the PIL library:
from PIL import Image
image1 = Image.open('img1.jpg')
image2 = Image.open('img2.png')
image1.paste(image2, (0,0))
image1.show()
Change the (0,0) values to move the png text image around on x,y co-ordinates.
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.
How can I save a matplotlib figure with text as a postscript image and that the text will be saved as text.
Currently when I save the image as postscript all the text in the image ( xlabel, ylabel etc.. ) is saved as path and not as text..
Is it possible to save it as text?
EDIT
If I use the following code ( use latex)
matplotlib.rcParams["text.usetex"] = True
and save the image as postscript the text is saved as text.. But I do not want to use latex.. Is it possible without latex?
Thanks
This is how to make matplotlib save text as font and not path:
matplotlib.rcParams['pdf.fonttype'] = 42
Read more here:
http://physicalmodelingwithpython.blogspot.com/2015/06/making-plots-for-publication.html