def test_extract_colors_from_image(): path = os.path.join(settings.ROOT, 'src/olympia/versions/tests/static_themes/weta.png') expected = [{ 'h': 45, 'l': 158, 'ratio': 0.40547158773994313, 's': 34 }, { 'h': 44, 'l': 94, 'ratio': 0.2812929380875291, 's': 28 }, { 'h': 68, 'l': 99, 'ratio': 0.13200103391513734, 's': 19 }, { 'h': 43, 'l': 177, 'ratio': 0.06251105336906689, 's': 93 }, { 'h': 47, 'l': 115, 'ratio': 0.05938209966397758, 's': 60 }, { 'h': 40, 'l': 201, 'ratio': 0.05934128722434598, 's': 83 }] assert extract_colors_from_image(path) == expected
def generate_static_theme_preview(theme_manifest, version_pk): # Make sure we import `index_addons` late in the game to avoid having # a "copy" of it here that won't get mocked by our ESTestCase from olympia.addons.tasks import index_addons tmpl = loader.get_template( 'devhub/addons/includes/static_theme_preview_svg.xml') file_ = File.objects.filter(version_id=version_pk).first() if not file_: return complete_context = _build_static_theme_preview_context( theme_manifest, file_) renderings = sorted(amo.THEME_PREVIEW_RENDERINGS.values(), key=operator.itemgetter('position')) colors = None for rendering in renderings: # Create a Preview for this rendering. preview = VersionPreview.objects.create( version_id=version_pk, position=rendering['position'], sizes={ 'image_format': rendering['image_format'], 'thumbnail_format': rendering['thumbnail_format'], }, ) # Add the size to the context and render complete_context.update(svg_render_size=rendering['full']) if rendering['image_format'] == 'svg': render_success = render_to_svg( tmpl, { **complete_context, **TRANSPARENT_UI }, preview, rendering['thumbnail'], theme_manifest, ) else: render_success = render_to_png(tmpl, complete_context, preview, rendering['thumbnail']) if render_success: # Extract colors once and store it for all previews. # Use the thumbnail for extra speed, we don't need to be super accurate. if colors is None: colors = extract_colors_from_image(preview.thumbnail_path) data = { 'sizes': { 'image': rendering['full'], 'thumbnail': rendering['thumbnail'], 'image_format': rendering['image_format'], 'thumbnail_format': rendering['thumbnail_format'], }, 'colors': colors, } preview.update(**data) addon_id = Version.objects.values_list('addon_id', flat=True).get(id=version_pk) index_addons.delay([addon_id])
def extract_colors_from_static_themes(ids, **kw): """Extract and store colors from existing static themes.""" log.info('Extracting static themes colors %d-%d [%d].', ids[0], ids[-1], len(ids)) addons = Addon.objects.filter(id__in=ids) extracted = [] for addon in addons: first_preview = addon.current_previews.first() if first_preview and not first_preview.colors: colors = extract_colors_from_image(first_preview.thumbnail_path) addon.current_previews.update(colors=colors) extracted.append(addon.pk) if extracted: index_addons.delay(extracted)
def test_extract_colors_from_image(): path = os.path.join( settings.ROOT, 'src/olympia/versions/tests/static_themes/weta.png') expected = [ {'h': 45, 'l': 158, 'ratio': 0.40547158773994313, 's': 34}, {'h': 44, 'l': 94, 'ratio': 0.2812929380875291, 's': 28}, {'h': 68, 'l': 99, 'ratio': 0.13200103391513734, 's': 19}, {'h': 43, 'l': 177, 'ratio': 0.06251105336906689, 's': 93}, {'h': 47, 'l': 115, 'ratio': 0.05938209966397758, 's': 60}, {'h': 40, 'l': 201, 'ratio': 0.05934128722434598, 's': 83} ] assert extract_colors_from_image(path) == expected
def extract_colors_from_static_themes(ids, **kw): """Extract and store colors from existing static themes.""" log.info('Extracting static themes colors %d-%d [%d].', ids[0], ids[-1], len(ids)) addons = Addon.objects.filter(id__in=ids) extracted = [] for addon in addons: first_preview = addon.current_previews.first() if first_preview and not first_preview.colors: colors = extract_colors_from_image(first_preview.thumbnail_path) addon.current_previews.update(colors=colors) extracted.append(addon.pk) if extracted: index_addons.delay(extracted)
def generate_static_theme_preview(theme_manifest, version_pk): # Make sure we import `index_addons` late in the game to avoid having # a "copy" of it here that won't get mocked by our ESTestCase from olympia.addons.tasks import index_addons tmpl = loader.get_template( 'devhub/addons/includes/static_theme_preview_svg.xml') file_ = File.objects.filter(version_id=version_pk).first() if not file_: return context = _build_static_theme_preview_context(theme_manifest, file_) renderings = sorted(amo.THEME_PREVIEW_RENDERINGS.values(), key=operator.itemgetter('position')) colors = None for rendering in renderings: # Create a Preview for this size. preview = VersionPreview.objects.create( version_id=version_pk, position=rendering['position'], sizes={'thumbnail_format': rendering['thumbnail_format']}, ) # Add the size to the context and render context.update(svg_render_size=rendering['full']) svg = tmpl.render(context).encode('utf-8') if write_svg_to_png(svg, preview.image_path): resize_image( preview.image_path, preview.thumbnail_path, rendering['thumbnail'], format=rendering['thumbnail_format'], quality= 35, # It's ignored for png format, so it's fine to always set. ) pngcrush_image(preview.image_path) # Extract colors once and store it for all previews. # Use the thumbnail for extra speed, we don't need to be super accurate. if colors is None: colors = extract_colors_from_image(preview.thumbnail_path) data = { 'sizes': { 'image': rendering['full'], 'thumbnail': rendering['thumbnail'], 'thumbnail_format': rendering['thumbnail_format'], }, 'colors': colors, } preview.update(**data) addon_id = Version.objects.values_list('addon_id', flat=True).get(id=version_pk) index_addons.delay([addon_id])
def generate_static_theme_preview(theme_manifest, version_pk): # Make sure we import `index_addons` late in the game to avoid having # a "copy" of it here that won't get mocked by our ESTestCase from olympia.addons.tasks import index_addons tmpl = loader.get_template( 'devhub/addons/includes/static_theme_preview_svg.xml') file_ = File.objects.filter(version_id=version_pk).first() if not file_: return context = _build_static_theme_preview_context(theme_manifest, file_) sizes = sorted( amo.THEME_PREVIEW_SIZES.values(), key=operator.itemgetter('position')) colors = None for size in sizes: # Create a Preview for this size. preview = VersionPreview.objects.create( version_id=version_pk, position=size['position']) # Add the size to the context and render context.update(svg_render_size=size['full']) svg = tmpl.render(context).encode('utf-8') if write_svg_to_png(svg, preview.image_path): resize_image( preview.image_path, preview.thumbnail_path, size['thumbnail']) pngcrush_image(preview.image_path) # Extract colors once and store it for all previews. # Use the thumbnail for extra speed, we don't need to be super # accurate. if colors is None: colors = extract_colors_from_image(preview.thumbnail_path) data = { 'sizes': { 'image': size['full'], 'thumbnail': size['thumbnail'], }, 'colors': colors, } preview.update(**data) addon_id = Version.objects.values_list( 'addon_id', flat=True).get(id=version_pk) index_addons.delay([addon_id])
def generate_static_theme_preview(theme_manifest, version_pk): tmpl = loader.get_template( 'devhub/addons/includes/static_theme_preview_svg.xml') file_ = File.objects.filter(version_id=version_pk).first() if not file_: return context = _build_static_theme_preview_context(theme_manifest, file_) sizes = sorted(amo.THEME_PREVIEW_SIZES.values(), lambda x, y: x['position'] - y['position']) colors = None for size in sizes: # Create a Preview for this size. preview = VersionPreview.objects.create(version_id=version_pk, position=size['position']) # Add the size to the context and render context.update(svg_render_size=size['full']) svg = tmpl.render(context).encode('utf-8') if write_svg_to_png(svg, preview.image_path): resize_image(preview.image_path, preview.thumbnail_path, size['thumbnail']) pngcrush_image(preview.image_path) # Extract colors once and store it for all previews. # Use the thumbnail for extra speed, we don't need to be super # accurate. if colors is None: colors = extract_colors_from_image(preview.thumbnail_path) data = { 'sizes': { 'image': size['full'], 'thumbnail': size['thumbnail'], }, 'colors': colors, } preview.update(**data) addon_id = Version.objects.values_list('addon_id', flat=True).get(id=version_pk) index_addons.delay([addon_id])