@@ -15,13 +15,10 @@ def string_to_img(image_string: str) -> Image.Image:
1515 rows = [s for s in image_string .replace (" " , "" ).split ("\n " ) if len (s )]
1616 height = len (rows )
1717 width = len (rows [0 ])
18- im = Image .new ("L" , (width , height ))
19- for i in range (width ):
20- for j in range (height ):
21- c = rows [j ][i ]
22- v = c in "X1"
23- im .putpixel ((i , j ), v )
24-
18+ im = Image .new ("1" , (width , height ))
19+ for x in range (width ):
20+ for y in range (height ):
21+ im .putpixel ((x , y ), rows [y ][x ] in "X1" )
2522 return im
2623
2724
@@ -42,10 +39,10 @@ def img_to_string(im: Image.Image) -> str:
4239 """Turn a (small) binary image into a string representation"""
4340 chars = ".1"
4441 result = []
45- for r in range (im .height ):
42+ for y in range (im .height ):
4643 line = ""
47- for c in range (im .width ):
48- value = im .getpixel ((c , r ))
44+ for x in range (im .width ):
45+ value = im .getpixel ((x , y ))
4946 assert not isinstance (value , tuple )
5047 assert value is not None
5148 line += chars [value > 0 ]
@@ -165,10 +162,12 @@ def test_edge() -> None:
165162 )
166163
167164
168- def test_corner () -> None :
165+ @pytest .mark .parametrize ("mode" , ("1" , "L" ))
166+ def test_corner (mode : str ) -> None :
169167 # Create a corner detector pattern
170168 mop = ImageMorph .MorphOp (patterns = ["1:(... ... ...)->0" , "4:(00. 01. ...)->1" ])
171- count , Aout = mop .apply (A )
169+ image = A .convert (mode ) if mode == "L" else A
170+ count , Aout = mop .apply (image )
172171 assert count == 5
173172 assert_img_equal_img_string (
174173 Aout ,
@@ -184,7 +183,7 @@ def test_corner() -> None:
184183 )
185184
186185 # Test the coordinate counting with the same operator
187- coords = mop .match (A )
186+ coords = mop .match (image )
188187 assert len (coords ) == 4
189188 assert tuple (coords ) == ((2 , 2 ), (4 , 2 ), (2 , 4 ), (4 , 4 ))
190189
@@ -232,14 +231,14 @@ def test_negate() -> None:
232231
233232
234233def test_incorrect_mode () -> None :
235- im = hopper ("RGB" )
234+ im = hopper ()
236235 mop = ImageMorph .MorphOp (op_name = "erosion8" )
237236
238- with pytest .raises (ValueError , match = "Image mode must be L" ):
237+ with pytest .raises (ValueError , match = "Image mode must be 1 or L" ):
239238 mop .apply (im )
240- with pytest .raises (ValueError , match = "Image mode must be L" ):
239+ with pytest .raises (ValueError , match = "Image mode must be 1 or L" ):
241240 mop .match (im )
242- with pytest .raises (ValueError , match = "Image mode must be L" ):
241+ with pytest .raises (ValueError , match = "Image mode must be 1 or L" ):
243242 mop .get_on_pixels (im )
244243
245244
0 commit comments