コード例 #1
0
 def img_to_str(self, img: np.ndarray):
     bio = BytesIO()
     img = Image.fromarray(img, mode='RGB')
     img.save(bio, format='png')
     val = bio.getvalue()
     bio.close()
     return val
コード例 #2
0
def jpeg_compression(x: np.ndarray,
                     strength: float,
                     xrange: tuple = None) -> np.ndarray:
    """
    Simulate changes due to JPEG compression for an image.

    Parameters
    ----------
    x
        Instance to be perturbed.
    strength
        Strength of compression (>1). Lower is actually more compressed.
    xrange
        Tuple with min and max data range.

    Returns
    -------
    Perturbed instance.
    """
    if not isinstance(xrange, tuple):
        xrange = (x.min(), x.max())

    if xrange[0] != 0 or xrange[1] != 255:
        x = (x - xrange[0]) / (xrange[1] - xrange[0]) * 255

    x = Image.fromarray(x.astype('uint8'), mode='RGB')
    output = BytesIO()
    x.save(output, 'JPEG', quality=strength)
    x = Image.open(output)
    x_jpeg = np.array(x, dtype=np.float32) / 255
    x_jpeg = x_jpeg * (xrange[1] - xrange[0]) + xrange[0]
    return x_jpeg
コード例 #3
0
def save_image(path: types.PathType, image: np.ndarray) -> None:
    """Saves the image to disk or gfile."""
    if not isinstance(path, gpath.GPath):
        path = gpath.GPath(path)

    with path.open("wb") as f:
        image = Image.fromarray(np.asarray(image))
        image.save(f, format=path.suffix.lstrip("."))
コード例 #4
0
ファイル: io.py プロジェクト: yeernet/web-avatarify
def image2bytes(image: np.ndarray,
                image_format: str = "jpeg",
                **save_kwargs) -> bytes:
    image = PIL.Image.fromarray(image)
    with BytesIO() as image_bytes:
        image.save(image_bytes, format=image_format, **save_kwargs)
        encoded_bytes = image_bytes.getvalue()
    return encoded_bytes
コード例 #5
0
    def writeimg(self, name: str, img: np.ndarray):
        if not self.has_recorded:
            self.has_recorded = True

        if not isinstance(img, np.ndarray):
            raise TypeError('The image needs to be a numpy array')

        path = f'{self.location}/images/{name}.jpg'

        img = Image.fromarray(img)
        img.save(path)
コード例 #6
0
def save_image(image_path: Path, image: np.ndarray) -> None:
    """Saves a provided image to a given path.

    Args:
        image_path (Path): Path of the image
        image (np.ndarray): Image to save
    """
    try:
        with open(image_path, 'w') as f:
            image.save(f)
    except OSError as e:
        logging.critical("Could not write to %s", image_path)
        raise OSError(e)
コード例 #7
0
ファイル: media.py プロジェクト: iffiX/machin
def create_image(image: np.ndarray, path: str, filename: str, extension: str = ".png"):
    """
    Args:
        image: A numpy array of shape (H, W, C) or (H, W), and with
            ``dtype`` = any float or any int.
            When a frame is float type, its value range should be [0, 1].
            When a frame is integer type, its value range should be [0, 255].
        path: Directory to save the image.
        filename: File name.
        extension: File extension.
    """
    image = numpy_array_to_pil_image(image)
    image.save(os.path.join(path, filename + extension))
