ipo_images.html
001| """IPO Implementation -- Images.
001|
001| Classes:
001| ipo_input_image (ipo_input_file) ~Image File Input subclass
001| ipo_output_image (ipo_output_file) ~Image File Output subclass
001|
001| """
002|
003| from PIL import Image
004| from logger import logger
005| from goodies import typename, KWARG
006| from ipo_defs import ipo_token, EOL_TAG, SPC_TAG
007| from ipo_files import ipo_input_file, ipo_output_file
008|
009| Log = logger('Images')
010|
011|
012|
013| class ipo_input_image (ipo_input_file):
014| """Image File Input subclass."""
015| tag = 'Image'
016|
017| def __init__ (self, **kwargs):
018| """New InputImageFile instance."""
019| super().__init__('ImageFileIn', **kwargs)
020| self.img_type = KWARG('type', 'PNG', str, kwargs)
021| self.img_mode = KWARG('mode', 'RGB', str, kwargs)
022| self.data = []
023|
024| def __call__ (self):
025| """Return next chunk of data."""
026| if self.status < 0:
027| return None
028|
029| if len(self.data) <= self.count:
030| self.status = -1
031| return None
032|
033| pixel = self.data[self.count]
034| datum = [ipo_token(pixel, self.count+1, 1, 'pixel')]
035| super().__call__(datum)
036| return datum
037|
038| def start (self, datum):
039| """Initialize this object."""
040| super().start(datum)
041| self.fp = Image.open(self.filename)
042| self.data = self.fp.getdata()
043| Log.info(f'Image.mode: {self.fp.mode}')
044| Log.info(f'Image.size: {self.fp.size}')
045| Log.info(f'Image.info: {self.fp.info}')
046| Log.info()
047| datum = [
048| ipo_token(self.fp.size[0], 0, 1, 'width'),
049| ipo_token(self.fp.size[1], 0, 2, 'height'),
050| ipo_token(self.fp.mode , 0, 3, 'mode'),
051| ]
052| return datum
053|
054|
055| class ipo_output_image (ipo_output_file):
056| """Image File Output subclass."""
057| tag = 'Image'
058|
059| def __init__ (self, **kwargs):
060| """New OutputImageFile instance."""
061| super().__init__('ImageFileOut', **kwargs)
062| self.img_type = KWARG('type', 'PNG', str, kwargs)
063| self.img_mode = KWARG('mode', 'RGB', str, kwargs)
064| self.img_width = KWARG('w', 800, int, kwargs)
065| self.img_height = KWARG('h', 600, int, kwargs)
066| self.data = []
067|
068| def __call__ (self, datum):
069| """Output next chunk of data."""
070| if datum is None:
071| self.status = -1
072| return
073| if (self.status < 0) or (len(datum) == 0):
074| return
075| super().__call__(datum)
076| for d in datum:
077| self.data.append(d())
078| return self.count
079|
080| def start (self, datum):
081| """Initialize this object."""
082| super().start(datum)
083|
084| if datum and (len(datum) == 3):
085| width,height,mode = datum
086| self.img_width = width()
087| self.img_height = height()
088| self.img_mode = mode()
089| Log.info(f'New Image: {self.img_width}x{self.img_height}; {self.img_mode}')
090|
091| dims = (self.img_width, self.img_height)
092| self.fp = Image.new(self.img_mode, dims)
093| return datum
094|
095| def stop (self, datum):
096| """Terminate this object."""
097|
098| self.fp.putdata(self.data)
099| self.fp.save(self.filename)
100| return super().stop(datum)
101|
102|
103|
104| if __name__ == '__main__':
105| print()
106| ipos = []
107|
108| ipos.append(ipo_input_image(**{'filename': r'C:\demo\hcc\python\colors.png'}))
109| ipos.append(ipo_output_image(**{'filename': r'C:\demo\image.png'}))
110| ipos.append(None)
111|
112| for ix,obj in enumerate(ipos):
113| if obj is None:
114| print()
115| continue
116| print(f'{ix:2d}: {obj!s}')
117| obj.start(None)
118| print()
119|
120| """eof"""
121|