def test_composite_merge(self): # http://stackoverflow.com/questions/3374878 if not hasattr(Image, 'alpha_composite'): raise SkipTest() img1 = Image.new('RGBA', size=(100, 100), color=(255, 0, 0, 255)) draw = ImageDraw.Draw(img1) draw.rectangle((33, 0, 66, 100), fill=(255, 0, 0, 128)) draw.rectangle((67, 0, 100, 100), fill=(255, 0, 0, 0)) img1 = ImageSource(img1) img2 = Image.new('RGBA', size=(100, 100), color=(0, 255, 0, 255)) draw = ImageDraw.Draw(img2) draw.rectangle((0, 33, 100, 66), fill=(0, 255, 0, 128)) draw.rectangle((0, 67, 100, 100), fill=(0, 255, 0, 0)) img2 = ImageSource(img2) result = merge_images([img2, img1], ImageOptions(transparent=True)) img = result.as_image() eq_(img.mode, 'RGBA') assert_colors_equal(img, [(1089, (0, 255, 0, 255)), (1089, (255, 255, 255, 0)), (1122, (0, 255, 0, 128)), (1122, (128, 126, 0, 255)), (1122, (255, 0, 0, 128)), (1156, (170, 84, 0, 191)), (3300, (255, 0, 0, 255))])
def test_paletted_merge(self): if not hasattr(Image, 'FASTOCTREE'): raise SkipTest() # generate RGBA images with a transparent rectangle in the lower right img1 = ImageSource(Image.new('RGBA', (50, 50), (0, 255, 0, 255))).as_image() draw = ImageDraw.Draw(img1) draw.rectangle((25, 25, 49, 49), fill=(0, 0, 0, 0)) paletted_img = quantize(img1, alpha=True) assert img_has_transparency(paletted_img) assert paletted_img.mode == 'P' rgba_img = Image.new('RGBA', (50, 50), (255, 0, 0, 255)) draw = ImageDraw.Draw(rgba_img) draw.rectangle((25, 25, 49, 49), fill=(0, 0, 0, 0)) img1 = ImageSource(paletted_img) img2 = ImageSource(rgba_img) # generate base image and merge the others above img3 = ImageSource(Image.new('RGBA', (50, 50), (0, 0, 255, 255))) result = merge_images([img3, img1, img2], ImageOptions(transparent=True)) img = result.as_image() assert img.mode == 'RGBA' eq_(img.getpixel((49, 49)), (0, 0, 255, 255)) eq_(img.getpixel((0, 0)), (255, 0, 0, 255))
def test_one_point(self): img = Image.new('RGB', (100, 100), color='#ff0000') draw = ImageDraw.Draw(img) draw.point((99, 99)) del draw assert not is_single_color_image(img)
def draw(self, img=None, size=None, in_place=True): """ Create the message image. Either draws on top of `img` or creates a new image with the given `size`. """ if not ((img and not size) or (size and not img)): raise TypeError, 'need either img or size argument' if img is None: base_img = self.new_image(size) elif not in_place: size = img.size base_img = self.new_image(size) else: base_img = img.as_image() size = base_img.size if not self.message: if img is not None: return img return ImageSource(base_img, size=size, image_opts=self.image_opts) draw = ImageDraw.Draw(base_img) self.draw_msg(draw, size) if not in_place and img: img = img.as_image() img.paste(base_img, (0, 0), base_img) base_img = img return ImageSource(base_img, size=size, image_opts=self.image_opts)
def test_multiline_center(self): font = ImageFont.load_default() td = TextDraw('Hello\nWorld', font, placement='cc') img = Image.new('RGB', (100, 100)) draw = ImageDraw.Draw(img) total_box, boxes = td.text_boxes(draw, (100, 100)) eq_(total_box, (35, 38, 65, 63)) eq_(boxes, [(35, 38, 65, 49), (35, 52, 65, 63)])
def test_multiline_lr(self): font = ImageFont.load_default() td = TextDraw('Hello\nWorld', font, placement='lr') img = Image.new('RGB', (100, 100)) draw = ImageDraw.Draw(img) total_box, boxes = td.text_boxes(draw, (100, 100)) eq_(total_box, (65, 70, 95, 95)) eq_(boxes, [(65, 70, 95, 81), (65, 84, 95, 95)])
def test_multiline_ul(self): font = ImageFont.load_default() td = TextDraw('Hello\nWorld', font) img = Image.new('RGB', (100, 100)) draw = ImageDraw.Draw(img) total_box, boxes = td.text_boxes(draw, (100, 100)) eq_(total_box, (5, 5, 35, 30)) eq_(boxes, [(5, 5, 35, 16), (5, 19, 35, 30)])
def test_ul(self): font = ImageFont.load_default() td = TextDraw('Hello', font) img = Image.new('RGB', (100, 100)) draw = ImageDraw.Draw(img) total_box, boxes = td.text_boxes(draw, (100, 100)) eq_(total_box, boxes[0]) eq_(len(boxes), 1)
def test_unicode(self): font = ImageFont.load_default() td = TextDraw(u'Héllö\nWørld', font, placement='cc') img = Image.new('RGB', (100, 100)) draw = ImageDraw.Draw(img) total_box, boxes = td.text_boxes(draw, (100, 100)) eq_(total_box, (35, 38, 65, 63)) eq_(boxes, [(35, 38, 65, 49), (35, 52, 65, 63)])
def test_transparent(self): font = ImageFont.load_default() td = TextDraw('Hello\nWorld', font, placement='cc') img = Image.new('RGBA', (100, 100), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) td.draw(draw, img.size) eq_(len(img.getcolors()), 2) # top color (bg) is transparent eq_(sorted(img.getcolors())[1][1], (0, 0, 0, 0))
def create_debug_img(size, transparent=True): if transparent: img = Image.new("RGBA", size) else: img = Image.new("RGB", size, ImageColor.getrgb("#EEE")) draw = ImageDraw.Draw(img) draw_pattern(draw, size) return img
def create_tmp_image_file(size, two_colored=False): fd, out_file = tempfile.mkstemp(suffix='.png') os.close(fd) print 'creating temp image %s (%r)' % (out_file, size) img = Image.new('RGBA', size) if two_colored: draw = ImageDraw.Draw(img) draw.rectangle((0, 0, img.size[0] // 2, img.size[1]), fill=ImageColor.getrgb('white')) img.save(out_file, 'png') return out_file
def check_placement(self, x, y): font = ImageFont.load_default() td = TextDraw('Hello\nWorld\n%s %s' % (x, y), font, placement=x + y, padding=5, linespacing=2) img = Image.new('RGB', (100, 100)) draw = ImageDraw.Draw(img) td.draw(draw, img.size) img.show()
def test_from_transparent(self): img = self._make_transp_test_image() draw = ImageDraw.Draw(img) draw.rectangle((0, 0, 4, 4), fill=(130, 100, 120, 0)) draw.rectangle((5, 5, 9, 9), fill=(130, 150, 120, 255)) img = make_transparent(img, (130, 150, 120, 120), tolerance=5) assert img.mode == 'RGBA' assert img.size == (50, 50) colors = sorted(img.getcolors(), reverse=True) eq_(colors, [(1550, (130, 140, 120, 100)), (900, (130, 150, 120, 0)), (25, (130, 150, 120, 255)), (25, (130, 100, 120, 0))])
def image_mask_from_geom(img, bbox, polygons): transf = make_lin_transf(bbox, (0, 0) + img.size) mask = Image.new('L', img.size, 255) draw = ImageDraw.Draw(mask) for p in polygons: draw.polygon([transf(coord) for coord in p.exterior.coords], fill=0) for ring in p.interiors: draw.polygon([transf(coord) for coord in ring.coords], fill=255) return mask
def test_composite_merge_opacity(self): if not hasattr(Image, 'alpha_composite'): raise SkipTest() bg = Image.new('RGBA', size=(100, 100), color=(255, 0, 255, 255)) bg = ImageSource(bg) fg = Image.new('RGBA', size=(100, 100), color=(0, 0, 0, 0)) draw = ImageDraw.Draw(fg) draw.rectangle((10, 10, 89, 89), fill=(0, 255, 255, 255)) fg = ImageSource(fg, image_opts=ImageOptions(opacity=0.5)) result = merge_images([bg, fg], ImageOptions(transparent=True)) img = result.as_image() eq_(img.mode, 'RGBA') assert_colors_equal(img, [(3600, (255, 0, 255, 255)), (6400, (128, 127, 255, 255))])
def create_mixed_mode_img(size, format='png'): img = Image.new("RGBA", size) # draw a black rectangle into the image, rect_width = 3/4 img_width # thus 1/4 of the image is transparent and with square tiles, one of two # tiles (img size = 512x256) will be fully opaque and the other # has transparency draw = ImageDraw.Draw(img) w, h = size red_color = ImageColor.getrgb("red") draw.rectangle((w / 4, 0, w, h), fill=red_color) data = StringIO() img.save(data, format) data.seek(0) yield data
def draw(self, img=None, size=None, in_place=True): """ Create the message image. Either draws on top of `img` or creates a new image with the given `size`. """ if not ((img and not size) or (size and not img)): raise TypeError, 'need either img or size argument' if img is None: base_img = self.new_image(size) elif not in_place: size = img.size base_img = self.new_image(size) else: base_img = img.as_image() size = base_img.size if not self.message: if img is not None: return img return ImageSource(base_img, size=size, image_opts=self.image_opts) draw = ImageDraw.Draw(base_img) self.draw_msg(draw, size) image_opts = self.image_opts if not in_place and img: image_opts = image_opts or img.image_opts img = img.as_image() converted = False if len(self.font_color) == 4 and img.mode != 'RGBA': # we need RGBA to keep transparency from text converted = img.mode img = img.convert('RGBA') img.paste(base_img, (0, 0), base_img) if converted == 'RGB': # convert image back img = img.convert('RGB') base_img = img return ImageSource(base_img, size=size, image_opts=image_opts)
def _make_transp_test_image(self): img = Image.new('RGBA', (50, 50), (130, 140, 120, 100)) draw = ImageDraw.Draw(img) draw.rectangle((10, 10, 39, 39), fill=(130, 150, 120, 120)) return img