コード例 #8
0
def image_save(filename: str, image: np.ndarray, mode=None):
    """
    save the image

    arguments:
        filename:   str
        image:      numpy array
        mode:       str, "cv2" or "pil", if not specified, use config.default_save_mode
    returns:
        none
    
    NOTE:
        - the image passed in should be in RGB order
        - use .png filetype as default if not specified
    """

    dirname = os.path.dirname(filename)
    basename = os.path.basename(filename)

    mode = config.default_save_mode if mode is None else mode

    # create folder
    if len(dirname) != 0:
        os.makedirs(dirname, exist_ok=True)

    # use png as default filetype
    if basename.find(".") == -1:
        basename = basename + ".png"

    image = np.copy(image)

    if mode.lower() == "cv2":
        # save image using cv2
        from cv2 import imwrite
        from cv2 import cvtColor, COLOR_RGB2BGR
        # convert RGB to BGR
        if len(image.shape) == 3 and image.shape[2] == 3:
            image = cvtColor(image, COLOR_RGB2BGR)
        imwrite(filename, image)
    elif mode.lower() == "pil":
        # read image using PIL
        from PIL import Image
        image = Image.fromarray(image)
        image.save(filename)
    else:
        raise ValueError(f"Only support cv2 and pil mode, but {mode} given")
コード例 #9
0
def save_image(path: str, image: np.ndarray, create_dir: bool = False,
        dtype: DTypeLike = np.uint8, **kwargs) -> None:
    # NOTE: Check destination path for existence
    # OpenCV silently fails if target directory does not exist
    dst_dir = osp.dirname(path)
    if dst_dir:
        if create_dir:
            os.makedirs(dst_dir, exist_ok=True)
        elif not osp.isdir(dst_dir):
            raise FileNotFoundError("Directory does not exist: '%s'" % dst_dir)

    if not kwargs:
        kwargs = {}

    # NOTE: OpenCV documentation says "If the image format is not supported,
    # the image will be converted to 8-bit unsigned and saved that way".
    # Conversion from np.int32 to np.uint8 is not working properly
    backend = _IMAGE_BACKEND
    if dtype == np.int32:
        backend = _IMAGE_BACKENDS.PIL
    if backend == _IMAGE_BACKENDS.cv2:
        # cv2.imwrite does not support paths that are not representable
        # in the locale encoding on Windows, so we write the image bytes
        # ourselves.

        ext = osp.splitext(path)[1]
        image_bytes = encode_image(image, ext, dtype=dtype, **kwargs)

        with open(path, 'wb') as f:
            f.write(image_bytes)
    elif backend == _IMAGE_BACKENDS.PIL:
        from PIL import Image

        params = {}
        params['quality'] = kwargs.get('jpeg_quality')
        if kwargs.get('jpeg_quality') == 100:
            params['subsampling'] = 0

        image = image.astype(dtype)
        if len(image.shape) == 3 and image.shape[2] in {3, 4}:
            image[:, :, :3] = image[:, :, 2::-1] # BGR to RGB
        image = Image.fromarray(image)
        image.save(path, **params)
    else:
        raise NotImplementedError()
コード例 #10
0
def encode_image(image: np.ndarray, ext: str, dtype: DTypeLike = np.uint8,
        **kwargs) -> bytes:
    if not kwargs:
        kwargs = {}

    if _IMAGE_BACKEND == _IMAGE_BACKENDS.cv2:
        import cv2

        params = []

        if not ext.startswith('.'):
            ext = '.' + ext

        if ext.upper() == '.JPG':
            params = [
                int(cv2.IMWRITE_JPEG_QUALITY), kwargs.get('jpeg_quality', 75)
            ]

        image = image.astype(dtype)
        success, result = cv2.imencode(ext, image, params=params)
        if not success:
            raise Exception("Failed to encode image to '%s' format" % (ext))
        return result.tobytes()
    elif _IMAGE_BACKEND == _IMAGE_BACKENDS.PIL:
        from PIL import Image

        if ext.startswith('.'):
            ext = ext[1:]

        params = {}
        params['quality'] = kwargs.get('jpeg_quality')
        if kwargs.get('jpeg_quality') == 100:
            params['subsampling'] = 0

        image = image.astype(dtype)
        if len(image.shape) == 3 and image.shape[2] in {3, 4}:
            image[:, :, :3] = image[:, :, 2::-1] # BGR to RGB
        image = Image.fromarray(image)
        with BytesIO() as buffer:
            image.save(buffer, format=ext, **params)
            return buffer.getvalue()
    else:
        raise NotImplementedError()
