Example #1
0
def renderify(filename, cols=80):
    """
    Render images on the terminal with xterm 256 colors.
    """
    try:
        url = urllib.urlopen(filename)
        im = StringIO(url.read())
        im = Image.open(im)
        url.close()
    except:
        im = Image.open(filename)

    im.mode == 'RGBA'
    width, height = im.size
    dx = float(width) / cols
    dy = 2 * dx
    pixels = im.load()

    buff = ''
    y = 0
    while (y < height):
        x = 0
        while (x < width):
            i = round(x - 0.5) if x != 0 else x
            j = round(y - 0.5) if y != 0 else y
            if (len(pixels[i, j]) == 3) or (pixels[i, j][3] > 0):
                color = x256.from_rgb(list(pixels[i, j])[0:3])
                buff += '\033[48;5;%dm \033[0m' % color
            else:
                buff += ' '
            x += dx
        buff += '\n'
        y += dy
    return buff
Example #2
0
def image_unzip(path, dim_ordering, resize, num_classes):
    ignore_list = ('__', '.')
    format_check = False
    is_grayscale = False
    data, label, label_names = [], [], []

    with ZipFile(path) as archive:
        classCounter = 0
        all_folders = [
            f for f in archive.infolist() if f.filename.endswith('/')
        ]
        for i, cfolder in enumerate(all_folders):
            if cfolder.filename.startswith(ignore_list):
                continue
            all_files = [
                f.filename for f in archive.infolist()
                if f.filename.startswith(cfolder.filename)
                and not f.filename.endswith('/')
            ]

            for f in all_files:
                splits = f.split('/')
                if splits[1].startswith(ignore_list):
                    all_files.remove(f)

            if not format_check:
                img = archive.read(all_files[0])
                img = BytesIO(img)
                img = Image.open(img)
                is_grayscale = if_grayscale(img)
                format_check = True

            for j, cimage in enumerate(all_files):
                img = archive.read(cimage)
                img = BytesIO(img)
                if is_grayscale:
                    img = Image.open(img).convert('L')
                else:
                    img = Image.open(img).convert('RGB')
                img = img.resize((resize[0], resize[1]), PIL.Image.ANTIALIAS)
                img.load()
                cdata = np.asarray(img, dtype="int32")
                data.append(cdata)
                label.append(classCounter)
                label_names.append(cfolder.filename.split('/')[0])
            classCounter += 1

        data = np.asarray(data)
        label = np.asarray(label)
        label_names = np.asarray(label_names)

        if (len(data.shape) == 3):
            if dim_ordering == 'channels_last':
                data = data.reshape(data.shape[0], data.shape[1],
                                    data.shape[2], 1).astype('float32')
            elif dim_ordering == 'channels_first':
                data = data.reshape(data.shape[0], 1, data.shape[1],
                                    data.shape[2]).astype('float32')
        label = utils.to_categorical(label, num_classes)

        return data, label, label_names
Example #3
0
__author__ = "bijan"
from PIL import Image
from io import BytesIO
from authentication import Authentication

IMAGE_URL = "http://www.pythonchallenge.com/pc/return/cave.jpg"

auth = Authentication(IMAGE_URL)
image = BytesIO(auth.access())
image = Image.open(image)
pix = image.load()
new_image = Image.new(image.mode, image.size)
[new_image.putpixel([i, j], pix[i, j]) for j in range(0, image.size[1], 2) for i in range(0, image.size[0], 2)]
new_image.show()