def extract_imgs(img_path:str, label_path:str, output_path:str):
    '''读取图像并进行切分处理

    Args:
        img_path(str): 要处理的图像路径
        label_path(str): 对应的标签图像
        output_path(str): 输出目录
    '''
    print('Loading source image from {}.'.format(img_path))
    src_img = vipImage.new_from_file(img_path)
    
    label_img = None
    if label_path is not None:
        print('Loading label image from {}.'.format(label_path))
        label_img = vipImage.new_from_file(label_path)

    # 输出图片基本信息
    for img in [('Source Image', src_img), ('Label Image', label_img)]:
        print('{} info:'.format(img[0]))
        if img[1] is None: continue
        for key in ['width', 'height', 'bands']:
            print('\t{}: {}'.format(key, getattr(img[1], key)))

    # 将图像完全加载到内存,耗时可能会很长,内存小的机器慎用
    print('Loading image to memory, it may take a few minutes.')
    src_img = load_to_memory(src_img)
    label_img = load_to_memory(label_img) if label_img is not None else None
    crop_and_save(src_img, label_img, output_path, img_path)
Exemplo n.º 2
0
 def new_rgba(cls, width, height, ink=None):
     """Creates a new transparent RGBA image sized width × height."""
     # Creating a placeholder image with new_from_memory (equivalent of the
     # old vipsCC frombuffer) creates an image
     # which is a few byes different when written back to memory, which means
     # we can't store and retrieve it from its hash.  So instead we use a
     # temporary transparent 256x256 file for the initial image
     image = Image.new_from_file(
         os.path.join(os.path.dirname(os.path.realpath(__file__)),
                      'default_rgba.png'))
     image = image.copy(
         width=width,
         height=height,
         coding='none',  # No coding and no compression
         interpretation='srgb',
         xres=2.835,
         yres=2.835,  # Arbitrary 600 dpi
         xoffset=0,
         yoffset=0  # Working buffer
     )
     if ink is not None:
         image.draw_rect([ink.r, ink.g, ink.b, ink.a],
                         0,
                         0,
                         width,
                         height,
                         fill=True)
     return image
Exemplo n.º 3
0
    def from_gdal_dataset(cls, dataset, band):
        """
        Creates a new 1-band pyvips.Image from `dataset` at `band`

        dataset: GDAL Dataset
        band: Number of the band, starting from 1
        """
        with LibVips.disable_warnings():
            filename = dataset.GetFileList()[0]
            image1 = Image.new_from_file(filename)

            # Extract the band
            image2 = image1.extract_band((band - 1), n=1)

            # Cast to the right datatype, if necessary
            datatype = dataset.GetRasterBand(band).NumPyDataType
            if VImageAdapter(image2).NumPyType() == datatype:
                return image2
            types = dict((v, k) for k, v in cls.NUMPY_TYPES.items())
            image3 = Image.new_from_memory(image2.write_to_memory(),
                                           width=image2.width,
                                           height=image2.height,
                                           bands=1,
                                           format=types[datatype])
            return image3
Exemplo n.º 4
0
    def from_gdal_dataset(cls, dataset, band):
        """
        Creates a new 1-band pyvips.Image from `dataset` at `band`

        dataset: GDAL Dataset
        band: Number of the band, starting from 1
        """
        with LibVips.disable_warnings():
            filename = dataset.GetFileList()[0]
            image1 = Image.new_from_file(filename)

            # Extract the band
            image2 = image1.extract_band((band - 1), n=1)

            # Cast to the right datatype, if necessary
            datatype = dataset.GetRasterBand(band).NumPyDataType
            if VImageAdapter(image2).NumPyType() == datatype:
                return image2
            types = dict((v, k) for k, v in cls.NUMPY_TYPES.items())
            image3 = Image.new_from_memory(image2.write_to_memory(),
                                           width=image2.width,
                                           height=image2.height,
                                           bands=1, format=types[datatype])
            image3._buf = image2
            return image3
Exemplo n.º 5
0
def pyvips_loader(path: str,
                  access=pyvips.Access.RANDOM,
                  memory=True) -> Image:
    """
    Strange performance gain when using Access.RANDOM
    """
    return Image.new_from_file(path, access=access, memory=memory)
