Python - Cambio de rsvg de python 2.7 a Rsvg de python 3 origina diferencias

 
Vista:

Cambio de rsvg de python 2.7 a Rsvg de python 3 origina diferencias

Publicado por Marco Toledo (2 intervenciones) el 07/04/2020 04:00:40
Este es el codigo en python 2.7:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
#Credito a https://stackoverflow.com/questions/120584/svg-rendering-in-a-pygame-application
import array
import math
 
import cairo
import pygame
import rsvg
 
WIDTH = 960
HEIGHT = 960
color = [0,0,0]
data = array.array('c', chr(0) * WIDTH * HEIGHT * 4)
print len(data)
 
surface = cairo.ImageSurface.create_for_data(
    data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT, WIDTH * 4)
#print surface
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
#print window
svg=[]
for k in range(60):
	svg.append(rsvg.Handle(file="./imggame/"+str(k)+".svg"))
	ctx = cairo.Context(surface)
	svg[k].render_cairo(ctx)
	screen = pygame.display.get_surface()
   	screen.fill(color)
	image = pygame.image.frombuffer(data.tostring(), (WIDTH, HEIGHT),"ARGB")
#	print len(image)
	screen.blit(image, (0,0))
	pygame.display.flip()
 
clock = pygame.time.Clock()
while True:
    clock.tick(15)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise SystemExit
 
Y este es el codigo en python 3, que origina un error entre el largo del buffer y el tamaño del formato y resolucion, que deben ser iguales. Intente cambiando la resolución de pygame, pero ahora creo que habría quecambiar el largo del buffer en 	image = pygame.image.frombuffer(data.tostring(), (WIDTH, HEIGHT),"ARGB")
 
#!/usr/bin/env python3
from PyQt5.QtWidgets import QMainWindow,QApplication,QWidget
from PyQt5.QtGui import QImage,QPainter
import pygame
import sys
import array
import math
import cairo
import pygame
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
 
 
WIDTH = 600
HEIGHT = 600
 
 
color = [0,0,0]
data = array.array('u', chr(0) * WIDTH * HEIGHT * 4)
print("largo ",len(data))
 
class MainWindow(QMainWindow):
    def __init__(self,surface,parent=None):
        super(MainWindow,self).__init__(parent)
        self.setCentralWidget(ImageWidget(surface))
        self.setGeometry(200,200,640,480)
 
surface = cairo.ImageSurface.create_for_data(
    data, cairo.FORMAT_ARGB32, WIDTH, HEIGHT, WIDTH *4)
 
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
 
svg=[]
for k in range(2):
    svg.append(Rsvg.Handle.new_from_file("./imggame/"+str(k)+".svg"))
    ctx = cairo.Context(surface)
    svg[k].render_cairo(ctx)
    screen = pygame.display.get_surface()
    resized_screen = pygame.transform.scale(screen, (WIDTH,HEIGHT))
    screen.fill(color)
    image = pygame.image.frombuffer(data.tostring(), (WIDTH, HEIGHT),"ARGB")
#1.pygame.image.frombuffer(string, size, format):          return Surface
 #      create a new Surface that shares data inside a string buffer
#1440000÷3686400
#    print(image)
 
    screen.blit(image, (0,0))
    pygame.display.flip()
 
clock = pygame.time.Clock()
while True:
    clock.tick(15)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise SystemExit
 
 
app=QApplication(sys.argv)
w=MainWindow()
w.show()
app.exec_()

Pongo una imagen de entre las que carga que son en total 106
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder