【问题标题】:Returning SVG object or XML string instead of an Image返回 SVG 对象或 XML 字符串而不是图像
【发布时间】:2017-09-27 11:10:47
【问题描述】:

大家好,我是一名初级程序员,在这个特定的应用程序中遇到了麻烦。

我想从 python-chess 库中渲染一个棋盘,它总是返回一个 SVG xml 字符串而不是图像。这是文档python-chess

>>> chess.svg.piece(chess.Piece.from_symbol("R"))
'<svg version="1.1" viewBox="0 0 45 45" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g class="white rook" fill="#fff" fill-rule="evenodd" id="white-rook" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path d="M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5" stroke-linecap="butt" /><path d="M34 14l-3 3H14l-3-3" /><path d="M31 17v12.5H14V17" stroke-linecap="butt" stroke-linejoin="miter" /><path d="M31 29.5l1.5 2.5h-20l1.5-2.5" /><path d="M11 14h23" fill="none" stroke-linejoin="miter" /></g></svg>'

我尝试用 svgwrite 渲染它,但做不到。

在 python 中有没有其他方法可以直接将所述 XML 渲染为图像并显示在屏幕上?

还尝试使用 IPython.display 模块来尝试显示 SVG,但它总是返回一个对象而不是图片。指引我正确的方向

非常感谢您

【问题讨论】:

  • 您的问题应该提到您正在使用哪个 GUI 框架。 FWIW,在 pygame here 中有一个渲染 SVG 的示例。请注意,它使用了几个其他库来实际处理 SVG 数据。
  • FWIW,您可以在浏览器中显示 SVG。如果 SVG 数据位于名为 svg_data 的字符串中,这将在 FireFox 中将其显示为数据 URI。 import base64, webbrowserwebbrowser.get('firefox').open('data:image/svg+xml;base64,' + base64.encodestring(svg_data.encode()).decode())

标签: python python-3.x svg


【解决方案1】:

试试 Pygame。它不仅可以让您显示图像,还可以帮助您移动它们并与它们进行交互。如果您打算制作国际象棋游戏,我相信 Pygame 会有所帮助!如果你真的想坚持使用 SVG,那么......我想我只是想帮忙。 如果您想要示例代码,请在下面发表评论。希望这会有所帮助!

好的,我已经完成了。到目前为止,游戏允许您将棋子移动到允许的位置。但是剩下的事情我会留给你,因为这需要一些时间。如果您在 pygame 中需要任何指导,请再次发表评论,我会尽力提供帮助。代码如下:

import pygame, time, sys, random
from pygame.locals import *

# Initialize pygame (this is nessseccary for it to run all it's commands)
pygame.init()

# Draw the screen. The width and the height define how long and how wide the window is
width = 600
height = 500
window = pygame.display.set_mode((width, height))
TRANSwindow = window.convert_alpha()

# this sets the window title to "Chess!"
pygame.display.set_caption("Chess!")

# define some colours that we might need for later decoration
aqua = 0, 255, 255
black = 0, 0, 0
blue = 0, 0, 255
fuchsia = 255, 0, 255
gray = 128, 128, 128
green = 0, 128, 0
lime = 0, 255, 0
maroon = 128, 0, 0
navy_blue = 0, 0, 128
olive = 128, 128, 0
purple = 128, 0, 128
red = 255, 0, 0
silver = 192, 192, 192
teal = 0, 128, 128
white = 255, 255, 255
yellow = 255, 255, 0


# Make a group that contains the sprite pieces that we need
pieces = pygame.sprite.Group()

# Make a group that contains the grid called "board" that we need
grid = pygame.sprite.Group()

# Make a group that contains the allowed click spaces called "ClickSpaces" that we need
ClickSpaces = pygame.sprite.Group()

class Board(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("yoHZB.png")
        self.image = pygame.transform.scale(self.image,(600,500))
        self.rect = self.image.get_rect()

class Piece(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("Hr7mM.png")
        self.image = pygame.transform.scale(self.image,(75,65))
        self.rect = self.image.get_rect()
class ClickSpace(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("I6rbE.png")
        self.image = pygame.transform.scale(self.image, (75, 65))
        self.rect = self.image.get_rect()

for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 0
    pieces.add(piece)
for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 65
    pieces.add(piece)
for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 380
    pieces.add(piece)
for i in range(8):
    piece = Piece()
    piece.rect.x = i*75
    piece.rect.y = 440
    pieces.add(piece)

board = Board()
grid.add(board)


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        elif event.type == MOUSEBUTTONUP:
            mouse = pygame.mouse.get_pos()
            for piece in pieces:
                if piece.rect.collidepoint(mouse):
                    print("clicked")
                    for i in range(2):
                        clickspace = ClickSpace()
                        clickspace.rect.x = piece.rect.left
                        clickspace.rect.y = piece.rect.top+((i+1)*65)
                        ClickSpaces.add(clickspace)
                        print("added clickspace")
                    ClickSpaces.draw(window)
                    pygame.display.update()
                    wait = True
                    while wait == True:
                        mouse = pygame.mouse.get_pos()
                        for event in pygame.event.get():
                            if event.type == QUIT:
                                pygame.quit()
                                exit()
                            elif event.type == MOUSEBUTTONUP:
                                mouse = pygame.mouse.get_pos()
                                for clickspace in ClickSpaces:
                                    if clickspace.rect.collidepoint(mouse):
                                        print("clickspace clicked")
                                        piece.rect.x = clickspace.rect.left
                                        piece.rect.y = clickspace.rect.top
                                        ClickSpaces.empty()
                                        wait = False
                                    else:
                                        ClickSpaces.empty()
                                        wait = False


    window.fill(white)
    grid.draw(window)
    pieces.draw(window)
    ClickSpaces.draw(window)
    pygame.display.update()

一旦你复制了代码并将其放入 python 中。保存项目,然后下载以下图片并将它们与您的代码放在同一个文件中:

【讨论】:

  • 是的,国际象棋游戏是我想做的。肯定可以在 pygame 中使用一些示例代码。
  • 顺便说一句,你有 pygame 模块吗?没有它,这将无法工作。
  • 是的,我以前用过 Pygame,我一定会试试看的!
猜你喜欢
  • 1970-01-01
  • 2013-02-23
  • 2017-07-18
  • 2011-07-29
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多