how to maintain canvas size when converting python turtle canvas to bitmap

Susan Stepney

I want to convert a Python Turtle module (tkinter) canvas into a bitmap. I have followed the suggestion at "How to convert a Python tkinter canvas postscript file to an image file readable by the PIL?" to convert it first to postscript; then I open it as a PIL image, then save that as a bitmap. But the bitmap is a different size from the original canvas.

import turtle
import io
from PIL import Image

myttl = turtle.Turtle()

wd=500
ht=500
turtle.setup(width=wd, height=ht, startx=0, starty=0)    
turtle.mode('logo')        # start pointing north
myttl.forward(100)

screen = turtle.Screen()
cv = screen.getcanvas()
ps = cv.postscript(colormode='mono')
img = Image.open(io.BytesIO(ps.encode('utf-8'))).convert(mode='1')
img.save('test.bmp')

In the above code, the canvas is 500x500. But the file test.bmp is shrunk to 374x374, and its image is smaller than the on-screen turtle graphic. How can I get an unshrunk 500x500 bitmap?

Pax Vobiscum

If we start by analyzing the postscript contents, we see that it does a scaling by 0.7498 to the dimensions of your canvas

%%Page: 1 1
save
306.0 396.0 translate
0.7498 0.7498 scale
3 -241 translate
-244 483 moveto 239 483 lineto 239 0 lineto -244 0 lineto closepath clip newpath
gsave
grestore
gsave
0 239 moveto
0 339 lineto
1 setlinecap
1 setlinejoin
1 setlinewidth
[] 0 setdash
0.000 0.000 0.000 setrgbcolor AdjustColor
stroke
grestore
gsave
grestore
gsave
0 339 moveto
-5 330 lineto
0 332 lineto
5 330 lineto
0 339 lineto
0.000 0.000 0.000 setrgbcolor AdjustColor
eofill
0 339 moveto
-5 330 lineto
0 332 lineto
5 330 lineto
0 339 lineto
1 setlinejoin 1 setlinecap
1 setlinewidth
[] 0 setdash
0.000 0.000 0.000 setrgbcolor AdjustColor
stroke
grestore
restore showpage

After some digging done on postscript I came across a perl/Tk reference guide on postscript conversion from tkinter's canvas here

What you actually can do is to set not only colormode but also pagewidth/pageheight. This resulted in the following line being changed from

ps = cv.postscript(colormode='mono')

to

ps = cv.postscript(colormode='mono', pagewidth=wd-1, pageheight=ht-1)

Result:

enter image description here

If you have any questions feel free to leave a comment and I will try my best to reply!

PS:

Don't ask me about the -1 part, it wouldn't give me anything other that 501x501 pixels so I compensated for that. I do not know why it still didn't work though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Changing canvas scroll region on Python turtle canvas

In Python tkinter, how to shrink the canvas, not the button, when window reduced the size

How to bounce turtle off the canvas

How to convert canvas to bitmap in android?

How to draw on canvas and convert to bitmap?

resizing the canvas dimensions and size of a bitmap file

How to set canvas size?

Python Canvas not expanding for turtle printing automatically

"Canvas: trying to draw too large bitmap" when Android N Display Size set larger than Small

How do I get the Turtle canvas screen to move with a Turtle module?

How to create canvas with large Bitmap, draw on it, and then scale to screen size to conserve memory?

How crop bitmap of selected area on canvas?

How to make a bitmap using canvas clickable?

How to rotate a bitmap on canvas without deform it on Android

how to move two bitmap-image on a canvas

How to Draw Portion of Bitmap via Canvas DrawBitmap

Using canvas and bitmap in Android , how to get this image?

on Firemonkey, how to draw masked bitmap on canvas?

how to draw a Bitmap on the Top Right of the Canvas

How to Rotate, Scale, and Translate Bitmap on Canvas in OnTounchEvent

How to draw a bitmap file on a canvas in html?

Unity - How to block a canvas when another canvas is on

How to draw a bitmap on a canvas, respecting alpha values of the bitmap?

How to Drag a bitmap over another Bitmap on Canvas with Surfaceview in Android

How can I save a Turtle canvas as an image (.png or .jpg) in Python 3

Trying to figure out how to make the canvas smaller for python turtle graphics but it is not working correctly

Android get bitmap from camera, scale canvas to bitmap size and save bitmap

OnDraw : Canvas is not drawing bitmap

Binding Writeable bitmap to canvas