Peano
Loading...
Searching...
No Matches
create-durham-logo.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3"""!
4
5Create DurhamLogo.h and DurhamLogo.cpp for the tutorial 11_tsunami.ipynb
6
7The script downloads the shield of Durham University and writes it as a C++
8array of strings into the directory of this script: one string per row of
9pixels, starting with the top row, with a 1 for each purple pixel and a 0
10for each white one. The tutorial uses the array as depth map. The two
11generated files are part of the repository, so the tutorial runs without
12internet access. Rerun the script only if you want to change the depth map:
13
14 python3 tutorials/exahype2/bathymetry/create-durham-logo.py
15
16"""
17import os
18import urllib.request
19
20import matplotlib.image
21
22Here = os.path.dirname(os.path.abspath(__file__))
23
24LogoURL = "https://www.topuniversities.com/sites/default/files/profiles/logos/durham-university_170_large.jpg"
25
26Header = """// This file is part of the Peano project. For conditions of distribution and
27// use, please see the copyright notice at www.peano-framework.org
28//
29// Generated by create-durham-logo.py. Do not edit.
30#pragma once
31
32namespace tutorials::exahype2::tsunami {{
33 /**
34 * Number of pixels per axis of DurhamLogo
35 */
36 constexpr int DurhamLogoSize = {size};
37
38 /**
39 * The shield of Durham University as depth map
40 *
41 * One string per row of pixels, starting with the top row of the image,
42 * i.e. DurhamLogo[row][column]. A '1' marks a purple pixel, a '0' a white
43 * one. The tutorial 11_tsunami.ipynb maps the image onto its domain and
44 * uses it as depth map: the water is deeper where the logo is purple.
45 *
46 * Created from
47 *
48 * {url}
49 *
50 * by create-durham-logo.py.
51 */
52 extern const char* const DurhamLogo[DurhamLogoSize];
53}} // namespace tutorials::exahype2::tsunami
54"""
55
56Implementation = """// This file is part of the Peano project. For conditions of distribution and
57// use, please see the copyright notice at www.peano-framework.org
58//
59// Generated by create-durham-logo.py. Do not edit.
60#include "DurhamLogo.h"
61
62const char* const tutorials::exahype2::tsunami::DurhamLogo[DurhamLogoSize] = {{
63{rows}
64}};
65"""
66
67
68if __name__ == "__main__":
69 # The server rejects requests without a browser-like User-Agent
70 request = urllib.request.Request(LogoURL, headers={"User-Agent": "Mozilla/5.0"})
71 logo = matplotlib.image.imread(urllib.request.urlopen(request), format="jpg") / 255.0
72 purple = logo.mean(axis=2) < 0.7
73 assert purple.shape[0] == purple.shape[1], "We expect a square logo"
74
75 rows = ",\n".join(
76 ' "' + "".join("1" if pixel else "0" for pixel in row) + '"' for row in purple
77 )
78 with open(os.path.join(Here, "DurhamLogo.h"), "w") as f:
79 f.write(Header.format(size=purple.shape[0], url=LogoURL))
80 with open(os.path.join(Here, "DurhamLogo.cpp"), "w") as f:
81 f.write(Implementation.format(rows=rows))
82 print("wrote DurhamLogo.h and DurhamLogo.cpp into {}".format(Here))