Python PIL.Image.ANTIALIAS Examples

The following are 30 code examples for showing how to use PIL.Image.ANTIALIAS(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module PIL.Image , or try the search function .

Example 1
Project: polapi-zero   Author: pierre-muth   File: polapizero_03.py    License: MIT License 7 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    title = 'Review Mode'
    # resize/dither to screen resolution and send to LCD
    image = Image.open(filename)
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(S_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
    image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
    # draw filename
    draw = ImageDraw.Draw(image_sized)
    font = ImageFont.truetype('arial.ttf', 18)
    draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
    draw.text((2, 2), title, fill='black', font=font)
    draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
    draw.text((290, 218), filename, fill='black', font=font)
    # display on LCD
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    lcd.write(image_sized.tobytes()) 
Example 2
Project: PiPark   Author: Humpheh   File: setup_selectarea.py    License: GNU General Public License v2.0 6 votes vote down vote up
def get_image_scaled(image):
    # Calculate the aspect ratio of the image
    image_aspect = float(image.size[1]) / float(image.size[0])

    # Scale the image
    image_scaled = (s.WINDOW_WIDTH, int(s.WINDOW_WIDTH * image_aspect))
    
    if image_aspect > 1:
        image_scaled = (int(s.WINDOW_HEIGHT / image_aspect), s.WINDOW_HEIGHT)

    coords = ((s.WINDOW_WIDTH - image_scaled[0])/2,
              (s.WINDOW_HEIGHT - image_scaled[1])/2,
              image_scaled[0], image_scaled[1])

    # Creat the resized image and return it and the co-ordinates.
    return ImageTk.PhotoImage(
        image.resize(image_scaled, Image.ANTIALIAS)), coords

# -----------------------------------------------------------------------------
# Function to output the co-ordinates of the boxes
# ----------------------------------------------------------------------------- 
Example 3
Project: neural-fingerprinting   Author: StephanZheng   File: download_images.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def download_image(image_id, url, x1, y1, x2, y2, output_dir):
    """Downloads one image, crops it, resizes it and saves it locally."""
    output_filename = os.path.join(output_dir, image_id + '.png')
    if os.path.exists(output_filename):
        # Don't download image if it's already there
        return True
    try:
        # Download image
        url_file = urlopen(url)
        if url_file.getcode() != 200:
            return False
        image_buffer = url_file.read()
        # Crop, resize and save image
        image = Image.open(BytesIO(image_buffer)).convert('RGB')
        w = image.size[0]
        h = image.size[1]
        image = image.crop((int(x1 * w), int(y1 * h), int(x2 * w),
                            int(y2 * h)))
        image = image.resize((299, 299), resample=Image.ANTIALIAS)
        image.save(output_filename)
    except IOError:
        return False
    return True 
Example 4
Project: polapi-zero   Author: pierre-muth   File: polapizero_05.py    License: MIT License 6 votes vote down vote up
def printImageFile(filename):
    print 'prints ', filename
    # resize to printer resolution and send to printer
    try:
        image = Image.open(filename)
        im_width, im_height = image.size
        if im_width > im_height:
            image = image.rotate(90)
        image.thumbnail((P_HEIGHT, P_WIDTH), Image.ANTIALIAS)
        printer.printImage(image, False)
        printer.justify('C')
        printer.setSize('S')
        printer.println("PolaPi-Zero")
        printer.feed(3)
    except IOError:
        print ("cannot identify image file", filename) 
Example 5
Project: polapi-zero   Author: pierre-muth   File: polapizero_09.py    License: MIT License 6 votes vote down vote up
def printImageFile(filename):
    print 'prints ', filename
    # resize to printer resolution and send to printer
    try:
        image = Image.open(filename)
        im_width, im_height = image.size
        if im_width > im_height:
            image = image.rotate(90, expand=1)
            im_width, im_height = image.size
        ratio = (PRINTER_HEIGHT/float(im_width))
        height = int((float(im_height)*float(ratio)))
        image = image.resize((PRINTER_HEIGHT, height), Image.ANTIALIAS)
        
        printer.printImage(image, False)
        printer.justify('C')
        printer.setSize('S')
        printer.println("PolaPi-Zero")
        printer.feed(3)
    except IOError:
        print ("cannot identify image file", filename) 
Example 6
Project: polapi-zero   Author: pierre-muth   File: polapizero_04.py    License: MIT License 6 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    title = 'Review Mode'
    # resize/dither to screen resolution and send to LCD
    image = Image.open(filename)
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(S_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
    image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
    # draw filename
    draw = ImageDraw.Draw(image_sized)
    font = ImageFont.truetype('arial.ttf', 18)
    draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
    draw.text((2, 2), title, fill='black', font=font)
    draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
    draw.text((290, 218), filename, fill='black', font=font)
    # display on LCD
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    lcd.write(image_sized.tobytes()) 
Example 7
Project: polapi-zero   Author: pierre-muth   File: polapizero_07.py    License: MIT License 6 votes vote down vote up
def printImageFile(filename):
    print 'prints ', filename
    # resize to printer resolution and send to printer
    try:
        image = Image.open(filename)
        im_width, im_height = image.size
        if im_width > im_height:
            image = image.rotate(90)
        image.thumbnail((PRINTER_HEIGHT, PRINTER_WIDTH), Image.ANTIALIAS)
        printer.printImage(image, False)
        printer.justify('C')
        printer.setSize('S')
        printer.println("PolaPi-Zero")
        printer.feed(3)
    except IOError:
        print ("cannot identify image file", filename) 
Example 8
Project: polapi-zero   Author: pierre-muth   File: polapizero_06.py    License: MIT License 6 votes vote down vote up
def printImageFile(filename):
    print 'prints ', filename
    # resize to printer resolution and send to printer
    try:
        image = Image.open(filename)
        im_width, im_height = image.size
        if im_width > im_height:
            image = image.rotate(90)
        image.thumbnail((P_HEIGHT, P_WIDTH), Image.ANTIALIAS)
        printer.printImage(image, False)
        printer.justify('C')
        printer.setSize('S')
        printer.println("PolaPi-Zero")
        printer.feed(3)
    except IOError:
        print ("cannot identify image file", filename) 
Example 9
Project: polapi-zero   Author: pierre-muth   File: polapizero_08.py    License: MIT License 6 votes vote down vote up
def printImageFile(filename):
    print 'prints ', filename
    # resize to printer resolution and send to printer
    try:
        image = Image.open(filename)
        im_width, im_height = image.size
        if im_width > im_height:
            image = image.rotate(90, expand=1)
            im_width, im_height = image.size
        ratio = (PRINTER_HEIGHT/float(im_width))
        height = int((float(im_height)*float(ratio)))
        image = image.resize((PRINTER_HEIGHT, height), Image.ANTIALIAS)
        
        printer.printImage(image, False)
        printer.justify('C')
        printer.setSize('S')
        printer.println("PolaPi-Zero")
        printer.feed(3)
    except IOError:
        print ("cannot identify image file", filename) 
Example 10
Project: License-Plate-Recognition   Author: wzh191920   File: surface.py    License: MIT License 6 votes vote down vote up
def get_imgtk(self, img_bgr):
		img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
		im = Image.fromarray(img)
		imgtk = ImageTk.PhotoImage(image=im)
		wide = imgtk.width()
		high = imgtk.height()
		if wide > self.viewwide or high > self.viewhigh:
			wide_factor = self.viewwide / wide
			high_factor = self.viewhigh / high
			factor = min(wide_factor, high_factor)
			wide = int(wide * factor)
			if wide <= 0 : wide = 1
			high = int(high * factor)
			if high <= 0 : high = 1
			im=im.resize((wide, high), Image.ANTIALIAS)
			imgtk = ImageTk.PhotoImage(image=im)
		return imgtk 
Example 11
Project: LearnPaddle2   Author: yeyupiaoling   File: use_infer_model.py    License: Apache License 2.0 6 votes vote down vote up
def load_image(file):
    im = Image.open(file)
    im = im.resize((32, 32), Image.ANTIALIAS)
    im = np.array(im).astype(np.float32)
    # PIL打开图片存储顺序为H(高度),W(宽度),C(通道)。
    # PaddlePaddle要求数据顺序为CHW,所以需要转换顺序。
    im = im.transpose((2, 0, 1))
    # CIFAR训练图片通道顺序为B(蓝),G(绿),R(红),
    # 而PIL打开图片默认通道顺序为RGB,因为需要交换通道。
    im = im[(2, 1, 0), :, :]  # BGR
    im = im / 255.0
    im = np.expand_dims(im, axis=0)
    return im


# 获取图片数据 
Example 12
Project: baidufm-py   Author: tdoly   File: c_image.py    License: MIT License 6 votes vote down vote up
def image_to_display(std_scr, path, login_win_row=0, start=None, length=None):
    """
    Display an image
    """
    login_max_y, login_max_x = std_scr.getmaxyx()
    rows, columns = os.popen('stty size', 'r').read().split()
    if not start:
        start = 2
    if not length:
        length = int(columns) - 2 * start
    i = Image.open(path)
    i = i.convert('RGBA')
    w, h = i.size
    i.load()
    width = min(w, length, login_max_x-1)
    height = int(float(h) * (float(width) / float(w)))
    height //= 2
    i = i.resize((width, height), Image.ANTIALIAS)
    height = min(height, 90, login_max_y-1)
    for y in xrange(height):
        for x in xrange(width):
            p = i.getpixel((x, y))
            r, g, b = p[:3]
            pixel_print(std_scr, login_win_row+y, start+x, rgb2short(r, g, b)) 
Example 13
Project: coursys   Author: sfu-fas   File: photos.py    License: GNU General Public License v3.0 6 votes vote down vote up
def possibly_resize(original):
    """
    Resize the jpeg to MAX_PHOTO_SIZE x MAX_PHOTO_SIZE if it's bigger. Otherwise, return as-is.
    """
    if Image is None:
        return original

    img = Image.open(io.BytesIO(original))
    w,h = img.size
    if w <= MAX_PHOTO_SIZE and h <= MAX_PHOTO_SIZE:
        # original is reasonably-sized
        return original

    # resize
    img.thumbnail((MAX_PHOTO_SIZE, MAX_PHOTO_SIZE), Image.ANTIALIAS)

    resize = io.BytesIO()
    img.save(resize, format='jpeg')
    return resize.getvalue() 
Example 14
Project: maple-file   Author: honmaple   File: router.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def gen_thumb_image(self, img_path, thumb_path, width):
        '''
        生成缩略图 or 展示图
        '''
        base_path = os.path.dirname(thumb_path)
        if not os.path.exists(base_path):
            os.makedirs(base_path)
        img = ImagePIL.open(img_path)
        if img.size[0] <= width:
            img.save(thumb_path, 'PNG')
            return thumb_path
        width = width
        height = float(width) / img.size[0] * img.size[1]
        img.thumbnail((width, height), ImagePIL.ANTIALIAS)
        img.save(thumb_path, 'PNG')
        return thumb_path 
Example 15
Project: script.tvguide.fullscreen   Author: primaeval   File: utils.py    License: GNU General Public License v2.0 6 votes vote down vote up
def autocrop_image(image, border = 0):
    from PIL import Image, ImageOps
    size = image.size
    bb_image = image
    bbox = bb_image.getbbox()
    if (size[0] == bbox[2]) and (size[1] == bbox[3]):
        bb_image=bb_image.convert("RGB")
        bb_image = ImageOps.invert(bb_image)
        bbox = bb_image.getbbox()
    image = image.crop(bbox)
    (width, height) = image.size
    width += border * 2
    height += border * 2
    ratio = float(width)/height
    cropped_image = Image.new("RGBA", (width, height), (0,0,0,0))
    cropped_image.paste(image, (border, border))
    #TODO find epg height
    logo_height = 450 / int(ADDON.getSetting('channels.per.page'))
    logo_height = logo_height - 2
    if ADDON.getSetting('program.channel.logo') == "false":
        cropped_image = cropped_image.resize((int(logo_height*ratio), logo_height),Image.ANTIALIAS)
    return cropped_image 
Example 16
Project: script.tvguide.fullscreen   Author: primaeval   File: ResizeLogos.py    License: GNU General Public License v2.0 6 votes vote down vote up
def autocrop_image(infile,outfile):
    infile = xbmc.translatePath(infile)
    image = Image.open(infile)
    border = 0
    size = image.size
    bb_image = image
    bbox = bb_image.getbbox()
    if (size[0] == bbox[2]) and (size[1] == bbox[3]):
        bb_image=bb_image.convert("RGB")
        bb_image = ImageOps.invert(bb_image)
        bbox = bb_image.getbbox()
    image = image.crop(bbox)
    (width, height) = image.size
    width += border * 2
    height += border * 2
    ratio = float(width)/height
    cropped_image = Image.new("RGBA", (width, height), (0,0,0,0))
    cropped_image.paste(image, (border, border))
    #TODO find epg height
    logo_height = 450 / int(ADDON.getSetting('channels.per.page'))
    logo_height = logo_height - 2
    cropped_image = cropped_image.resize((int(logo_height*ratio), logo_height),Image.ANTIALIAS)
    outfile = xbmc.translatePath(outfile)
    cropped_image.save(outfile) 
Example 17
Project: open-solution-salt-identification   Author: neptune-ai   File: callbacks.py    License: MIT License 6 votes vote down vote up
def _send_image_channels(self):
        self.model.eval()
        image_triplets = self._get_image_triplets()
        if self.image_nr is not None:
            image_triplets = image_triplets[:self.image_nr]
        self.model.train()

        for i, image_triplet in enumerate(image_triplets):
            h, w = image_triplet.shape[1:]
            image_glued = np.zeros((h, 3 * w + 20))

            image_glued[:, :w] = image_triplet[0, :, :]
            image_glued[:, (w + 10):(2 * w + 10)] = image_triplet[1, :, :]
            image_glued[:, (2 * w + 20):] = image_triplet[2, :, :]

            pill_image = Image.fromarray((image_glued * 255.).astype(np.uint8))
            h_, w_ = image_glued.shape
            pill_image = pill_image.resize((int(self.image_resize * w_), int(self.image_resize * h_)),
                                           Image.ANTIALIAS)

            neptune.send_image('{} predictions'.format(self.model_name), pill_image) 
Example 18
Project: tensorflow-image-wavenet   Author: Zeta36   File: testing.py    License: MIT License 6 votes vote down vote up
def load_generic_text(directory):
    '''Generator that yields image raw from the directory.'''
    files = find_files(directory)
    for filename in files:
        pic = _read_image(filename)
        pic = pic.resize((128,128), Image.ANTIALIAS)
        img = np.array(pic)
        print (img)
        img = img.reshape(-1, 1)
        print (img)
        img = img.reshape(128, 128)
        print(img)
        new_img = Image.fromarray(img)
        new_img.save('output_file.jpg')
        
        yield img, filename 
Example 19
Project: avocado-vt   Author: avocado-framework   File: ppm_utils.py    License: GNU General Public License v2.0 6 votes vote down vote up
def image_average_hash(image, img_wd=8, img_ht=8):
    """
    Resize and convert the image, then get image data as sequence object,
    calculate the average hash
    :param image: an image path or an opened image object
    """
    if not isinstance(image, Image.Image):
        image = Image.open(image)
    image = image.resize((img_wd, img_ht), Image.ANTIALIAS).convert('L')
    avg = reduce(lambda x, y: x + y, image.getdata()) / (img_wd * img_ht)

    def _hta(i):
        if i < avg:
            return 0
        else:
            return 1
    return reduce(lambda x, y_z: x | (y_z[1] << y_z[0]),
                  enumerate(map(_hta, image.getdata())), 0) 
Example 20
Project: kobo-predict   Author: awemulya   File: image_tools.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _save_thumbnails(image, path, size, suffix):
    nm = NamedTemporaryFile(suffix='.%s' % settings.IMG_FILE_TYPE)
    default_storage = get_storage_class()()
    try:
        # Ensure conversion to float in operations
        image.thumbnail(
            get_dimensions(image.size, float(size)), Image.ANTIALIAS)
    except ZeroDivisionError:
        pass
    try:
        image.save(nm.name)
    except IOError:
        # e.g. `IOError: cannot write mode P as JPEG`, which gets raised when
        # someone uploads an image in an indexed-color format like GIF
        image.convert('RGB').save(nm.name)
    default_storage.save(
        get_path(path, suffix), ContentFile(nm.read()))
    nm.close() 
Example 21
Project: kobo-predict   Author: awemulya   File: forms.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def save(self):
        photo = super(OrganizationForm, self).save()
        x = self.cleaned_data.get('x')
        y = self.cleaned_data.get('y')
        w = self.cleaned_data.get('width')
        h = self.cleaned_data.get('height')

        if x is not None and y is not None:
            image = Image.open(photo.logo)
            cropped_image = image.crop((x, y, w+x, h+y))
            resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
            # resized_image.save(photo.logo.path)
            resized_image_file = StringIO.StringIO()
            mime = mimetypes.guess_type(photo.logo.name)[0]
            plain_ext = mime.split('/')[1]
            resized_image.save(resized_image_file, plain_ext)
            default_storage.delete(photo.logo.name)
            default_storage.save(photo.logo.name, ContentFile(resized_image_file.getvalue()))
            resized_image_file.close()
        return photo 
Example 22
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def tensor_load_rgbimage(filename, ctx, size=None, scale=None, keep_asp=False):
    img = Image.open(filename).convert('RGB')
    if size is not None:
        if keep_asp:
            size2 = int(size * 1.0 / img.size[0] * img.size[1])
            img = img.resize((size, size2), Image.ANTIALIAS)
        else:
            img = img.resize((size, size), Image.ANTIALIAS)

    elif scale is not None:
        img = img.resize((int(img.size[0] / scale), int(img.size[1] / scale)), Image.ANTIALIAS)
    img = np.array(img).transpose(2, 0, 1).astype(float)
    img = F.expand_dims(mx.nd.array(img, ctx=ctx), 0)
    return img 
Example 23
Project: polapi-zero   Author: pierre-muth   File: interface_tests2.py    License: MIT License 5 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    # resize/dither to screen resolution and send to LCD
    image = Image.open(filename)
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(S_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
    image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    
    lcd.write(image_sized.tobytes()) 
Example 24
Project: polapi-zero   Author: pierre-muth   File: polapizero_05.py    License: MIT License 5 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    title = 'Review Mode'
    # resize/dither to screen resolution and send to LCD
    try:
        image = Image.open(filename)
    except IOError:
        print ("cannot identify image file", filename)
        image = Image.open('unidentified.jpg')
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(S_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
    image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
    # draw the filename
    draw = ImageDraw.Draw(image_sized)
    font = ImageFont.truetype('arial.ttf', 18)
    draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
    draw.text((2, 2), title, fill='black', font=font)
    draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
    draw.text((290, 218), filename, fill='black', font=font)
    # display on LCD
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    lcd.write(image_sized.tobytes()) 
Example 25
Project: polapi-zero   Author: pierre-muth   File: polapizero_03.py    License: MIT License 5 votes vote down vote up
def printImageFile(filename):
    print 'prints ', filename
    # resize to printer resolution and send to printer
    image = Image.open(filename)
    im_width, im_height = image.size
    if im_width > im_height:
        image = image.rotate(90)
    image.thumbnail((P_HEIGHT, P_WIDTH), Image.ANTIALIAS)
    printer.printImage(image, False)
    printer.feed(3) 
Example 26
Project: polapi-zero   Author: pierre-muth   File: polapizero_07.py    License: MIT License 5 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    title = 'Review Mode'
    # resize/dither to screen resolution and send to LCD
    try:
        image = Image.open(filename)
    except IOError:
        print ("cannot identify image file", filename)
        image = Image.open('unidentified.jpg')
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(SCREEN_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', SCREEN_SIZE, (0, 0, 0))
    image_sized.paste(image,((SCREEN_SIZE[0] - image.size[0]) / 2, (SCREEN_SIZE[1] - image.size[1]) / 2))
    # draw texts
    draw = ImageDraw.Draw(image_sized)
    font = ImageFont.truetype('arial.ttf', 18)
    draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
    draw.text((2, 2), title, fill='black', font=font)
    draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
    draw.text((290, 218), filename, fill='black', font=font)
    font = ImageFont.truetype('arial.ttf', 10)
    draw.rectangle([(300, 0), (399, 14)], fill=(255,255,255), outline=(0,0,0))
    draw.text((302, 2), hostIP, fill='black', font=font)
    # display on LCD
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    lcd.write(image_sized.tobytes()) 
Example 27
Project: polapi-zero   Author: pierre-muth   File: polapizero_01.py    License: MIT License 5 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    # resize/dither to screen resolution and send to LCD
    image = Image.open(filename)
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(S_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
    image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    
    lcd.write(image_sized.tobytes()) 
Example 28
Project: polapi-zero   Author: pierre-muth   File: polapizero_06.py    License: MIT License 5 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    title = 'Review Mode'
    # resize/dither to screen resolution and send to LCD
    try:
        image = Image.open(filename)
    except IOError:
        print ("cannot identify image file", filename)
        image = Image.open('unidentified.jpg')
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90)
    image.thumbnail(S_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', S_SIZE, (0, 0, 0))
    image_sized.paste(image,((S_SIZE[0] - image.size[0]) / 2, (S_SIZE[1] - image.size[1]) / 2))
    # draw the filename
    draw = ImageDraw.Draw(image_sized)
    font = ImageFont.truetype('arial.ttf', 18)
    draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
    draw.text((2, 2), title, fill='black', font=font)
    draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
    draw.text((290, 218), filename, fill='black', font=font)
    # display on LCD
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    lcd.write(image_sized.tobytes()) 
Example 29
Project: polapi-zero   Author: pierre-muth   File: polapizero_08.py    License: MIT License 5 votes vote down vote up
def displayImageFileOnLCD(filename):
    print 'displays ', filename
    title = 'Review Mode'
    # resize/dither to screen resolution and send to LCD
    try:
        image = Image.open(filename)
    except IOError:
        print ("cannot identify image file", filename)
        image = Image.open('unidentified.jpg')
    im_width, im_height = image.size
    if im_width < im_height:
        image = image.rotate(90, expand=1)
    image.thumbnail(SCREEN_SIZE, Image.ANTIALIAS)
    image_sized = Image.new('RGB', SCREEN_SIZE, (0, 0, 0))
    image_sized.paste(image,((SCREEN_SIZE[0] - image.size[0]) / 2, (SCREEN_SIZE[1] - image.size[1]) / 2))
    # draw texts
    draw = ImageDraw.Draw(image_sized)
    font = ImageFont.truetype('arial.ttf', 18)
    draw.rectangle([(0, 0), (115, 22)], fill=(255,255,255), outline=(0,0,0))
    draw.text((2, 2), title, fill='black', font=font)
    draw.rectangle([(279, 217), (399, 239)], fill=(255,255,255), outline=(0,0,0))
    draw.text((290, 218), filename, fill='black', font=font)
    font = ImageFont.truetype('arial.ttf', 10)
    draw.rectangle([(300, 0), (399, 14)], fill=(255,255,255), outline=(0,0,0))
    draw.text((302, 2), hostIP, fill='black', font=font)
    # display on LCD
    image_sized = ImageOps.invert(image_sized)
    image_sized = image_sized.convert('1') # convert image to black and white
    lcd.write(image_sized.tobytes()) 
Example 30
Project: toolium   Author: Telefonica   File: visual_test.py    License: Apache License 2.0 5 votes vote down vote up
def mobile_resize(self, img):
        """Resize image in iOS (native and web) and Android (web) to fit window size

        :param img: image object
        :returns: modified image object
        """
        if self.driver_wrapper.is_ios_test() or self.driver_wrapper.is_android_web_test():
            scale = img.size[0] / self.utils.get_window_size()['width']
            if scale != 1:
                new_image_size = (int(img.size[0] / scale), int(img.size[1] / scale))
                img = img.resize(new_image_size, Image.ANTIALIAS)
        return img