def create_thumbnail(self, width=None, height=None): elements = self.get_patterns_as_json() elements.sort(cmp=lambda x, y: cmp(x['z_index'], y['z_index'])) if not width or not height: max_width = 0 max_height = 0 for el in elements: bounds = MyMath.get_bounds(el['left'], el['top'], el['width'], el['height'], el['rotation']) if max_width < bounds[0]: max_width = bounds[0] if max_height < bounds[1]: max_height = bounds[1] width, height = int(max_width * 1.1), int(max_height * 1.1) dest_img = Image.new("RGBA", (width, height), "rgb(255,255,255)") dest_img.putalpha(1) try: font = ImageFont.truetype(settings.DRAW_TEXT_FONT, 18) except Exception: logger.error("Couldn't use font from settings.DRAW_TEXT_FONT: %s" % getattr(settings, 'DRAW_TEXT_FONT', None)) font = None for el in elements: curr_img = Image.new("RGBA", (el['width'], el['height']), "rgb(200,200,200)") draw = ImageDraw.Draw(curr_img) draw.rectangle((1, 1, el['width'] - 1, el['height'] - 1), fill="rgb(255,240,200)", outline="rgb(128,128,128)") if font and el['label']: w, h = draw.textsize(el['label'], font=font) draw.text((max(0, el['width'] - w) / 2, max(0, el['height'] - h) / 2), el['label'], fill="black") bounds = MyMath.get_bounds(0, 0, el['width'], el['height'], el['rotation']) final_left = el['left'] - (bounds[0] - el['width']) / 2 final_top = el['top'] - (bounds[1] - el['height']) / 2 if el['rotation']: curr_img = curr_img.rotate(-el['rotation'], Image.BICUBIC, expand=True) dest_img.paste(curr_img, (final_left, final_top), curr_img) pixels = dest_img.load() width, height = dest_img.size for x in xrange(width): for y in xrange(height): r, g, b, a = pixels[x, y] if a == 0: pixels[x, y] = (255, 255, 255, 255) dest_img = dest_img.convert('RGB') new_size = _calc_size_min(dest_img.size, 200) dest_img = dest_img.resize(new_size, Image.ANTIALIAS) dest_buff = StringIO() dest_img.save(dest_buff, 'JPEG') dest_buff.seek(0) if getattr(settings, 'LOCAL_PATH_FILE_SAVE', False): dest_img.save('%s/final_%s.png' % (settings.LOCAL_PATH_FILE_SAVE, self.id), 'PNG') else: self.thumbnail.save("%s_%s.jpg" % (slugify(self.name), self.id), File(dest_buff))
def create_thumbnail(self, width=None, height=None): elements = self.get_elements_as_json() elements.sort(cmp=lambda x, y: cmp(x['z_index'], y['z_index'])) if not width or not height: max_width = 0 max_height = 0 for el in elements: bounds = MyMath.get_bounds(el['left'], el['top'], el['width'], el['height'], el['rotation']) if max_width < bounds[0]: max_width = bounds[0] if max_height < bounds[1]: max_height = bounds[1] width, height = max_width, max_height dest_img = Image.new("RGBA", (width, height), "rgb(255,255,255)") dest_img.putalpha(1) for el in elements: bounds = MyMath.get_bounds(0, 0, el['width'], el['height'], el['rotation']) final_left = el['left'] - (bounds[0] - el['width']) / 2 final_top = el['top'] - (bounds[1] - el['height']) / 2 url = el.get('image', {}).get('src') if not url: continue if url.startswith('//'): url = 'http:' + url try: if re.search('^https?://', url, re.I): content = urllib2.urlopen(url).read() else: content = global_storage.open(url).read() except Exception: raise curr_img = Image.open(cStringIO.StringIO(content)) new_size = _calc_max_width_height(curr_img.size, el['width'], el['height']) if int(el['image']['scale'] * 20.0) != 20: new_size = (int(new_size[0] * el['image']['scale']), int(new_size[1] * el['image']['scale'])) if new_size != curr_img.size: curr_img = curr_img.resize(new_size, Image.ANTIALIAS) if int(el['image']['scale'] * 20.0) != 20 or el['image']['offset_x'] or el['image']['offset_y']: crop_params = [(new_size[0] - el['width']) / 2 - el['image']['offset_x'], (new_size[1] - el['height']) / 2 - el['image']['offset_y']] img_bckg = Image.new("RGBA", (abs(crop_params[0]) + el['width'], abs(crop_params[1]) + el['height']), "rgb(255,255,255)") img_bckg.putalpha(1) paste_x, paste_y = 0, 0 if crop_params[0] < 0: paste_x = -crop_params[0] crop_params[0] = 0 if crop_params[1] < 0: paste_y = -crop_params[1] crop_params[1] = 0 img_bckg.paste(curr_img, (paste_x, paste_y)) curr_img = img_bckg.crop((crop_params[0], crop_params[1], crop_params[0] + el['width'], crop_params[1] + el['height'])) if el['rotation']: curr_img = curr_img.rotate(-el['rotation'], Image.BICUBIC, expand=True) dest_img.paste(curr_img, (final_left, final_top), curr_img) pixels = dest_img.load() width, height = dest_img.size for x in xrange(width): for y in xrange(height): r, g, b, a = pixels[x, y] if a == 0: pixels[x, y] = (255, 255, 255, 255) dest_img = dest_img.convert('RGB') new_size = _calc_size_min(dest_img.size, 200) dest_img = dest_img.resize(new_size, Image.ANTIALIAS) dest_buff = StringIO() dest_img.save(dest_buff, 'JPEG') dest_buff.seek(0) self.thumbnail.save("%s_%s.jpg" % (slugify(self.name), self.id), File(dest_buff))