Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Flatland
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pranjal_dhole
Flatland
Commits
2336434c
Commit
2336434c
authored
4 years ago
by
spmohanty
Browse files
Options
Downloads
Patches
Plain Diff
Remove cairo support - again -_-
parent
5c4db475
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
flatland/utils/graphics_pil.py
+92
-108
92 additions, 108 deletions
flatland/utils/graphics_pil.py
with
92 additions
and
108 deletions
flatland/utils/graphics_pil.py
+
92
−
108
View file @
2336434c
import
io
import
os
import
time
#import tkinter as tk
import
numpy
as
np
from
PIL
import
Image
,
ImageDraw
,
ImageFont
...
...
@@ -9,26 +10,14 @@ from pkg_resources import resource_string as resource_bytes
from
flatland.utils.graphics_layer
import
GraphicsLayer
def
enable_windows_cairo_support
():
if
os
.
name
==
'
nt
'
:
import
site
import
ctypes.util
default_os_path
=
os
.
environ
[
'
PATH
'
]
os
.
environ
[
'
PATH
'
]
=
''
for
s
in
site
.
getsitepackages
():
os
.
environ
[
'
PATH
'
]
=
os
.
environ
[
'
PATH
'
]
+
'
;
'
+
s
+
'
\\
cairo
'
os
.
environ
[
'
PATH
'
]
=
os
.
environ
[
'
PATH
'
]
+
'
;
'
+
default_os_path
if
ctypes
.
util
.
find_library
(
'
cairo
'
)
is
None
:
print
(
"
Error: cairo not installed
"
)
enable_windows_cairo_support
()
from
cairosvg
import
svg2png
# noqa: E402
from
flatland.core.grid.rail_env_grid
import
RailEnvTransitions
# noqa: E402
class
PILGL
(
GraphicsLayer
):
# tk.Tk() must be a singleton!
# https://stackoverflow.com/questions/26097811/image-pyimage2-doesnt-exist
# window = tk.Tk()
RAIL_LAYER
=
0
PREDICTION_PATH_LAYER
=
1
TARGET_LAYER
=
2
...
...
@@ -279,104 +268,99 @@ class PILSVG(PILGL):
def
pil_from_svg_file
(
self
,
package
,
resource
):
bytestring
=
resource_bytes
(
package
,
resource
)
bytesPNG
=
svg2png
(
bytestring
=
bytestring
,
output_height
=
self
.
nPixCell
,
output_width
=
self
.
nPixCell
)
with
io
.
BytesIO
(
bytesPNG
)
as
fIn
:
with
io
.
BytesIO
(
bytestring
)
as
fIn
:
pil_img
=
Image
.
open
(
fIn
)
pil_img
.
load
()
pil_img
=
pil_img
.
resize
(
(
self
.
nPixCell
,
self
.
nPixCell
),
Image
.
ANTIALIAS
)
return
pil_img
def
pil_from_svg_bytes
(
self
,
bytesSVG
):
bytesPNG
=
svg2png
(
bytesSVG
,
output_height
=
self
.
nPixCell
,
output_width
=
self
.
nPixCell
)
with
io
.
BytesIO
(
bytesPNG
)
as
fIn
:
pil_img
=
Image
.
open
(
fIn
)
return
pil_img
def
load_buildings
(
self
):
lBuildingFiles
=
[
"
Buildings-Bank.
sv
g
"
,
"
Buildings-Bar.
sv
g
"
,
"
Buildings-Wohnhaus.
sv
g
"
,
"
Buildings-Hochhaus.
sv
g
"
,
"
Buildings-Hotel.
sv
g
"
,
"
Buildings-Office.
sv
g
"
,
"
Buildings-Polizei.
sv
g
"
,
"
Buildings-Post.
sv
g
"
,
"
Buildings-Supermarkt.
sv
g
"
,
"
Buildings-Tankstelle.
sv
g
"
,
"
Buildings-Fabrik_A.
sv
g
"
,
"
Buildings-Fabrik_B.
sv
g
"
,
"
Buildings-Fabrik_C.
sv
g
"
,
"
Buildings-Fabrik_D.
sv
g
"
,
"
Buildings-Fabrik_E.
sv
g
"
,
"
Buildings-Fabrik_F.
sv
g
"
,
"
Buildings-Fabrik_G.
sv
g
"
,
"
Buildings-Fabrik_H.
sv
g
"
,
"
Buildings-Fabrik_I.
sv
g
"
"
Buildings-Bank.
pn
g
"
,
"
Buildings-Bar.
pn
g
"
,
"
Buildings-Wohnhaus.
pn
g
"
,
"
Buildings-Hochhaus.
pn
g
"
,
"
Buildings-Hotel.
pn
g
"
,
"
Buildings-Office.
pn
g
"
,
"
Buildings-Polizei.
pn
g
"
,
"
Buildings-Post.
pn
g
"
,
"
Buildings-Supermarkt.
pn
g
"
,
"
Buildings-Tankstelle.
pn
g
"
,
"
Buildings-Fabrik_A.
pn
g
"
,
"
Buildings-Fabrik_B.
pn
g
"
,
"
Buildings-Fabrik_C.
pn
g
"
,
"
Buildings-Fabrik_D.
pn
g
"
,
"
Buildings-Fabrik_E.
pn
g
"
,
"
Buildings-Fabrik_F.
pn
g
"
,
"
Buildings-Fabrik_G.
pn
g
"
,
"
Buildings-Fabrik_H.
pn
g
"
,
"
Buildings-Fabrik_I.
pn
g
"
]
imgBg
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Background_city.
sv
g
"
)
imgBg
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Background_city.
pn
g
"
)
imgBg
=
imgBg
.
convert
(
"
RGBA
"
)
#print("imgBg mode:", imgBg.mode)
self
.
lBuildings
=
[]
for
sFile
in
lBuildingFiles
:
#print("Loading:", sFile)
img
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
sFile
)
img
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
sFile
)
#print("img mode:", img.mode)
img
=
Image
.
alpha_composite
(
imgBg
,
img
)
self
.
lBuildings
.
append
(
img
)
def
load_scenery
(
self
):
scenery_files
=
[
"
Scenery-Laubbaume_A.
sv
g
"
,
"
Scenery-Laubbaume_B.
sv
g
"
,
"
Scenery-Laubbaume_C.
sv
g
"
,
"
Scenery-Nadelbaume_A.
sv
g
"
,
"
Scenery-Nadelbaume_B.
sv
g
"
,
"
Scenery-Bergwelt_B.
sv
g
"
"
Scenery-Laubbaume_A.
pn
g
"
,
"
Scenery-Laubbaume_B.
pn
g
"
,
"
Scenery-Laubbaume_C.
pn
g
"
,
"
Scenery-Nadelbaume_A.
pn
g
"
,
"
Scenery-Nadelbaume_B.
pn
g
"
,
"
Scenery-Bergwelt_B.
pn
g
"
]
scenery_files_d2
=
[
"
Scenery-Bergwelt_C_Teil_1_links.
sv
g
"
,
"
Scenery-Bergwelt_C_Teil_2_rechts.
sv
g
"
"
Scenery-Bergwelt_C_Teil_1_links.
pn
g
"
,
"
Scenery-Bergwelt_C_Teil_2_rechts.
pn
g
"
]
scenery_files_d3
=
[
"
Scenery-Bergwelt_A_Teil_1_links.
sv
g
"
,
"
Scenery-Bergwelt_A_Teil_2_mitte.
sv
g
"
,
"
Scenery-Bergwelt_A_Teil_3_rechts.
sv
g
"
"
Scenery-Bergwelt_A_Teil_1_links.
pn
g
"
,
"
Scenery-Bergwelt_A_Teil_2_mitte.
pn
g
"
,
"
Scenery-Bergwelt_A_Teil_3_rechts.
pn
g
"
]
scenery_files_water
=
[
"
Scenery_Water.
sv
g
"
"
Scenery_Water.
pn
g
"
]
img_back_ground
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Background_Light_green.
sv
g
"
).
convert
(
"
RGBA
"
)
img_back_ground
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Background_Light_green.
pn
g
"
).
convert
(
"
RGBA
"
)
self
.
scenery_background_white
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Background_white.
sv
g
"
).
convert
(
"
RGBA
"
)
self
.
scenery_background_white
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Background_white.
pn
g
"
).
convert
(
"
RGBA
"
)
self
.
scenery
=
[]
for
file
in
scenery_files
:
img
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
file
)
img
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
file
)
img
=
Image
.
alpha_composite
(
img_back_ground
,
img
)
self
.
scenery
.
append
(
img
)
self
.
scenery_d2
=
[]
for
file
in
scenery_files_d2
:
img
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
file
)
img
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
file
)
img
=
Image
.
alpha_composite
(
img_back_ground
,
img
)
self
.
scenery_d2
.
append
(
img
)
self
.
scenery_d3
=
[]
for
file
in
scenery_files_d3
:
img
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
file
)
img
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
file
)
img
=
Image
.
alpha_composite
(
img_back_ground
,
img
)
self
.
scenery_d3
.
append
(
img
)
self
.
scenery_water
=
[]
for
file
in
scenery_files_water
:
img
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
file
)
img
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
file
)
img
=
Image
.
alpha_composite
(
img_back_ground
,
img
)
self
.
scenery_water
.
append
(
img
)
...
...
@@ -384,55 +368,55 @@ class PILSVG(PILGL):
"""
Load the rail SVG images, apply rotations, and store as PIL images.
"""
rail_files
=
{
""
:
"
Background_Light_green.
sv
g
"
,
"
WE
"
:
"
Gleis_Deadend.
sv
g
"
,
"
WW EE NN SS
"
:
"
Gleis_Diamond_Crossing.
sv
g
"
,
"
WW EE
"
:
"
Gleis_horizontal.
sv
g
"
,
"
EN SW
"
:
"
Gleis_Kurve_oben_links.
sv
g
"
,
"
WN SE
"
:
"
Gleis_Kurve_oben_rechts.
sv
g
"
,
"
ES NW
"
:
"
Gleis_Kurve_unten_links.
sv
g
"
,
"
NE WS
"
:
"
Gleis_Kurve_unten_rechts.
sv
g
"
,
"
NN SS
"
:
"
Gleis_vertikal.
sv
g
"
,
"
NN SS EE WW ES NW SE WN
"
:
"
Weiche_Double_Slip.
sv
g
"
,
"
EE WW EN SW
"
:
"
Weiche_horizontal_oben_links.
sv
g
"
,
"
EE WW SE WN
"
:
"
Weiche_horizontal_oben_rechts.
sv
g
"
,
"
EE WW ES NW
"
:
"
Weiche_horizontal_unten_links.
sv
g
"
,
"
EE WW NE WS
"
:
"
Weiche_horizontal_unten_rechts.
sv
g
"
,
"
NN SS EE WW NW ES
"
:
"
Weiche_Single_Slip.
sv
g
"
,
"
NE NW ES WS
"
:
"
Weiche_Symetrical.
sv
g
"
,
"
NN SS EN SW
"
:
"
Weiche_vertikal_oben_links.
sv
g
"
,
"
NN SS SE WN
"
:
"
Weiche_vertikal_oben_rechts.
sv
g
"
,
"
NN SS NW ES
"
:
"
Weiche_vertikal_unten_links.
sv
g
"
,
"
NN SS NE WS
"
:
"
Weiche_vertikal_unten_rechts.
sv
g
"
,
"
NE NW ES WS SS NN
"
:
"
Weiche_Symetrical_gerade.
sv
g
"
,
"
NE EN SW WS
"
:
"
Gleis_Kurve_oben_links_unten_rechts.
sv
g
"
""
:
"
Background_Light_green.
pn
g
"
,
"
WE
"
:
"
Gleis_Deadend.
pn
g
"
,
"
WW EE NN SS
"
:
"
Gleis_Diamond_Crossing.
pn
g
"
,
"
WW EE
"
:
"
Gleis_horizontal.
pn
g
"
,
"
EN SW
"
:
"
Gleis_Kurve_oben_links.
pn
g
"
,
"
WN SE
"
:
"
Gleis_Kurve_oben_rechts.
pn
g
"
,
"
ES NW
"
:
"
Gleis_Kurve_unten_links.
pn
g
"
,
"
NE WS
"
:
"
Gleis_Kurve_unten_rechts.
pn
g
"
,
"
NN SS
"
:
"
Gleis_vertikal.
pn
g
"
,
"
NN SS EE WW ES NW SE WN
"
:
"
Weiche_Double_Slip.
pn
g
"
,
"
EE WW EN SW
"
:
"
Weiche_horizontal_oben_links.
pn
g
"
,
"
EE WW SE WN
"
:
"
Weiche_horizontal_oben_rechts.
pn
g
"
,
"
EE WW ES NW
"
:
"
Weiche_horizontal_unten_links.
pn
g
"
,
"
EE WW NE WS
"
:
"
Weiche_horizontal_unten_rechts.
pn
g
"
,
"
NN SS EE WW NW ES
"
:
"
Weiche_Single_Slip.
pn
g
"
,
"
NE NW ES WS
"
:
"
Weiche_Symetrical.
pn
g
"
,
"
NN SS EN SW
"
:
"
Weiche_vertikal_oben_links.
pn
g
"
,
"
NN SS SE WN
"
:
"
Weiche_vertikal_oben_rechts.
pn
g
"
,
"
NN SS NW ES
"
:
"
Weiche_vertikal_unten_links.
pn
g
"
,
"
NN SS NE WS
"
:
"
Weiche_vertikal_unten_rechts.
pn
g
"
,
"
NE NW ES WS SS NN
"
:
"
Weiche_Symetrical_gerade.
pn
g
"
,
"
NE EN SW WS
"
:
"
Gleis_Kurve_oben_links_unten_rechts.
pn
g
"
}
target_files
=
{
"
EW
"
:
"
Bahnhof_#d50000_Deadend_links.
sv
g
"
,
"
NS
"
:
"
Bahnhof_#d50000_Deadend_oben.
sv
g
"
,
"
WE
"
:
"
Bahnhof_#d50000_Deadend_rechts.
sv
g
"
,
"
SN
"
:
"
Bahnhof_#d50000_Deadend_unten.
sv
g
"
,
"
EE WW
"
:
"
Bahnhof_#d50000_Gleis_horizontal.
sv
g
"
,
"
NN SS
"
:
"
Bahnhof_#d50000_Gleis_vertikal.
sv
g
"
}
"
EW
"
:
"
Bahnhof_#d50000_Deadend_links.
pn
g
"
,
"
NS
"
:
"
Bahnhof_#d50000_Deadend_oben.
pn
g
"
,
"
WE
"
:
"
Bahnhof_#d50000_Deadend_rechts.
pn
g
"
,
"
SN
"
:
"
Bahnhof_#d50000_Deadend_unten.
pn
g
"
,
"
EE WW
"
:
"
Bahnhof_#d50000_Gleis_horizontal.
pn
g
"
,
"
NN SS
"
:
"
Bahnhof_#d50000_Gleis_vertikal.
pn
g
"
}
# Dict of rail cell images indexed by binary transitions
pil_rail_files_org
=
self
.
load_svgs
(
rail_files
,
rotate
=
True
)
pil_rail_files
=
self
.
load_svgs
(
rail_files
,
rotate
=
True
,
background_image
=
"
Background_rail.
sv
g
"
,
whitefilter
=
"
Background_white_filter.
sv
g
"
)
pil_rail_files
=
self
.
load_svgs
(
rail_files
,
rotate
=
True
,
background_image
=
"
Background_rail.
pn
g
"
,
whitefilter
=
"
Background_white_filter.
pn
g
"
)
# Load the target files (which have rails and transitions of their own)
# They are indexed by (binTrans, iAgent), ie a tuple of the binary transition and the agent index
pil_target_files_org
=
self
.
load_svgs
(
target_files
,
rotate
=
False
,
agent_colors
=
self
.
agent_colors
)
pil_target_files
=
self
.
load_svgs
(
target_files
,
rotate
=
False
,
agent_colors
=
self
.
agent_colors
,
background_image
=
"
Background_rail.
sv
g
"
,
whitefilter
=
"
Background_white_filter.
sv
g
"
)
background_image
=
"
Background_rail.
pn
g
"
,
whitefilter
=
"
Background_white_filter.
pn
g
"
)
# Load station and recolorize them
station
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Bahnhof_#d50000_target.
sv
g
"
)
station
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Bahnhof_#d50000_target.
pn
g
"
)
self
.
station_colors
=
self
.
recolor_image
(
station
,
[
0
,
0
,
0
],
self
.
agent_colors
,
False
)
cell_occupied
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Cell_occupied.
sv
g
"
)
cell_occupied
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Cell_occupied.
pn
g
"
)
self
.
cell_occupied
=
self
.
recolor_image
(
cell_occupied
,
[
0
,
0
,
0
],
self
.
agent_colors
,
False
)
# Merge them with the regular rails.
...
...
@@ -461,14 +445,14 @@ class PILSVG(PILGL):
transition_16_bit_string
=
""
.
join
(
transition_16_bit
)
binary_trans
=
int
(
transition_16_bit_string
,
2
)
pil_rail
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
file
).
convert
(
"
RGBA
"
)
pil_rail
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
file
).
convert
(
"
RGBA
"
)
if
background_image
is
not
None
:
img_bg
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
background_image
).
convert
(
"
RGBA
"
)
img_bg
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
background_image
).
convert
(
"
RGBA
"
)
pil_rail
=
Image
.
alpha_composite
(
img_bg
,
pil_rail
)
if
whitefilter
is
not
None
:
img_bg
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
whitefilter
).
convert
(
"
RGBA
"
)
img_bg
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
whitefilter
).
convert
(
"
RGBA
"
)
pil_rail
=
Image
.
alpha_composite
(
pil_rail
,
img_bg
)
if
rotate
:
...
...
@@ -560,7 +544,7 @@ class PILSVG(PILGL):
if
target
is
not
None
:
if
is_selected
:
svgBG
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Selected_Target.
sv
g
"
)
svgBG
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Selected_Target.
pn
g
"
)
self
.
clear_layer
(
PILGL
.
SELECTED_TARGET_LAYER
,
0
)
self
.
draw_image_row_col
(
svgBG
,
(
row
,
col
),
layer
=
PILGL
.
SELECTED_TARGET_LAYER
)
...
...
@@ -585,9 +569,9 @@ class PILSVG(PILGL):
# Seed initial train/zug files indexed by tuple(iDirIn, iDirOut):
file_directory
=
{
(
0
,
0
):
"
Zug_Gleis_#0091ea.
sv
g
"
,
(
1
,
2
):
"
Zug_1_Weiche_#0091ea.
sv
g
"
,
(
0
,
3
):
"
Zug_2_Weiche_#0091ea.
sv
g
"
(
0
,
0
):
"
Zug_Gleis_#0091ea.
pn
g
"
,
(
1
,
2
):
"
Zug_1_Weiche_#0091ea.
pn
g
"
,
(
0
,
3
):
"
Zug_2_Weiche_#0091ea.
pn
g
"
}
# "paint" color of the train images we load - this is the color we will change.
...
...
@@ -600,7 +584,7 @@ class PILSVG(PILGL):
for
directions
,
path_svg
in
file_directory
.
items
():
in_direction
,
out_direction
=
directions
pil_zug
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
path_svg
)
pil_zug
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
path_svg
)
# Rotate both the directions and the image and save in the dict
for
rot_direction
in
range
(
4
):
...
...
@@ -630,7 +614,7 @@ class PILSVG(PILGL):
self
.
draw_image_row_col
(
self
.
scenery_background_white
,
(
row
,
col
),
layer
=
PILGL
.
RAIL_LAYER
)
if
is_selected
:
bg_svg
=
self
.
pil_from_svg_file
(
'
flatland.
sv
g
'
,
"
Selected_Agent.
sv
g
"
)
bg_svg
=
self
.
pil_from_svg_file
(
'
flatland.
pn
g
'
,
"
Selected_Agent.
pn
g
"
)
self
.
clear_layer
(
PILGL
.
SELECTED_AGENT_LAYER
,
0
)
self
.
draw_image_row_col
(
bg_svg
,
(
row
,
col
),
layer
=
PILGL
.
SELECTED_AGENT_LAYER
)
if
show_debug
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment