@@ -99,39 +99,47 @@ def set_record_id(self, record_id: int):
99
99
self .record_id = record_id
100
100
101
101
def _repr (self ) -> List [str ]:
102
- return [f"Image ID: { self .record_id } " ]
102
+ return [f"Record ID: { self .record_id } " ]
103
103
104
104
def as_dict (self ) -> dict :
105
105
return {"record_id" : self .record_id }
106
106
107
107
108
108
# TODO: we need a way to combine filepath and image mixin
109
- # TODO: rename to ImageArrayRecordComponent
110
109
class ImageRecordComponent (RecordComponent ):
111
110
def __init__ (self , task = tasks .common ):
112
111
super ().__init__ (task = task )
113
112
self .img = None
114
113
115
- def set_img (self , img : np .ndarray ):
114
+ def set_img (self , img : Union [PIL .Image .Image , np .ndarray ]):
115
+ assert isinstance (img , (PIL .Image .Image , np .ndarray ))
116
116
self .img = img
117
- height , width , _ = self .img .shape
117
+ if isinstance (img , PIL .Image .Image ):
118
+ height , width = img .shape
119
+ elif isinstance (img , np .ndarray ):
120
+ # else:
121
+ height , width , _ = self .img .shape
118
122
# this should set on SizeRecordComponent
119
123
self .composite .set_img_size (ImgSize (width = width , height = height ), original = True )
120
124
121
125
def _repr (self ) -> List [str ]:
122
126
if self .img is not None :
123
- ndims = len (self .img .shape )
124
- if ndims == 3 : # RGB, RGBA
125
- height , width , channels = self .img .shape
126
- elif ndims == 2 : # Grayscale
127
- height , width , channels = [* self .img .shape , 1 ]
128
- else :
129
- raise ValueError (
130
- f"Expected image to have 2 or 3 dimensions, got { ndims } instead"
131
- )
132
- return [f"Image: { width } x{ height } x{ channels } <np.ndarray> Image" ]
127
+ if isinstance (self .img , np .ndarray ):
128
+ ndims = len (self .img .shape )
129
+ if ndims == 3 : # RGB, RGBA
130
+ height , width , channels = self .img .shape
131
+ elif ndims == 2 : # Grayscale
132
+ height , width , channels = [* self .img .shape , 1 ]
133
+ else :
134
+ raise ValueError (
135
+ f"Expected image to have 2 or 3 dimensions, got { ndims } instead"
136
+ )
137
+ return [f"Img: { width } x{ height } x{ channels } <np.ndarray> Image" ]
138
+ elif isinstance (self .img , PIL .Image .Image ):
139
+ height , width = self .img .shape
140
+ return [f"Img: { width } x{ height } <PIL.Image; mode='{ self .img .mode } '>" ]
133
141
else :
134
- return [f"Image : { self .img } " ]
142
+ return [f"Img : { self .img } " ]
135
143
136
144
def _unload (self ):
137
145
self .img = None
0 commit comments