0

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) :)

  • Put the text in a dummy canvas using `.create_text` and then save the contents of the canvas as an image. Also how are you going to read the image? Generally it's a bad idea to convert text => image, unless you want to print out the text. – TheLizzard Sep 04 '21 at 10:05
  • TheLizzard i didn't understood what you are talking can you please explain – Muhammad Jamil Sep 04 '21 at 10:08
  • Why save it as a picture to make it read-only? why not just change the file permission to make the text file read-only? – Art Sep 04 '21 at 11:27
  • This might help Text to PDF [https://stackoverflow.com/questions/34837707/how-to-extract-text-from-a-pdf-file] – Derek Sep 04 '21 at 11:43

2 Answers2

1

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.

Block
  • 159
  • 1
  • 6
  • 1
    What if OP doesn't know the amount of text that needs to be saved? Right now you create an image 100x30 but sometimes that might not be enough. Also please add an explanation to how your code works. It will be more likely to help OP. – TheLizzard Sep 04 '21 at 11:39
  • Thank for this comment! – Block Sep 04 '21 at 11:54
1

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)

Arian Raci
  • 11
  • 2