예제 #1
0
def mirror_bottom(source):
    crop_height = int(source.height / 2)
    cropped = source.crop((0, crop_height, source.width, 2 * crop_height))
    result = Image.new(source.mode, (source.width, 2 * crop_height))
    result.paste(flip(cropped), (0, 0))
    result.paste(cropped, (0, cropped.height))
    return result
예제 #2
0
    def __getitem__(self, idx):
        img_line = self.lines[idx]
        img_path, label = img_line.split(' ')
        img_path = os.path.abspath(os.path.join(self.root_dir, img_path))

        label = label.replace('\n', '')
        label = int(label)

        image = Image.open(img_path)

        if self.get_flipped:
            image_horizontal_flip = mirror(image)
            image_vertical_flip = flip(image)

            if self.transform:
                image = self.transform(image)
                image_horizontal_flip = self.transform(image_horizontal_flip)
                image_vertical_flip = self.transform(image_vertical_flip)

            return image, image_horizontal_flip, image_vertical_flip, label

        else:
            if self.transform:
                image = self.transform(image)

            return image, label
예제 #3
0
def flip_all(path: str):
    """ Vertically flip all images in a directory. Mainly for debugging purposes. """
    from tesseractXplore.image_glob import get_images_from_dir
    for source in get_images_from_dir(path):
        image = Image.open(source)
        image = flip(image)
        image.save(source)
        image.close()
예제 #4
0
def get_orientated_image(source, default_flip: bool = True) -> Image:
    """
    Load and rotate/transpose image according to EXIF orientation, if any. If missing orientation
    and the image was fetched from iNat, it will be vertically mirrored. (?)
    """
    image = Image.open(source)
    exif = image.getexif()

    if exif.get(EXIF_ORIENTATION_ID):
        image = exif_transpose(image)
    # TODO: In the future there may be more cases than just local images and remote images from iNat
    elif default_flip and isinstance(source, IOBase):
        image = flip(image)

    return image
예제 #5
0
    def requestPixmap(self, id, size):
        url = QUrl(id)
        query = QUrlQuery(url.query())
        map_dir = QDir(url.toLocalFile()).path()
        dot_size = round(float(query.queryItemValue('dot_size')))
        should_flip = query.queryItemValue('flip') in ['True', 'true']
        opacity = round(float(query.queryItemValue('opacity')))

        try:
            img, size = self._generate_heatmap(map_dir, dot_size, opacity)
            if should_flip:
                img = flip(img)
            p = QPixmap.fromImage(img.toqimage())
            return p, size
        except Exception:
            return QPixmap(), QSize(-1, -1)
예제 #6
0
def process_image_file(input_filepath, output_filepath, threshold):
    """Rotate and crop image.

    Args:
        input_filepath: Path to the source image.
        output_filepath: Path to the output image.

    Raises:
        OSError: file ops
    """
    if input_filepath == output_filepath:
        raise ValueError("Cannot overwrite input file.")
    input_image = Image.open(input_filepath)
    print(input_image.format, input_image.size, input_image.mode)
    flipped_image = flip(input_image)
    quad = find_mount_boundary_quad(flipped_image, threshold)
    d = distortion(*quad)
    print("distortion =", d)
    extracted_image = extract_quad(flipped_image, *quad)
    cropped_image = crop_border(extracted_image, 16)
    cropped_image.save(output_filepath)
예제 #7
0
    def __getitem__(self, idx):
        """ Returns a transformed image and filename. """
        image = Image.open(os.path.join(self.images_dir,
                                        self.image_files[idx]))

        # Return the image and it's flipped counterparts.
        if self.get_flipped:
            image_horizontal_flip = mirror(image)
            image_vertical_flip = flip(image)

            if self.transform:
                image = self.transform(image)
                image_horizontal_flip = self.transform(image_horizontal_flip)
                image_vertical_flip = self.transform(image_vertical_flip)
            return image, image_horizontal_flip, image_vertical_flip, self.image_files[
                idx]

        # Otherwise, just return the single image.
        else:
            if self.transform: image = self.transform(image)
            return image, self.image_files[idx]
예제 #8
0
def tile_img(img, width, height):
    # Opens an image
    bg = Image.open(img)
    # The width and height of the background tile
    bg_w, bg_h = bg.size
    # Creates a new empty image, RGB mode, and size 1000 by 1000
    new_im = Image.new('RGB', (width, height))

    # The width and height of the new image
    w, h = new_im.size

    # Iterate through a grid, to place the background tile
    for i in range(0, w, bg_w):
        for j in range(0, h, bg_h):
            # bg = Image.eval(bg, lambda x: x+(i+j)/1000)
            new_im.paste(bg, (i, j))

            # paste the image at location i, j:

    new_im = flip(new_im)

    return new_im.tobytes()
예제 #9
0
    filename = 'button-small-%s.png' % label.lower().replace(' ', '-')

# Hintergrund zusammenbauen
gradients = Image.open('generate-button-small.png')
y = 0
height = 23
background = gradients.crop((0, y, 200, height))
right = gradients.crop((203, 0, 208, height))

# Font laden und rausbekommen, wie gross der Button werden muss
ttf = '/Library/Fonts/Arial Narrow Bold.ttf'
font = truetype(ttf, 12, encoding='unic')
text_width, _ = font.getsize(label)
width = text_width + 2 * args.padding

# jetzt den Hintergrund in den Button reinkopieren
button = Image.new('RGBA', (width, height * 2))
button.paste(background, (0, 0))
button.paste(flip(background), (0, height))
button.paste(right, (width-5, 0))
button.paste(flip(right), (width-5, height))

# dann die Beschriftung reinmalen
draw = Draw(button)
draw.text((args.padding, 0+3), label, font=font, fill=args.text_color)
draw.text((args.padding, 23+4), label, font=font, fill=args.text_color)

# und schliesslich nur noch den Button speichern
#button.show()
button.save(filename, 'PNG')
예제 #10
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'ipetrash'

if __name__ == '__main__':
    from PIL import Image
    from PIL.ImageOps import flip

    im = Image.open('im.png')
    im = flip(im)
    im.show()