29
29
###############################################################################
30
30
31
31
32
- import os
33
32
import struct
34
33
35
34
import gdaltest
46
45
)
47
46
48
47
49
- @pytest .fixture ()
48
+ @pytest .fixture (scope = "module" )
50
49
def script_path ():
51
50
return test_py_scripts .get_py_script ("rgb2pct" )
52
51
53
52
54
- ###############################################################################
55
- # Test rgb2pct
53
+ @pytest .fixture (scope = "module" )
54
+ def rgb2pct1_tif (script_path , tmp_path_factory ):
55
+
56
+ tif_fname = str (tmp_path_factory .mktemp ("tmp" ) / "test_rgb2pct_1.tif" )
57
+
58
+ test_py_scripts .run_py_script (
59
+ script_path ,
60
+ "rgb2pct" ,
61
+ test_py_scripts .get_data_path ("gcore" ) + f"rgbsmall.tif { tif_fname } " ,
62
+ )
56
63
64
+ yield tif_fname
57
65
58
- def test_rgb2pct_1 (script_path ):
66
+
67
+ @pytest .fixture (scope = "module" )
68
+ def rgb2pct2_tif (script_path , tmp_path_factory ):
69
+
70
+ tif_fname = str (tmp_path_factory .mktemp ("tmp" ) / "test_rgb2pct_2.tif" )
59
71
60
72
test_py_scripts .run_py_script (
61
73
script_path ,
62
74
"rgb2pct" ,
63
- test_py_scripts .get_data_path ("gcore" ) + "rgbsmall.tif tmp/test_rgb2pct_1.tif " ,
75
+ "-n 16 " + test_py_scripts .get_data_path ("gcore" ) + f "rgbsmall.tif { tif_fname } " ,
64
76
)
65
77
66
- ds = gdal .Open ("tmp/test_rgb2pct_1.tif" )
67
- assert ds .GetRasterBand (1 ).Checksum () == 31231
68
- ds = None
78
+ yield tif_fname
79
+
80
+
81
+ ###############################################################################
82
+ # Test rgb2pct
83
+
84
+
85
+ def test_rgb2pct_1 (rgb2pct1_tif ):
86
+
87
+ with gdal .Open (rgb2pct1_tif ) as ds :
88
+ assert ds .GetRasterBand (1 ).Checksum () == 31231
69
89
70
90
71
91
###############################################################################
72
92
# Test pct2rgb
73
93
74
94
75
- def test_pct2rgb_1 (script_path ):
95
+ def test_pct2rgb_1 (script_path , tmp_path , rgb2pct1_tif ):
76
96
gdal_array = pytest .importorskip ("osgeo.gdal_array" )
77
97
try :
78
98
gdal_array .BandRasterIONumPy
79
99
except AttributeError :
80
100
pytest .skip ("osgeo.gdal_array.BandRasterIONumPy is unavailable" )
81
101
102
+ output_tif = str (tmp_path / "test_pct2rgb_1.tif" )
103
+
82
104
test_py_scripts .run_py_script (
83
- script_path , "pct2rgb" , "tmp/test_rgb2pct_1.tif tmp/test_pct2rgb_1.tif "
105
+ script_path , "pct2rgb" , f" { rgb2pct1_tif } { output_tif } "
84
106
)
85
107
86
- ds = gdal .Open ("tmp/test_pct2rgb_1.tif" )
108
+ ds = gdal .Open (output_tif )
87
109
assert ds .GetRasterBand (1 ).Checksum () == 20963
88
110
89
111
ori_ds = gdal .Open (test_py_scripts .get_data_path ("gcore" ) + "rgbsmall.tif" )
@@ -98,17 +120,9 @@ def test_pct2rgb_1(script_path):
98
120
# Test rgb2pct -n option
99
121
100
122
101
- def test_rgb2pct_2 (script_path ):
123
+ def test_rgb2pct_2 (script_path , rgb2pct2_tif ):
102
124
103
- test_py_scripts .run_py_script (
104
- script_path ,
105
- "rgb2pct" ,
106
- "-n 16 "
107
- + test_py_scripts .get_data_path ("gcore" )
108
- + "rgbsmall.tif tmp/test_rgb2pct_2.tif" ,
109
- )
110
-
111
- ds = gdal .Open ("tmp/test_rgb2pct_2.tif" )
125
+ ds = gdal .Open (rgb2pct2_tif )
112
126
assert ds .GetRasterBand (1 ).Checksum () == 16596
113
127
114
128
ct = ds .GetRasterBand (1 ).GetRasterColorTable ()
@@ -125,17 +139,19 @@ def test_rgb2pct_2(script_path):
125
139
# Test rgb2pct -pct option
126
140
127
141
128
- def test_rgb2pct_3 (script_path ):
142
+ def test_rgb2pct_3 (script_path , tmp_path , rgb2pct2_tif ):
143
+
144
+ output_tif = str (tmp_path / "test_rgb2pct_3.tif" )
129
145
130
146
test_py_scripts .run_py_script (
131
147
script_path ,
132
148
"rgb2pct" ,
133
- "-pct tmp/test_rgb2pct_2.tif "
149
+ f "-pct { rgb2pct2_tif } "
134
150
+ test_py_scripts .get_data_path ("gcore" )
135
- + "rgbsmall.tif tmp/test_rgb2pct_3.tif " ,
151
+ + f "rgbsmall.tif { output_tif } " ,
136
152
)
137
153
138
- ds = gdal .Open ("tmp/test_rgb2pct_3.tif" )
154
+ ds = gdal .Open (output_tif )
139
155
assert ds .GetRasterBand (1 ).Checksum () == 16596
140
156
141
157
ct = ds .GetRasterBand (1 ).GetRasterColorTable ()
@@ -152,22 +168,22 @@ def test_rgb2pct_3(script_path):
152
168
# Test pct2rgb with big CT (>256 entries)
153
169
154
170
155
- def test_pct2rgb_4 (script_path ):
171
+ def test_pct2rgb_4 (script_path , tmp_path ):
156
172
gdal_array = pytest .importorskip ("osgeo.gdal_array" )
157
173
try :
158
174
gdal_array .BandRasterIONumPy
159
175
except AttributeError :
160
176
pytest .skip ("osgeo.gdal_array.BandRasterIONumPy is unavailable" )
161
177
178
+ output_tif = str (tmp_path / "test_pct2rgb_4.tif" )
179
+
162
180
test_py_scripts .run_py_script (
163
181
script_path ,
164
182
"pct2rgb" ,
165
- "-rgba "
166
- + test_py_scripts .get_data_path ("gcore" )
167
- + "rat.img tmp/test_pct2rgb_4.tif" ,
183
+ "-rgba " + test_py_scripts .get_data_path ("gcore" ) + f"rat.img { output_tif } " ,
168
184
)
169
185
170
- ds = gdal .Open ("tmp/test_pct2rgb_4.tif" )
186
+ ds = gdal .Open (output_tif )
171
187
ori_ds = gdal .Open (test_py_scripts .get_data_path ("gcore" ) + "rat.img" )
172
188
173
189
ori_data = struct .unpack (
@@ -189,10 +205,10 @@ def test_pct2rgb_4(script_path):
189
205
ori_ds = None
190
206
191
207
192
- def test_gdalattachpct_1 ():
193
- pct_filename = "tmp/test_rgb2pct_2.tif"
208
+ def test_gdalattachpct_1 (tmp_path , rgb2pct2_tif ):
209
+ pct_filename = rgb2pct2_tif
194
210
src_filename = test_py_scripts .get_data_path ("gcore" ) + "rgbsmall.tif"
195
- pct_filename4 = "tmp/ test_gdalattachpct_1_4.txt"
211
+ pct_filename4 = str ( tmp_path / " test_gdalattachpct_1_4.txt")
196
212
197
213
# pct from raster
198
214
ct0 = color_table .get_color_table (pct_filename )
@@ -247,25 +263,3 @@ def test_gdalattachpct_1():
247
263
ct3 = None
248
264
ct4 = None
249
265
ct5 = None
250
-
251
-
252
- ###############################################################################
253
- # Cleanup
254
-
255
-
256
- def test_rgb2pct_cleanup ():
257
-
258
- lst = [
259
- "tmp/test_rgb2pct_1.tif" ,
260
- "tmp/test_pct2rgb_1.tif" ,
261
- "tmp/test_rgb2pct_2.tif" ,
262
- "tmp/test_rgb2pct_3.tif" ,
263
- "tmp/test_pct2rgb_1.tif" ,
264
- "tmp/test_pct2rgb_4.tif" ,
265
- "tmp/test_gdalattachpct_1_4.txt" ,
266
- ]
267
- for filename in lst :
268
- try :
269
- os .remove (filename )
270
- except OSError :
271
- pass
0 commit comments