-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathutils_pv.py
More file actions
58 lines (44 loc) · 1.41 KB
/
utils_pv.py
File metadata and controls
58 lines (44 loc) · 1.41 KB
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
"""
Collection of small utils to create figures with PyVista:
- Setup nice default parameters.
- Trim the output PNG files.
"""
__author__ = "Thomas Guillod"
__copyright__ = "Thomas Guillod - Dartmouth College"
__license__ = "Mozilla Public License Version 2.0"
import pyvista as pv
import PIL.Image as img
def set_global():
"""
Set the global PyVista options.
"""
pv.set_plot_theme("document")
pv.global_theme.transparent_background = True
pv.global_theme.show_scalar_bar = False
pv.global_theme.show_vertices = False
pv.global_theme.show_edges = False
pv.render_points_as_spheres = True
def get_crop(filename, margin=0):
"""
Function for removing the transparent border from images.
The original file is overwritten.
Parameters
----------
filename : str
Path of the filename to be cropped.
margin : int
Margin (in pixels) for the cropping.
"""
# open the image
input_img = img.open(filename)
# get the bounding box and crop
bbox = input_img.getbbox()
image = input_img.crop(bbox)
# get the new size
(width, height) = image.size
# create the new image
output_img = img.new("RGBA", (width + 2 * margin, height + 2 * margin), (0, 0, 0, 0))
# add the cropped image with the margin as offset
output_img.paste(image, (margin, margin))
# overwrite the original image
output_img.save(filename)