Exemplo n.º 6
0
    def __init__(self,
                 original_image,
                 output_raster_width,
                 output_raster_height,
                 total_seconds,
                 fps,
                 image_lib='cv'):
        self.file_path = original_image
        self.image_lib = None

        self.output_raster_width = output_raster_width
        self.output_raster_height = output_raster_height
        self.total_frames = int(float(total_seconds) * float(fps))

        self.image_lib = image_lib

        self.fps = fps

        if self.image_lib == 'pillow':
            print('using Pillow')
            self.original_image = PImage.open(self.file_path)
            self.original_image_width, self.original_image_height = self.original_image.size

        if self.image_lib == 'vips':
            from pyvips import Image as VImage
            from gi.repository import Vips
            print('using vips')
            self.original_image = VImage.new_from_file(self.file_path)
            self.original_image_width = self.original_image.width
            self.original_image_height = self.original_image.height

        self.output_file_name = 'output'

        if self.image_lib == 'cv':
            print('using cv')
            self.original_image = cv2.imread(self.file_path,
                                             cv2.IMREAD_UNCHANGED)
            self.original_image_height, self.original_image_width, _ = self.original_image.shape

        self.processed_frames = 0
        self.render_start_time = 0
        self.render_status = 'queued'
        self.render_estimated_seconds_remaining = 0
        self.render_estimated_total_seconds = 0
        self.render_fps = 0

        self.prores_mez = False

        self.ffmpeg = shutil.which('ffmpeg')
        if not self.ffmpeg:
            self.ffmpeg = '/usr/local/bin/ffmpeg'

        self.convert = shutil.which('convert')

        if not self.convert:
            self.convert = '/usr/local/bin/convert'

        self.water_mark = False
Exemplo n.º 7
0
    def load_image(self, image_id):
        """Load the specified image and return a [H,W,3] Numpy array.
        """
        # Load image with Vips because if ignores EXIF rotation (as it should).
        image = self.vips_image_to_numpy_array(VipsImage.new_from_file(self.image_info[image_id]['path']))
        # image = skimage.io.imread(self.image_info[image_id]['path'])
        # If grayscale. Convert to RGB for consistency.
        if image.ndim != 3 or image.shape[-1] == 1:
            image = skimage.color.gray2rgb(image.squeeze())
        # If has an alpha channel, remove it for consistency
        if image.shape[-1] == 4:
            image = image[..., :3]

        return image
Exemplo n.º 8
0
    def process_image(self, imageId, proposals):
        try:
            image = VipsImage.new_from_file(self.images[imageId])

            if bool(self.scale_factors) != False:
                scale_factor = self.scale_factors[imageId]
                image = image.resize(scale_factor)
                proposals = np.round(
                    np.array(proposals, dtype=np.float32) *
                    scale_factor).astype(int)

            masks = []
            for proposal in proposals:
                mask = np.zeros((image.height, image.width), dtype=np.uint8)
                cv2.circle(mask, (proposal[0], proposal[1]), proposal[2], 1,
                           -1)
                masks.append(mask.astype(np.bool))

            image_paths = []
            mask_paths = []
            mean_pixels = []

            for i, proposal in enumerate(proposals):
                image_file = '{}_{}.jpg'.format(imageId, i)
                image_crop, mask_crops = self.generate_crop(
                    image, masks, proposal)
                mask_file = self.save_mask(mask_crops, image_file,
                                           self.training_masks_path)
                image_crop.write_to_file(os.path.join(
                    self.training_images_path, image_file),
                                         strip=True,
                                         Q=95)
                image_paths.append(image_file)
                mask_paths.append(mask_file)
                np_crop = np.ndarray(buffer=image_crop.write_to_memory(),
                                     shape=[
                                         image_crop.height, image_crop.width,
                                         image_crop.bands
                                     ],
                                     dtype=np.uint8)
                mean_pixels.append(
                    np_crop.reshape((-1, image.bands)).mean(axis=0))

        except VipsError as e:
            print('Image #{} is corrupt! Skipping...'.format(imageId))

            return False, False, False

        return image_paths, mask_paths, mean_pixels
Exemplo n.º 9
0
def _preprocess_image(img_data: dict, local_file: str) -> Image:
    try:
        image = Image.new_from_file(local_file, access='sequential')
        max_height = config.DEFAULT_MAX_HEIGHT
        max_width = config.DEFAULT_MAX_WIDTH
        if img_data["copyrightStatus"] and \
                img_data["copyrightStatus"].lower() == "copyright":
            max_height = config.COPYRIGHT_MAX_HEIGHT
            max_width = config.COPYRIGHT_MAX_WIDTH
        if image.height > max_height or image.width > max_width:
            if image.height >= image.width:
                shrink_by = image.height / max_height
            else:
                shrink_by = image.width / max_width
            logger.debug(f"Resizing {img_data['id']} by {shrink_by}")
            image = image.shrink(shrink_by, shrink_by)
    except Error as pye:
        image = None
        Statistic.vips_err(img_data)
        logger.error(f"VIPs error - {pye.message}")
    return image