コード例 #11
0
    def drawAnchorMap(anchor: np.ndarray, df: pd.DataFrame, coef: float,
                      streetwidth_coef: float):
        def drawStreet(draw: ImageDraw.ImageDraw, geometry: list, coef: float,
                       width: int):

            street = pd.DataFrame(geometry)
            street['lon'] = coef * (street['lon'] - minlon)
            street['lat'] = coef * (street['lat'] - minlat)

            points = np.ravel(
                np.asarray((street['lon'].tolist(),
                            street['lat'].tolist())).T).tolist()
            draw.line(points, fill=255, width=width)

        minlat, maxlat = df['minlat'].min(), df['maxlat'].max()
        minlon, maxlon = df['minlon'].min(), df['maxlon'].max()

        anchor = Image.fromarray(anchor)

        draw = ImageDraw.Draw(anchor)
        fdrawStreet = np.vectorize(drawStreet)
        fdrawStreet(draw, df['geometry'].values, coef,
                    df['width'].values * streetwidth_coef)
        del draw

        anchor = anchor.transpose(Image.FLIP_TOP_BOTTOM)
        anchor.save('workshop/PIL.png')
        anchor = cv2.imread('workshop/PIL.png')

        rows, cols, patches = makeImagePatches(anchor, export=False)
        cleared_patches = []
        for patch in patches:
            cleared_patch = patch
            for i in range(10):
                cleared_patch = cv2.blur(cleared_patch, (3, 3))
                cleared_patch[cleared_patch < 255 / 2] = 0
                cleared_patch[cleared_patch >= 255 / 2] = 255
            cleared_patches.append(
                removeSmallComponents(cleared_patch, component_min_area=200))
        anchor = reconstituteImage(anchor, rows, cols, patches=cleared_patches)

        return anchor
コード例 #12
0
ファイル: main.py プロジェクト: ilim0t/bushitsuchan-PC
def convert_image_buffer(image: np.ndarray) -> bytes:
    image = Image.fromarray(image)
    with BytesIO() as byte_io:
        image.save(byte_io, format="JPEG")
        buffer = byte_io.getvalue()
    return buffer
コード例 #13
0
ファイル: image.py プロジェクト: der-Daniel/Bachelor-Thesis
def save_image(img: np.ndarray, name: str, path: str):
	img.save(path + '/' + name + '.png')
コード例 #14
0
def save_image(img: np.ndarray, path_to_output: str) -> None:
    # TODO: write description
    img = Image.fromarray(img)
    img.save(path_to_output)
コード例 #15
0
def _img_to_b64(im: np.ndarray) -> str:
    im = Image.fromarray(im[..., ::-1])
    buffered = io.BytesIO()
    im.save(buffered, format="JPEG")
    return base64.b64encode(buffered.getvalue()).decode()
コード例 #16
0
ファイル: utils.py プロジェクト: Stareven233/fzu_homework
def save_array_img(img: np.ndarray, filename: str) -> None:
    img = Image.fromarray(img)
    img.save(filename)
コード例 #17
0
def save_image(img: np.ndarray, outfile: str):
    """Save a numpy array as an image to an output file."""
    img = Image.fromarray(img)
    img.save(outfile)
