def create_lut(self): for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'): lb = ImageMorph.LutBuilder(op_name=op) lut = lb.build_lut() with open('Tests/images/%s.lut' % op, 'wb') as f: f.write(lut)
def create_lut(): for op in ("corner", "dilation4", "dilation8", "erosion4", "erosion8", "edge"): lb = ImageMorph.LutBuilder(op_name=op) lut = lb.build_lut() with open("Tests/images/%s.lut" % op, "wb") as f: f.write(lut)
def test_lut(self): for op in ('corner', 'dilation4', 'dilation8', 'erosion4', 'erosion8', 'edge'): lb = ImageMorph.LutBuilder(op_name=op) lut = lb.build_lut() with open('Tests/images/%s.lut' % op, 'rb') as f: self.assertEqual(lut, bytearray(f.read()))
def test_pattern_syntax_error(self): # Arrange lb = ImageMorph.LutBuilder(op_name='corner') new_patterns = ['a pattern with a syntax error'] lb.add_patterns(new_patterns) # Act / Assert self.assertRaises(Exception, lb.build_lut)
def test_lut(self): for op in ("corner", "dilation4", "dilation8", "erosion4", "erosion8", "edge"): lb = ImageMorph.LutBuilder(op_name=op) self.assertIsNone(lb.get_lut()) lut = lb.build_lut() with open("Tests/images/%s.lut" % op, "rb") as f: self.assertEqual(lut, bytearray(f.read()))
def test_lut(): for op in ("corner", "dilation4", "dilation8", "erosion4", "erosion8", "edge"): lb = ImageMorph.LutBuilder(op_name=op) assert lb.get_lut() is None lut = lb.build_lut() with open("Tests/images/%s.lut" % op, "rb") as f: assert lut == bytearray(f.read())
def test_pattern_syntax_error(): # Arrange lb = ImageMorph.LutBuilder(op_name="corner") new_patterns = ["a pattern with a syntax error"] lb.add_patterns(new_patterns) # Act / Assert with pytest.raises(Exception) as e: lb.build_lut() assert str( e.value) == 'Syntax error in pattern "a pattern with a syntax error"'
def test_set_lut(): # Arrange lb = ImageMorph.LutBuilder(op_name="corner") lut = lb.build_lut() mop = ImageMorph.MorphOp() # Act mop.set_lut(lut) # Assert assert mop.lut == lut
def test_set_lut(self): # Arrange lb = ImageMorph.LutBuilder(op_name='corner') lut = lb.build_lut() mop = ImageMorph.MorphOp() # Act mop.set_lut(lut) # Assert self.assertEqual(mop.lut, lut)
def test_pattern_syntax_error(self): # Arrange lb = ImageMorph.LutBuilder(op_name='corner') new_patterns = ['a pattern with a syntax error'] lb.add_patterns(new_patterns) # Act / Assert with self.assertRaises(Exception) as e: lb.build_lut() self.assertEqual( str(e.exception), 'Syntax error in pattern "a pattern with a syntax error"')
def processImage(path): im = Image.open(path) print("Path:", path) print("Format:", im.format) print("Dimensions:", im.size) print("Mode:", im.mode) # Gerando níveis de dimensões diferentes dimensions = [] # Utilizei potencias de dois por arbitrariedade mesmo for j in [2**i for i in range(5)]: dimensions.append((im.width / j, im.height / j)) print(dimensions[-1]) im_all = [] for (width, height) in dimensions: aux = im.resize((int(width), int(height))) im_all.append(aux.resize((im.width, im.height))) map_char = [] for im in im_all: for it in im_all: if im != it: map_char.append(ImageChops.difference(im, it)) result = map_char[0] for img in map_char[1:]: result = ImageChops.add(result, img, scale=2.0) # get an image # make a blank image for the text, initialized to transparent text color txt = Image.new('RGBA', im.size, (255, 255, 255, 0)) # get a font fnt = ImageFont.truetype('./Roboto-Black.ttf', 30) # get a drawing context d = ImageDraw.Draw(txt) grayscale = result.convert('L') im = Image.open(path).convert('RGBA') out = im for i in range(1, 35): _, maxima = grayscale.getextrema() mask_image = grayscale.point(lambda i: 255 if i == maxima else 0) mask = ImageMorph.MorphOp(lut=ImageMorph.LutBuilder( patterns=["1:(000 010 000)->1"]).build_lut()) match = mask.match(mask_image) d.text(match[0], str(i), font=fnt, fill=(255, 255, 255, 200)) out = Image.alpha_composite(im, txt) supress = Image.new('L', (20, 20), "black") (w, h) = match[0] grayscale.paste(supress, (w - 10, h - 10)) out.show()
def test_add_patterns(self): # Arrange lb = ImageMorph.LutBuilder(op_name='corner') self.assertEqual(lb.patterns, ['1:(... ... ...)->0', '4:(00. 01. ...)->1']) new_patterns = ['M:(00. 01. ...)->1', 'N:(00. 01. ...)->1'] # Act lb.add_patterns(new_patterns) # Assert self.assertEqual(lb.patterns, [ '1:(... ... ...)->0', '4:(00. 01. ...)->1', 'M:(00. 01. ...)->1', 'N:(00. 01. ...)->1' ])
def test_add_patterns(): # Arrange lb = ImageMorph.LutBuilder(op_name="corner") assert lb.patterns == ["1:(... ... ...)->0", "4:(00. 01. ...)->1"] new_patterns = ["M:(00. 01. ...)->1", "N:(00. 01. ...)->1"] # Act lb.add_patterns(new_patterns) # Assert assert lb.patterns == [ "1:(... ... ...)->0", "4:(00. 01. ...)->1", "M:(00. 01. ...)->1", "N:(00. 01. ...)->1", ]
def test_wrong_mode(self): lut = ImageMorph.LutBuilder(op_name='corner').build_lut() imrgb = Image.new('RGB', (10, 10)) iml = Image.new('L', (10, 10)) with self.assertRaises(RuntimeError): _imagingmorph.apply(bytes(lut), imrgb.im.id, iml.im.id) with self.assertRaises(RuntimeError): _imagingmorph.apply(bytes(lut), iml.im.id, imrgb.im.id) with self.assertRaises(RuntimeError): _imagingmorph.match(bytes(lut), imrgb.im.id) # Should not raise _imagingmorph.match(bytes(lut), iml.im.id)
def test_wrong_mode(): lut = ImageMorph.LutBuilder(op_name="corner").build_lut() imrgb = Image.new("RGB", (10, 10)) iml = Image.new("L", (10, 10)) with pytest.raises(RuntimeError): _imagingmorph.apply(bytes(lut), imrgb.im.id, iml.im.id) with pytest.raises(RuntimeError): _imagingmorph.apply(bytes(lut), iml.im.id, imrgb.im.id) with pytest.raises(RuntimeError): _imagingmorph.match(bytes(lut), imrgb.im.id) # Should not raise _imagingmorph.match(bytes(lut), iml.im.id)
def test_add_patterns(self): # Arrange lb = ImageMorph.LutBuilder(op_name="corner") self.assertEqual(lb.patterns, ["1:(... ... ...)->0", "4:(00. 01. ...)->1"]) new_patterns = ["M:(00. 01. ...)->1", "N:(00. 01. ...)->1"] # Act lb.add_patterns(new_patterns) # Assert self.assertEqual( lb.patterns, [ "1:(... ... ...)->0", "4:(00. 01. ...)->1", "M:(00. 01. ...)->1", "N:(00. 01. ...)->1", ], )
# rozměry obrázku width = test_image.size[0] height = test_image.size[1] # načtení fontu font = ImageFont.truetype('FreeMono.ttf', 100) # vykreslení jednoduchého textu draw.text((70, height / 2 - 50), "Pillow", font=font, fill=255) # vzorek pro LUT patterns = ["1:(1)->0", "1:(0)->1"] # vytvoření objektu pro morfologické operace lutBuilder = ImageMorph.LutBuilder(patterns=patterns) lut = lutBuilder.build_lut() # aplikace morfologické operace morphOp = ImageMorph.MorphOp(lut=lut) pixels, updated_image = morphOp.apply(test_image) # zobrazení původního i upraveného obrázku test_image.show() updated_image.show() # uložení původního i upraveného obrázku test_image.save("42_bitmap.png") updated_image.save("42_updated.png") except Exception as e:
def test_unknown_pattern(self): self.assertRaises(Exception, lambda: ImageMorph.LutBuilder(op_name='unknown'))
#import RPGPIO as gpio import cognitive_face as MCF # @Microsoft 认知服务 import requests from PIL import Image, ImageMorph # 图像处理 from picamera import PiCamera camera = PiCamera() # 初始化第三方库 Cj = ImageMorph.LutBuilder() camera.capture('test.jpg') camera.capture('test1.jpg') Ae = ImageMorph.MorphOp.match(Cj, 'test1.jpg') print(Ae) #key = input('Please enter your key') #KEY = '%s' % key #MCF.Key.set(KEY) #BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/' #MCF.BaseUrl.set(BASE_URL) # Microsoft 认知服务初始化 #def facecdetect(): #camera.resolution = (1024, 768) #camera.capture('test.jpg') #faces = MCF.face.detect('test.jpg') #rect = faces['faceRectangle'] #left = rect['left'] #top = rect['top'] #bottom = left + rect['height'] #right = top + rect['width'] #return ((left, top), (bottom, right)) #while 0 == 0:
test_image = Image.new("L", (512, 512)) # objekt umožňující kreslení do obrázku draw = ImageDraw.Draw(test_image) # rozměry obrázku width = test_image.size[0] height = test_image.size[1] # načtení fontu font = ImageFont.truetype('FreeMono.ttf', 100) # vykreslení jednoduchého textu draw.text((70, height / 2 - 50), "Pillow", font=font, fill=255) lutBuilder = ImageMorph.LutBuilder(patterns=["4:(... .0. .1.)->1"]) # vytvoření objektu pro morfologické operace lut = lutBuilder.build_lut() morphOp = ImageMorph.MorphOp(lut=lut) # aplikace morfologické operace pixels, dilated_image = morphOp.apply(test_image) print(pixels) # aplikace morfologické operace pixels, more_dilated_image = morphOp.apply(dilated_image) print(pixels) # zobrazení původního i upraveného obrázku test_image.show()
def test_unknown_pattern(): with pytest.raises(Exception): ImageMorph.LutBuilder(op_name="unknown")