Exemplo n.º 10
0
 def new_rgba(cls, width, height, ink=None):
     """Creates a new transparent RGBA image sized width × height."""
     # Creating a placeholder image with new_from_memory (equivalent of the
     # old vipsCC frombuffer) creates an image
     # which is a few byes different when written back to memory, which means
     # we can't store and retrieve it from its hash.  So instead we use a
     # temporary transparent 256x256 file for the initial image
     image = Image.new_from_file(
         os.path.join(
             os.path.dirname(os.path.realpath(__file__)), 'default_rgba.png'
         )
     )
     image = image.copy(
         width=width, height=height,
         coding='none',  # No coding and no compression
         interpretation='srgb',
         xres=2.835, yres=2.835,  # Arbitrary 600 dpi
         xoffset=0, yoffset=0  # Working buffer
     )
     if ink is not None:
         image.draw_rect(
             [ink.r, ink.g, ink.b, ink.a], 0, 0, width, height, fill=True
         )
     return image
Exemplo n.º 11
0
 def image(self):
     if self._image is None:
         self._image = Image.new_from_file(self.inputfile)
     return self._image
Exemplo n.º 12
0
def deferred_vips():
    im = VipsImage.new_from_file(imagename)
    im.resize(0.4, kernel='cubic')
    im.write_to_buffer('.jpeg', Q=85)
def run_on_large_image(root_path: str, original_img: str, ckpt: str,
                       refine: bool):
    '''在一整张图上执行运算

    Args:
        root_path(str): 从大图中提取的小图所在目录
        original_img(str): 大图的路径
        ckpt(str): 检查点路径
    '''
    assert os.path.exists(root_path), '路径不存在: {}'.format(root_path)
    assert os.path.exists(original_img), '路径不存在: {}'.format(original_img)
    # 配置模型
    print('Setting up model.')
    model, device = setup(DeepLabV3Res101())
    print('Loading model from {}.'.format(ckpt))
    model, _ = restore_from(model, ckpt)
    model.eval()

    file_list = os.listdir(root_path)
    original_img_array = vipImage.new_from_file(original_img)
    ow, oh = [getattr(original_img_array, key) for key in ['width', 'height']]
    large_array = np.zeros((oh, ow), dtype=np.uint8)
    # 统计尺度
    scales = set()
    for file in file_list:
        scale, _, _, _ = extract_info_from_filename(file)
        scales.add(scale)
    # 使用最大尺度的图片作为索引
    max_scale = max(scales) if refine else min(scales)
    for file in tqdm(file_list, desc='Processing[r={}]'.format(refine)):
        scale, x, y, _ = extract_info_from_filename(file)
        if scale != max_scale: continue
        file_path = os.path.join(root_path, file)
        if refine:
            refined_image = run_and_refine_single_image(file_path,
                                                        ckpt,
                                                        False,
                                                        model=model,
                                                        device=device)
        else:
            refined_image = run_on_single_image(file_path,
                                                ckpt,
                                                model=model,
                                                device=device)
        rh, rw = refined_image.shape  # 根据生成图像大小填充最终图像
        large_array[x:x + rh, y:y + rw] = refined_image
    print('Saving to files...')
    # 为输出图片上色,方便查看
    colored_array = large_array * 85
    # 输出目录
    flag = 'refined' if refine else 'norefine'
    filename, extension = os.path.basename(original_img).rsplit('.', 1)
    output_file = os.path.join(
        './output', '{}_predict_{}_{}.{}'.format(filename, max_scale, flag,
                                                 extension))
    colored_file = os.path.join(
        './output',
        '{}_predict_colored_{}_{}.{}'.format(filename, max_scale, flag,
                                             extension))
    vipImage.new_from_memory(large_array.data, ow, oh, 1,
                             'uchar').write_to_file(output_file)
    vipImage.new_from_memory(colored_array.data, ow, oh, 1,
                             'uchar').write_to_file(colored_file)
    print('Large Image File has been saved to {} and {}'.format(
        output_file, colored_file))
