I have searched on a variety of variation of "PIL tkinter canvas". The closest thing I have found is this: How to convert a Python tkinter canvas postscript file to an image file readable by the PIL?
turtle.py creates images on a scrollable tkinter canvas so I wrote a script based on the Answer above.
#! /usr/bin/python3
import sys
print(sys.argv[0])
sys.path.append('C:\\Program Files\\gs\\gs9.21\\bin\\') # Where Ghostscript lives
import turtle as t
from PIL import Image
def line(nPlots=0,x0=0,y0=0,H=6000):
t.penup()
t.goto(x0,y0)
#t.setheading(90)
t.begin_fill()
t.pendown()
print('Forward:',H)
t.forward(H)
t.screensize(12000,700)
t.color('red','light blue')
t.width(20)
t.tracer(10000)
line()
c=t.getcanvas()
t.update()
c.update()
import io
def savecanvas():
screen=t.getscreen()
canvas=screen.getcanvas()
postscript=canvas.postscript().encode('utf-8')
im=Image.open(io.BytesIO(postscript))
im.save('canvas.png')
savecanvas()
This indeed results in a screencapture but not a complete canvascapture. The output file is the required 12000 pixels long but the long red bar is clipped by the screen size. The culprit seems to be the Ghostscript Bounding Box. Here is a snippet from the gs file:
infile: b"%!PS-Adobe-3.0 EPSF-3.0\n%%Creator: Tk Canvas Widget\n%%Title: Window .!canvas\n%%CreationDate: Sat Apr 29 10:36:40 2017\n%%BoundingBox: -197 188 811 605\ ...
Ghostscript is setup within the PIL EpsImagePlugin.py called from the PIL Image module.
Here is that code:
# Build ghostscript command
command = ["gs",
"-q", # quiet mode
"-g%dx%d" % (12000, 800), # set output geometry (pixels)
"-r%fx%f" % (96,96), # set input DPI (dots per inch)
"-dNOPAUSE", # don't pause between pages,
"-dSAFER", # safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % outfile, # output file
"-c", "%d %d translate" % (-300,-375),
# adjust for image origin
"-f", infile, # input file
I have tried to force the bbox with no success. I suspect I am in need of some additional gs command line option.
Does anyone know how to coerce Ghostscript into accepting wide input images?