Example #1
0
def URL_to_ASCII(url, width=80, height=24):
    # good bits from http://stevendkay.wordpress.com/2009/09/08/generating-ascii-art-from-photographs-in-python/
    # and http://blog.hardlycode.com/pil-image-from-url-2011-01/
    # TODO: MouseText
    greyscale = [
        " ", " ", ".,-", "_ivc=!/|\\~", "gjez2]/(YL)t[+T7Vf",
        "mdK4ZGbNDXY5P*Q", "W8KMA", "#%$"
    ]
    greyscale.reverse()  # white on black
    zonebounds = [36, 72, 108, 144, 180, 216, 252]
    img_file = urllib.urlopen(url)
    im = StringIO(img_file.read())
    im = Image.open(im)
    (w, h) = im.size
    # change width but not height
    new_width = int(width)
    new_height = int((float(width) / float(w)) * float(h))
    im = im.resize((new_width, new_height), Image.BILINEAR)
    im = im.convert("L")
    pic = ""
    for y in range(0, im.size[1]):
        for x in range(0, im.size[0]):
            lum = 255 - im.getpixel((x, y))
            row = bisect(zonebounds, lum)
            possibles = greyscale[row]
            pic = pic + possibles[random.randint(0, len(possibles) - 1)]
        pic = pic + "\n"
    return pic
Example #2
0
def URL_to_ASCII(url, width=80, height=24):
    # good bits from http://stevendkay.wordpress.com/2009/09/08/generating-ascii-art-from-photographs-in-python/
    # and http://blog.hardlycode.com/pil-image-from-url-2011-01/
    # TODO: MouseText
    greyscale = [" ", " ", ".,-", "_ivc=!/|\\~", "gjez2]/(YL)t[+T7Vf", "mdK4ZGbNDXY5P*Q", "W8KMA", "#%$"]
    greyscale.reverse()  # white on black
    zonebounds = [36, 72, 108, 144, 180, 216, 252]
    img_file = urllib.urlopen(url)
    im = StringIO(img_file.read())
    im = Image.open(im)
    (w, h) = im.size
    # change width but not height
    new_width = int(width)
    new_height = int((float(width) / float(w)) * float(h))
    im = im.resize((new_width, new_height), Image.BILINEAR)
    im = im.convert("L")
    pic = ""
    for y in range(0, im.size[1]):
        for x in range(0, im.size[0]):
            lum = 255 - im.getpixel((x, y))
            row = bisect(zonebounds, lum)
            possibles = greyscale[row]
            pic = pic + possibles[random.randint(0, len(possibles) - 1)]
        pic = pic + "\n"
    return pic
Example #3
0
 def getImageArtist(self, browser, *args, **kwargs):
     # Uses badge url to create a matplotlib artist where the
     # matplotlib.offsetbox.AnnotationBbox
     # is created by *args and **kwargs. Typical usage: (ix, day), frameon=False, box_alignment=(0, 0.5)
     fileName, extension = os.path.splitext(self.url)
     zoom = kwargs.pop('zoom', 1)
     try:
         img = browser.visitURL(self.url, extension=extension)
         img = StringIO(img)
         img = mpimg.imread(img, format=extension) # may raise error due to .jpg, support for other images than .png is added by PIL
         #if max(img.size) > maxdim:
         img.resize((maxdim, maxdim))
         # Add image:
         of = matplotlib.offsetbox.OffsetImage(img, zoom=zoom)
         ab = matplotlib.offsetbox.AnnotationBbox(of, *args, **kwargs)
         return ab                   # use plt.gca().add_artist(ab)
     except Exception:
         logger.exception('Badge image failed, url = %s, extension = %s', self.url, extension)
Example #4
0
def average_hash(im):
    if isinstance(im, str):
        im = StringIO(im)
    if not isinstance(im, Image.Image):
        im = Image.open(im)
    im = im.resize((8,8),Image.ANTIALIAS).convert('L')
    avg = reduce(lambda x, y: x + y, im.getdata()) / 64.0
    return reduce(lambda x, (y,z): x | (z << y),
                  enumerate(map(lambda i: 0 if i < avg else 1, im.getdata())),
                  0)
Example #5
0
def average_hash(im):
    if isinstance(im, str):
        im = StringIO(im)
    if not isinstance(im, Image.Image):
        im = Image.open(im)
    im = im.resize((8, 8), Image.ANTIALIAS).convert('L')
    avg = reduce(lambda x, y: x + y, im.getdata()) / 64.0
    return reduce(lambda x, (y, z): x | (z << y),
                  enumerate(map(lambda i: 0
                                if i < avg else 1, im.getdata())), 0)