Exemplo n.º 14
0
def genMobileScreenShot(s_1_text, s_2_text, s_3_text, s_4_text, s_7_text, input_img_folder_path, output_img_folder_path):
    if not isinstance(s_1_text, list):
        s_1_text = [s_1_text]
    if not isinstance(s_2_text, list):
        s_2_text = [s_2_text]
    if not isinstance(s_3_text, list):
        s_3_text = [s_3_text]
    if not isinstance(s_4_text, list):
        s_4_text = [s_4_text]
    if not isinstance(s_7_text, list):
        s_7_text = [s_7_text]

    mobile_screen_ds = {
        "screen_1": {
            "text_1_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_1_text[0],
                "abs_x": 127,
                "abs_y": 760,
                "max_width": 235,
                "max_height": 38
            }
        },
        "screen_2": {
            "text_1_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": None,
                "font_family": "Arial Regular",
                "font_size": 70,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (255,255,255),
                "text": s_2_text[0],
                "abs_x": 115,
                "abs_y": 316,
                "max_width": 221,
                "max_height": 12
            }
        },
        "screen_3": {
            "text_1_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": None,
                "font_family": "Arial Regular",
                "font_size": 78,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_3_text[0],
                "abs_x": 150,
                "abs_y": 315,
                "max_width": 300,
                "max_height": 14
            }
        },
        "screen_4": {
            "text_1_ds": {
                "hide_extra_text": False,
                "manage_abs_x": "center",
                "manage_abs_y": "center",
                "font_family": "Arial Regular",
                "font_size": 84,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (255,255,255),
                "text": s_4_text[0],
                "abs_x": 127,
                "abs_y": 350,
                "max_width": 330,
                "max_height": 30
            },
            "text_2_ds": {
                "hide_extra_text": True,
                "manage_abs_x": "center",
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 75,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (255,255,255),
                "text": s_4_text[1],
                "abs_x": 126,
                "abs_y": 385,
                "max_width": 75,
                "max_height": 11
            },
            "text_3_ds": {
                "hide_extra_text": True,
                "manage_abs_x": "center",
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 75,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (255,255,255),
                "text": s_4_text[2],
                "abs_x": 247,
                "abs_y": 385,
                "max_width": 75,
                "max_height": 11
            },
            "text_4_ds": {
                "hide_extra_text": True,
                "manage_abs_x": "center",
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 75,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (255,255,255),
                "text": s_4_text[3],
                "abs_x": 370,
                "abs_y": 385,
                "max_width": 75,
                "max_height": 11
            },
            "text_5_ds": {
                "hide_extra_text": True,
                "manage_abs_x": "center",
                "manage_abs_y": "center",
                "font_family": "Arial Regular",
                "font_size": 54,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_4_text[4],
                "abs_x": 118,
                "abs_y": 488,
                "max_width": 118,
                "max_height": 8
            },
            "text_6_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color":  (0,0,0),
                "text": s_4_text[5],
                "abs_x": 125,
                "abs_y": 625,
                "max_width": 235,
                "max_height": 38
            },
            "text_7_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_4_text[6],
                "abs_x": 125,
                "abs_y": 713,
                "max_width": 235,
                "max_height": 38
            },
            "text_8_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_4_text[7],
                "abs_x": 125,
                "abs_y": 803,
                "max_width": 235,
                "max_height": 38
            }
        },
        "screen_7": {
            "text_1_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_7_text[0],
                "abs_x": 124,
                "abs_y": 367,
                "max_width": 235,
                "max_height": 38
            },
            "text_2_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_7_text[1],
                "abs_x": 124,
                "abs_y": 485,
                "max_width": 235,
                "max_height": 38
            },
            "text_3_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_7_text[2],
                "abs_x": 124,
                "abs_y": 605,
                "max_width": 235,
                "max_height": 38
            },
            "text_4_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_7_text[3],
                "abs_x": 124,
                "abs_y": 726,
                "max_width": 235,
                "max_height": 38
            },
            "text_5_ds": {
                "hide_extra_text": True,
                "manage_abs_x": None,
                "manage_abs_y": "down",
                "font_family": "Arial Regular",
                "font_size": 77,
                "font_file": "fonts/Arial-regular.ttf",
                "text_color": (0,0,0),
                "text": s_7_text[4],
                "abs_x": 124,
                "abs_y": 844,
                "max_width": 235,
                "max_height": 38
            }
        }
    }

    img_list = ["1", "2", "3", "4", "7"]
    renderer_lib = 2
    for idx in img_list:
        output_file = getCurrentImgFilename(path=output_img_folder_path)
        if os.path.exists(output_file):
            continue
        img_base_file = "%s%s.jpg" % (input_img_folder_path, idx)
        if int(renderer_lib) == 1:
            background_image = Image.open(img_base_file)
        else:
            background_image = pyImage.new_from_file(img_base_file)
        for text_values in mobile_screen_ds["screen_%s" % idx].values():
            args = [text_values["text"], text_values["abs_x"], text_values["abs_y"], background_image, text_values["font_file"], text_values["text_color"], text_values["font_size"], text_values["max_width"]]
            background_image = renderTextOnImage(*args, manage_abs_y=text_values["manage_abs_y"], manage_abs_x=text_values["manage_abs_x"], renderer_lib=renderer_lib, max_height=text_values["max_height"], hide_extra_text=text_values["max_height"], font_family=text_values["font_family"])[1]
        if int(renderer_lib) == 1:
            background_image.save(output_file)
        else:
            background_image.write_to_file(output_file)

        background_image.clear
        print("mobile screentshot generated at: ", output_file)
