Beispiel #1
0
    def run(self):
        argv = sys.argv[1:]
        (options, args) = self.parser.parse_args(argv)

        if args:
            # image filenames were provided as arguments
            for filename in args:
                try:
                    palette = extract_colors(
                        filename,
                        min_saturation=options.min_saturation,
                        min_prominence=options.min_prominence,
                        min_distance=options.min_distance,
                        max_colors=options.max_colors,
                        n_quantized=options.n_quantized)

                except Exception, e:  # TODO: it's too broad exception.
                    print >> sys.stderr, filename, e
                    continue

                print_colors(filename, palette)
                if options.save_palette:
                    save_palette_as_image(filename, palette)

            sys.exit(1)
Beispiel #2
0
def attach_colour_to_product(product_data, using_gallery_img):
    info_msg("Attaching colours to products")
    total_products = len(product_data)
    invalid_images = []
    for index, data in enumerate(product_data):
        info_msg("[{0}/{1}]Extracting colours for product {2}".format(
            index,
            total_products,
            data['sku'])
        )
        product_filename = get_product_filename(data, check_file_exists=True)
        if product_filename is None:
            continue

        if using_gallery_img:
            product_filename = transform_image_to_rgba(product_filename)

        if product_filename is None:
            invalid_images.append(data['sku'])
            continue

        colours_found = palette.extract_colors(product_filename)


        data['colours'] = [
            (c.value, c.prominence)
            for c in colours_found.colors
        ]

        bg_colour = colours_found.bgcolor
        if bg_colour:
            data['colours'].insert(0, (bg_colour.value, bg_colour.prominence))

    return invalid_images
Beispiel #3
0
def main(meetup, tc=(255, 255, 255), bg=None, *tags):
    target_url = meetup

    soup = BeautifulSoup(requests.get(target_url).content, "html.parser")

    description = soup.find("div", id="groupDesc")
    description = (" " * 4).join(map(lambda x: str(x), description.contents)) + (" " * 4)
    description = "\n".join(map(lambda x: x.rstrip(), description.split("\n")))

    target_meetup_name = target_url.split("/")[-2]
    target = target_url.split("/")[-2].lower().replace("-", "_")

    if re.match("^\d", target):
        target = "_" + target

    logo_url = soup.find("img", "photo")["src"] if soup.find("img", "photo") else None

    if bg == None:
        if logo_url:
            palette = extract_colors(Image.open(BytesIO(requests.get(logo_url).content)))

            colors = palette.colors
            background_color = colors[0].value
            text_color = tc
        else:
            h = (random.randint(1, 100) * 0.618033988749895) % 1
            background_color = hsv_to_rgb(h, .5, .95)
            text_color = "#000000"

        h, s, v = rgb_to_hsv(background_color)
    else:
        background_color = bg

        text_color = tc

    # background_color = map(lambda x: (x + 255)/2, background_color)

    red = RedBaron(open("agendas/be.py", "r").read())

    for i in red("def", recursive=False):
        if target < i.name:
            break

    i.insert_before(template % {
        "background_color": rgb_to_hex(background_color) if not (isinstance(background_color, basestring) and background_color.startswith("#")) else background_color,
        "text_color": rgb_to_hex(text_color) if not (isinstance(text_color, basestring) and text_color.startswith("#")) else text_color,
        "url": target_url,
        "tags": ", ".join(map(repr, tags)),
        "function_name": target,
        "description": description,
        "meetup_name": target_meetup_name,
    })

    red.dumps()

    open("agendas/be.py", "w").write(red.dumps())

    os.system("python manage.py fetch_events %s" % target)
Beispiel #4
0
 def test_extraction(self):
     expected = [(0, 101, 185),
                 (187, 214, 236),
                 (255, 0, 0),
                 (45, 68, 86),
                 (119, 173, 218)]
     p = palette.extract_colors(self.filename)
     found = [c.value for c in p.colors]
     self.assertEquals(found, expected)
Beispiel #5
0
def color_stream_st(istream=sys.stdin, save_palette=False, **kwargs):
    """
    Read filenames from the input stream and detect their palette.
    """
    for line in istream:
        filename = line.strip()
        try:
            palette = extract_colors(filename, **kwargs)

        except Exception, e:
            print >> sys.stderr, filename, e
            continue

        print_colors(filename, palette)
        if save_palette:
            save_palette_as_image(filename, palette)
Beispiel #6
0
def color_process(queue, lock):
    "Receive filenames and get the colors from their images."
    while True:
        block = queue.get()
        if block == config.SENTINEL:
            break

        for filename in block:
            try:
                palette = extract_colors(filename)
            except:  # TODO: it's too broad exception.
                continue
            lock.acquire()
            try:
                print_colors(filename, palette)
            finally:
                lock.release()
Beispiel #7
0
    def run(self):
        argv = sys.argv[1:]
        (options, args) = self.parser.parse_args(argv)

        if args:
            # image filenames were provided as arguments
            for filename in args:
                try:
                    palette = extract_colors(
                        filename,
                        min_saturation=options.min_saturation,
                        min_prominence=options.min_prominence,
                        min_distance=options.min_distance,
                        max_colors=options.max_colors,
                        n_quantized=options.n_quantized,
                    )

                except Exception as e:  # TODO: it's too broad exception.
                    print >>sys.stderr, filename, e
                    continue

                print_colors(filename, palette)
                if options.save_palette:
                    save_palette_as_image(filename, palette)

            sys.exit(1)

        if options.n_processes > 1:
            # XXX add all the knobs we can tune
            color_stream_mt(n=options.n_processes)

        else:
            color_stream_st(
                min_saturation=options.min_saturation,
                min_prominence=options.min_prominence,
                min_distance=options.min_distance,
                max_colors=options.max_colors,
                n_quantized=options.n_quantized,
                save_palette=options.save_palette,
            )
Beispiel #8
0
 def _extract_colors(self):
     return extract_colors(self.get_source_path)