将海龟输出保存为 jpeg [英] Save turtle output as jpeg

查看:32
本文介绍了将海龟输出保存为 jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分形图像创建器.它创建了一个随机的分形树之类的东西.完成后,它会提示用户保存树.我现在将它保存为 .svg 并且可以使用,但我希望将其保存为更方便的文件类型,例如 jpeg.有任何想法吗?代码:

I have a fractal image creator. It creates a random fractal tree like thing. When done, it prompts the user to save the tree. I have it saving as a .svg right now and that works BUT I want it to save to a more convenient file type, like jpeg. Any ideas? Code:

import turtle
import random
from sys import exit
from time import clock
import canvasvg
turtle.colormode(255)
red = 125
green = 70
blue = 38        
pen = 10
def saveImg():
    print("Done.")
    save = input("Would you like to save this tree? Y/N \n")
    if save.upper() == "Y":
        t.hideturtle()
        name = input("What would you like to name it? \n")
        nameSav = name + ".svg"
        ts = turtle.getscreen().getcanvas()
        canvasvg.saveall(nameSav, ts)
    elif save.upper() == "N":
        def runChk():
            runAgain = input("Would you like to run again? Y/N (N will exit)")
            if runAgain.upper() == "Y":
                print("Running")
                main()
            elif runAgain.upper() == "N":
                print("Exiting...")
                exit()
            else:
                print("Invalid response.")
                runChk()
        runChk()
    else:
        print("Invalid response.")
        saveImg()

def tree(branchLen, t, red, green, blue, pen):
    time = str(round(clock()))
    print("Drawing... " + time)
    if branchLen > 3:
        pen = pen*0.8
        t.pensize(pen)
        if (red > 10 and green < 140):
            red = red - 15
            green = green + 8
    if branchLen > 5:
        angle = random.randrange(18, 55)
        angleTwo = 0.5*angle
        sub = random.randrange(1,16)
        t.color(red, green, blue)
        t.forward(branchLen)
        t.right(angleTwo)
        tree(branchLen-sub,t, red, green, blue, pen)
        t.left(angle)
        tree(branchLen-sub, t, red, green, blue, pen)
        t.right(angleTwo)
        t.backward(branchLen)

def main():
    t = turtle.Turtle()
    myWin = turtle.Screen()
    t.speed(0)
    t.hideturtle()
    t.left(90)
    t.up()
    t.backward(100)
    t.down()
    print("Please wait while I draw...")
    tree(random.randrange(60,95),t,red,green,blue, pen)
    saveImg()
main()

推荐答案

它必须是 JPEG 吗?PNG 就足够了吗?

Does it have to be a JPEG? Would PNG suffice?

如果是这样,您可以使用 cairosvg 从 SVG 转换为 PNG.不幸的是,canvasvg.saveall() 只允许您向它传递一个文件名,它将写入 SVG,因此您需要为 SVG 使用一个临时文件,然后将该临时文件转换为 PNG使用 cairosvg.svg2png().所以应该做这样的事情:

If so you can convert from SVG to PNG using cairosvg. Unfortunately canvasvg.saveall() only allows you to pass it a file name into which it will write the SVG, so you will need to use a temporary file for the SVG and then convert that temp file to PNG using cairosvg.svg2png(). So something like this should do the job:

import os
import shutil
import tempfile

import canvasvg

name = raw_input("What would you like to name it? \n")
nameSav = name + ".png"
tmpdir = tempfile.mkdtemp()  # create a temporary directory
tmpfile = os.path.join(tmpdir, 'tmp.svg')  # name of file to save SVG to
ts = turtle.getscreen().getcanvas()
canvasvg.saveall(tmpfile, ts)
with open(tmpfile) as svg_input, open(nameSav, 'wb') as png_output:
    cairosvg.svg2png(bytestring=svg_input.read(), write_to=png_output)
shutil.rmtree(tmpdir)  # clean up temp file(s)

如果你愿意,你可以基于 canvasvg.saveall()(它非常小)编写你自己的 saveall() 函数,它接受一个类似文件的对象文件名,并写入该对象.然后你可以传入一个 StringIO 对象,而不必费心处理临时文件.或者您的 saveall() 可以将 SVG 数据作为字节字符串返回.

If you liked, you could write your own saveall() function based on canvasvg.saveall() (it's quite small) that accepts a file-like object instead of a file name, and writes to that object. Then you can pass in a StringIO object and not have to bother with the temporary file. Or your saveall() could just return the SVG data as a byte string.

这篇关于将海龟输出保存为 jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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