将python turtle canvas转换为位图时如何保持画布大小 [英] how to maintain canvas size when converting python turtle canvas to bitmap

查看:433
本文介绍了将python turtle canvas转换为位图时如何保持画布大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Python Turtle模块(tkinter)画布转换为位图.我已按照"

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

在上面的代码中,画布为500x500.但是文件test.bmp缩小为374x374,并且其图像小于屏幕上的龟图形.如何获得未缩小的500x500位图?

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?

推荐答案

如果我们先分析后记的内容,就会发现它对画布的尺寸进行了0.7498的缩放

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

在对后记进行一些挖掘之后,我从tkinter的画布上找到了有关后记转换的perl/Tk参考指南

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

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

结果:

如果您有任何问题,请随时发表评论,我会尽力答复!

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

PS:

不要问我关于-1部分的问题,它不会给我501x501像素以外的任何东西,因此我对此进行了补偿.我不知道为什么它仍然不起作用.

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.

这篇关于将python turtle canvas转换为位图时如何保持画布大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