Example #6
0
def get_image(width, height):
    quantity = database.picture.find({}).count()
    if quantity == 0:
        return 'No data'
    random_value = random.randrange(0, quantity)
    photo = database.picture.find({})[random_value]
    object_id = photo['picture_id']
    _file = fs.get(object_id)

    im = StringIO()
    im.write(_file.read())
    im.seek(0)
    im = Image.open(im)
    size = (width, height)
    im = im.resize(size, Image.ANTIALIAS)
    im_io = StringIO()
    format_file = _file.name.split('.')[1]
    if format_file == 'jpg':
        format_file = 'jpeg'
    im.save(im_io, format_file, quality=50)
    im_io.seek(0)

    return send_file(im_io, mimetype='image/jpeg')
Example #7
0
def get_image(width, height):
    quantity = database.picture.find({}).count()
    if quantity == 0:
        return 'No data'
    random_value = random.randrange(0, quantity)
    photo = database.picture.find({})[random_value]
    object_id = photo['picture_id']
    _file = fs.get(object_id)

    im = StringIO()
    im.write(_file.read())
    im.seek(0)
    im = Image.open(im)
    size = (width, height)
    im = im.resize(size, Image.ANTIALIAS)
    im_io = StringIO()
    format_file = _file.name.split('.')[1]
    if format_file == 'jpg':
        format_file = 'jpeg'
    im.save(im_io, format_file, quality=50)
    im_io.seek(0)

    return send_file(im_io, mimetype='image/jpeg')
Example #8
0
def scaleImage(image, width=None, height=None, direction="down",
        quality=88, result=None):
    """Scale an image to another size.

    The generated image is a JPEG image, unless the original is a PNG
    image. This is needed to make sure alpha channel information is
    not lost, which JPEG does not support.

    Three different scaling options are supported:
    
    * `up` scaling scales the smallest dimension up to the required size
      and scrops the other dimension if needed.
    * `down` scaling starts by scaling the largest dimension to the required
      size and scrops the other dimension if needed.
    * `thumbnail` scales to the requested dimensions without cropping. The
      resulting image may have a different size than requested. This option
      requires both width and height to be specified. `keep` is accepted as
      an alternative spelling for this option, but its use is deprecated.

    The `image` parameter can either be the raw image data (ie a `str`
    instance) or an open file.

    The `quality` parameter can be used to set the quality of the
    resulting image scales.

    The return value is a tuple with the new image, the image format and
    a size-tuple.  Optionally a file-like object can be given as the
    `result` parameter, in which the generated image scale will be stored.
    """
    if direction=="keep":
        direction="thumbnail"

    if direction=="thumbnail" and not (width and height):
        raise ValueError("Thumbnailing requires both width and height to be specified")
    elif width is None and height is None:
        raise ValueError("Either width or height need to be given")

    if isinstance(image, str):
        image=StringIO(image)
    image=PIL.Image.open(image)

    if image.mode=="1":
        # Convert black&white to grayscale
        image=image.convert("L")
    elif image.mode=="P":
        # Convert palette based images to 3x8bit+alpha
        image=image.convert("RGBA")
    elif image.mode=="CMYK":
        # Convert CMYK to RGB, allowing for web previews of print images
        image=image.convert("RGB")

    # When we create a new image during scaling we loose the format
    # information, so remember it here.
    image_format=image.format

    current_size=image.size
    # Determine scale factor needed to get the right height
    if height is None:
        scale_height=None
    else:
        scale_height=(float(height)/float(current_size[1]))
    if width is None:
        scale_width=None
    else:
        scale_width=(float(width)/float(current_size[0]))

    if scale_height==scale_width or direction=="thumbnail":
        # The original already has the right aspect ratio, so we only need
        # to scale.
        image.thumbnail((width, height), PIL.Image.ANTIALIAS)
    else:
        if direction=="down":
            if scale_height is None or (scale_width is not None and scale_width>scale_height):
                # Width is the smallest dimension (relatively), so scale up
                # to the desired width
                new_width=width
                new_height=int(round(current_size[1]*scale_width))
            else:
                new_height=height
                new_width=int(round(current_size[0]*scale_height))
        else:
            if scale_height is None or (scale_width is not None and scale_width<scale_height):
                # Width is the largest dimension (relatively), so scale up
                # to the desired width
                new_width=width
                new_height=int(round(current_size[1]*scale_width))
            else:
                new_height=height
                new_width=int(round(current_size[0]*scale_height))

        image.draft(image.mode, (new_width, new_height))
        image=image.resize((new_width, new_height), PIL.Image.ANTIALIAS)

        if (width is not None and new_width>width) or (height is not None and new_height>height):
            if width is None:
                left=0
                right=new_width
            else:
                left=int((new_width-width)/2.0)
                right=left+width
            if height is None:
                height=new_height
            image=image.crop((left, 0, right, height))

    if image_format=="PNG":
        format="PNG"
    else:
        format="JPEG"

    if result is None:
        result=StringIO()
        image.save(result, format, quality=quality, optimize=True)
        result=result.getvalue()
    else:
        image.save(result, format, quality=quality, optimize=True)
        result.seek(0)

    return (result, format, image.size)