0

Hey guys I'm a beginner level programmer and having trouble in this particular application.

I want to render a chess board from the python-chess library which is always returning me a SVG xml string instead of image. here is the documentation 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>'

I've tried to render this with svgwrite but couldn't do it.

Is there any other way in python i can directly render said XML to an image and display on the screen ?

Also tried using IPython.display module to try and display the SVG, But it always returned me an Object instead of a picture. Guide me in a proper direction

Thank you very much in advance

Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
  • Your question should mention which GUI framework you're using. FWIW, there's an example of rendering SVG in pygame [here](http://stackoverflow.com/questions/120584/svg-rendering-in-a-pygame-application). Note that it uses a couple of other libraries to actually handle the SVG data. – PM 2Ring Apr 29 '17 at 12:35
  • FWIW, you can display SVG in your browser. If the SVG data is in a string called `svg_data`, this'll display it in FireFox as a data URI. `import base64, webbrowser` `webbrowser.get('firefox').open('data:image/svg+xml;base64,' + base64.encodestring(svg_data.encode()).decode())` – PM 2Ring Apr 29 '17 at 12:39

1 Answers1

2

Try Pygame. It will not only allow you to display images, but it help you move them and interact with them. If you are planning to make a chess game, I'm sure Pygame will help! If you really want to stick to SVG then.... I guess I'm just trying to help. if you want an example code please comment below. Hope this helps!

Ok I'm done. The game so far allows you to move the pieces in their allowed places. But the rest I'll leave up to you, since it will take a bit of time. If you need any guidance in pygame just comment again and I'll try to help. Here is the code:

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

Once you copied the code and put it in python. Save the project then download the following images and put them in the same file as your code:

enter image description here

enter image description here

enter image description here