コード例 #18
0
def save_image(imgfile: pathlib.Path,
               img: np.ndarray,
               cmin: Optional[float] = None,
               cmax: Optional[float] = None,
               ctype: str = 'gray',
               atol: float = 1e-5):
    """ Save an image to a file

    :param Path imgfile:
        The image file to save
    :param ndarray img:
        The 2D or 3D (RGB or RGBA) image to save
    :param float cmin:
        The minimum value in the image to save (the black level)
    :param float cmax:
        The maximum value in the image to save (the while level)
    :param str ctype:
        Check that the image is grey, RGB, or RGBA
    """
    # Clean up the input file types
    ctype = ctype.lower()
    imgfile = pathlib.Path(imgfile)

    # Force the image to be float for math purposes
    img = img.astype(np.float64)
    if cmin is None:
        cmin = np.nanmin(img)
    if cmax is None:
        cmax = np.nanmax(img)
    img[np.isnan(img)] = cmin

    # Scale the image using the contrast bounds
    crange = cmax - cmin
    if crange < atol:
        img = np.zeros_like(img)
    else:
        img = np.round((img - cmin) / crange * 255)

    # Force the image to be 8-bit
    img[img < 0] = 0
    img[img > 255] = 255
    img = img.astype(np.uint8)

    # Make sure that the image has the correct shape
    if img.ndim == 2:
        img = np.stack([img, img, img], axis=2)
    elif img.shape[2] == 1:
        img = np.concatenate([img, img, img], axis=2)

    # Make sure the expected type has the correct dimensions
    if ctype in ('grey', 'gray'):
        if img.ndim != 3 or img.shape[2] not in (3, 4):
            raise ValueError(
                f'Expected gray image, got image with shape {img.shape}')
    elif ctype in ('color', 'colour'):
        if img.ndim != 3 or img.shape[2] not in (3, 4):
            raise ValueError(
                f'Expected color image, got image with shape {img.shape}')
    elif ctype == 'rgb':
        if img.ndim != 3 or img.shape[2] != 3:
            raise ValueError(
                f'Expected RGB image, got image with shape {img.shape}')
    elif ctype == 'rgba':
        if img.ndim != 3 or img.shape[2] != 4:
            raise ValueError(
                f'Expected RGBA image, got image with shape {img.shape}')
    else:
        raise KeyError(f'Unknown color type "{ctype}"')

    # Write the actual images
    imgfile.parent.mkdir(exist_ok=True, parents=True)

    img = Image.fromarray(img)
    img.save(str(imgfile))
コード例 #19
0
    def __folder_thread_func(
        self,
        img: np.ndarray,
        zip_fs: zipfs.WriteZipFS,
        img_output_path_rel: Path,
        device: torch.device = None,
        task_upscaling: TaskID = None,
        progress: Progress = None,
        progress_text: str = "",
        output_zip_path: Path = None,
    ):
        # img = self.image(img, device, progress, progress_text)
        img = self.image(img, device)
        if device.type == "cuda":
            device_name = torch.cuda.get_device_name(device.index)
        else:
            device_name = "CPU"
        self.log.info(
            f'Upscaling "{img_output_path_rel.name}" using "{device_name}"')

        if self.imagemagick:
            img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA)
            img = WandImage.from_array(img)
            if self.jpg:
                # https://developers.google.com/speed/docs/insights/OptimizeImages
                # img.format = "jpg"
                img.sampling_factors = "4:2:0"
                img.interlace_scheme = "jpeg"
                img.colorspace = "srgb"
            else:
                img.format = "png"
            img.strip()
            if self.resize != 100:
                img.transform(resize=f"{self.resize}%")
        else:
            if self.resize != 100:
                width = int(img.shape[1] * self.resize / 100)
                height = int(img.shape[0] * self.resize / 100)
                img = cv2.resize(img, (width, height),
                                 interpolation=cv2.INTER_AREA)
        if self.zip:
            if self.imagemagick:
                buffer = io.BytesIO()
                img.save(file=buffer)
                buffer.seek(0)
            else:
                is_success, buffer = cv2.imencode(
                    ".jpg" if self.jpg else ".png", img)
                buffer = io.BytesIO(buffer)
            img_output_path_rel = img_output_path_rel.relative_to(
                output_zip_path.parent)
            if not zip_fs.isdir(img_output_path_rel.parent.as_posix()):
                zip_fs.makedirs(img_output_path_rel.parent.as_posix())
            zip_fs.writefile(
                img_output_path_rel.as_posix(),
                buffer,
            )
        else:
            if self.imagemagick:
                img.save(filename=str(img_output_path_rel.absolute()))
            else:
                cv2.imwrite(str(img_output_path_rel.absolute()), img)
        progress.advance(task_upscaling)