Exemplo n.º 15
0
def genBannerImgs(text, input_img_folder_path, output_img_folder_path):    
    banner_absolute_values = {
        "text_1_ds": {
            "hide_extra_text": True,
            "manage_abs_x": None,
            "manage_abs_y": None,
            "font_family": "Poppins Regular",
            "font_size": 129,
            "font_file": "fonts/Poppins-Regular.ttf",
            "text_color": (0,0,0),
            "text": text[0],
            "abs_x": 402,
            "abs_y": 74,
            "max_width": 580,
            "max_height": 27
        },
        "text_2_ds": {
            "hide_extra_text": True,
            "manage_abs_x": None,
            "manage_abs_y": None,
            "font_family": "Poppins SemiBold",
            "font_size": 118,
            "font_file": "fonts/Poppins-SemiBold.ttf",
            "text_color": (0,0,0),
            "text": text[1],
            "abs_x": 402,
            "abs_y": 103,
            "max_width": 580,
            "max_height": 58
        },
        "text_3_ds": {
            "hide_extra_text": True,
            "manage_abs_x": None,
            "manage_abs_y": None,
            "font_family": "Poppins SemiBold",
            "font_size": 124,
            "font_file": "fonts/Poppins-SemiBold.ttf",
            "text_color": (0,0,0),
            "text": text[2],
            "abs_x": 402,
            "abs_y": 257,
            "max_width": 580,
            "max_height": 22
        }
    }

    banner_empty_imgs = ["1.jpg", "2.jpg", "3.jpg"]
    renderer_lib = 2
    img_file = "%s%s" % (input_img_folder_path, banner_empty_imgs[0])
    if int(renderer_lib) == 1:
        background_image = Image.open(img_file)
    else:
        background_image = pyImage.new_from_file(img_file)
    output_file = getCurrentImgFilename(path=output_img_folder_path)
    if os.path.exists(output_file):
        return
    for text_values in banner_absolute_values.values():
        args = [text_values["text"], text_values["abs_x"], text_values["abs_y"], background_image, text_values["font_file"], text_values["text_color"], text_values["font_size"], text_values["max_width"]]
        background_image = renderTextOnImage(*args, hide_extra_text=text_values["hide_extra_text"], manage_abs_y="down", renderer_lib=renderer_lib, max_height=text_values["max_height"], font_family=text_values["font_family"])[1]
    if int(renderer_lib) == 1:
        background_image.save(output_file)
    else:
        background_image.write_to_file(output_file)
    
    print("banner screentshot generated at: ", output_file)
Exemplo n.º 16
0
import sys
from pyvips import Image


if __name__ == "__main__":
	plane = sys.argv[1]
	quality = sys.argv[2]

	filename = plane + '.jpg'
	image = Image.new_from_file(filename, access='sequential')
	image.dzsave(plane, overlap=0, tile_size=256, depth='onetile', suffix='.jpg[Q=' + str(quality) + ']')
Exemplo n.º 17
0
Arquivo: OSF.py Projeto: NEONKID/PLSP
 def __init__(self, path):
     self.filename = path
     self.file = Image.new_from_file(path, access='sequential')
     self.slide = open_slide(filename=self.filename)
Exemplo n.º 18
0
 def image(self):
     if self._image is None:
         self._image = Image.new_from_file(self.inputfile)
     return self._image