0

[SOLVED] Please don't judge the code. I just need to fix one bug with capturing canvas into file. As you can see I've tried multiple solutions and still while saving file is all-white...

Do you have any soulutions to that?

This app is to generate randomly dots on canvas with option to save it. it works when number of dots is above 40000 and rectangle is black. But otherwise is white.

enter image description here

HEIGTH = 207
WIDTH = 207




def snapsaveCanvas():
    fname = filename.get()
    my_window.update_idletasks()
    my_window.update()
    canvas.update()
    canvas.update_idletasks()
    ps = canvas.postscript(colormode='color')
    img = Image.open(io.BytesIO(ps.encode('utf-8')))
    img.save(fname + ".bmp", 'bmp')
    print("done")





canvas = tk.Canvas(frame2, width=HEIGTH, height=WIDTH, background='white')
canvas.grid(row=0, column=0)
canvas.create_rectangle(4, 4, 206, 206)
UnryMF
  • 13
  • 4
  • Why don't you just show the code and the traceback? – mrvol Sep 20 '20 at 11:20
  • I wanted to put it into comment as I was prevented from putting it in the post... – UnryMF Sep 20 '20 at 11:25
  • sorry i could not save without this string below – UnryMF Sep 20 '20 at 11:28
  • Whats the error? Could you include some picture? – Delrius Euphoria Sep 20 '20 at 11:30
  • @CoolCloud added. The issue is that when i want to save results i see blank image unless i put more then 40000 dots to make canvas full black. It should looks like on the canvas when i add 25000 but its white after saving. black1 file. – UnryMF Sep 20 '20 at 11:34
  • What does `img = Image.open(io.BytesIO(ps.encode('utf-8')))` ? as an alternative you could also get the window location and get the screenshot of the window. – Delrius Euphoria Sep 20 '20 at 11:38
  • it encode canvas poscript and coverts it to bmp , I tried to use PIL.grabImage but it is not a good solution.. Its better to use scrapping tool but its not what i want. – UnryMF Sep 20 '20 at 11:47
  • or if i can get window location and then capture canvas area within that app window that would be enough but I dont know how to do this. – UnryMF Sep 20 '20 at 11:49
  • several methods you can try: https://stackoverflow.com/questions/9886274/how-can-i-convert-canvas-content-to-an-image – Wups Sep 20 '20 at 11:52
  • Thanks Wups. it helps.. there were one method I was not aware of :) – UnryMF Sep 20 '20 at 12:01
  • Is this issue solved? – Delrius Euphoria Sep 20 '20 at 12:01
  • yes, its working fine now . I've posted answer below. Thanks for paying attention and being helpful :) It took me 2h to try diffrent solutions and I was so desparate to post this issue : D – UnryMF Sep 20 '20 at 12:04

1 Answers1

1

Ok, thanks for links.

I've tested several methods and one of them works. taken from How can I convert canvas content to an image?

import win32gui

def snapsaveCanvas():
    fileName = filename.get()
    canvas.update()
    canvas.update_idletasks()

    HWND = canvas.winfo_id()  # get the handle of the canvas
    rect = win32gui.GetWindowRect(HWND)  # get the coordinate of the canvas
    im = ImageGrab.grab(rect).save(fileName + ".bmp")
    print("done")
UnryMF
  • 13
  • 4