Python cv2 模块,IMWRITE_PNG_COMPRESSION 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用cv2.IMWRITE_PNG_COMPRESSION

项目:opencv-helpers    作者:abarrak    | 项目源码 | 文件源码
def save(path, image, jpg_quality=None, png_compression=None):
  '''
  persist :image: object to disk. if path is given, load() first.
  jpg_quality: for jpeg only. 0 - 100 (higher means better). Default is 95.
  png_compression: For png only. 0 - 9 (higher means a smaller size and longer compression time).
                  Default is 3.
  '''
  if isinstance(image, str):
    image = load(str)

  if jpg_quality:
    cv.imwrite(path, image, [cv.IMWRITE_JPEG_QUALITY, jpg_quality])
  elif png_compression:
    cv.imwrite(path, image, [cv.IMWRITE_PNG_COMPRESSION, png_compression])
  else:
    cv.imwrite(path, image)
项目:dm-challenge    作者:ping133    | 项目源码 | 文件源码
def gen_cropped_image(im, rx, cx, size, images, nameString, imageDir,
                      imgMetaNew, outputImageDir):

    imgMetaNew['orig'] = imgMetaNew.filename
    imgMetaNew.filename = imgMetaNew.filename.replace(
        ".png", "_" + nameString + ".png")

    croppedImage = cv2.resize(im[rx[0]:rx[1], cx[0]:cx[1], :], (size, size))
    params = list()
    params.append(cv2.IMWRITE_PNG_COMPRESSION)
    params.append(0)
    cv2.imwrite(outputImageDir + '/' +
                imgMetaNew.filename, croppedImage, params)

    images = images.append(imgMetaNew)

    return images
项目:Face_Recognition    作者:AkiraXD0712    | 项目源码 | 文件源码
def crop_save(image):
    crop_image = image[112 + 2:112 + FRAME_SIZE - 2, 192 + 2:192 + FRAME_SIZE - 2]
    timestamp = str(time.time())
    cv2.imwrite(
        'C:\\Users\Akira.DESKTOP-HM7OVCC\Desktop\database\\' + timestamp + '.png',
        crop_image,
        (cv2.IMWRITE_PNG_COMPRESSION, 0)
    )
项目:ros-interop    作者:mcgill-robotics    | 项目源码 | 文件源码
def from_msg(cls, msg):
        """Serializes a ROS Image message into a compressed PNG image.

        Args:
            msg: ROS Image or CompressedImage message.

        Returns:
            Compressed PNG image.

        Raises:
            CvBridgeError: On image conversion error.
        """
        if isinstance(msg, CompressedImage):
            # Decompress message.
            msg = cls.from_raw(msg.data)

        # Convert ROS Image to OpenCV image.
        bridge = CvBridge()
        img = bridge.imgmsg_to_cv2(msg)

        # Convert to PNG with highest level of compression to limit bandwidth
        # usage. PNG is used since it is a lossless format, so this can later
        # be retrieved as a ROS image without issue.
        compression = [cv2.IMWRITE_PNG_COMPRESSION, 9]
        png = cv2.imencode(".png", img, compression)[1].tostring()

        return png
项目:mxnet_tk1    作者:starimpact    | 项目源码 | 文件源码
def pack_img(header, img, quality=80, img_fmt='.jpg'):
    """pack an image into MXImageRecord

    Parameters
    ----------
    header : IRHeader
        header of the image record
    img : numpy.ndarray
        image to pack
    quality : int
        quality for JPEG encoding. 1-100, or compression for PNG encoding. 1-9.
    img_fmt : str
        Encoding of the image. .jpg for JPEG, .png for PNG.

    Returns
    -------
    s : str
        The packed string
    """
    assert opencv_available
    jpg_formats = set(['.jpg', '.jpeg', '.JPG', '.JPEG'])
    png_formats = set(['.png', '.PNG'])
    encode_params = None
    if img_fmt in jpg_formats:
        encode_params = [cv2.IMWRITE_JPEG_QUALITY, quality]
    elif img_fmt in png_formats:
        encode_params = [cv2.IMWRITE_PNG_COMPRESSION, quality]

    ret, buf = cv2.imencode(img_fmt, img, encode_params)
    assert ret, 'failed encoding image'
    return pack(header, buf.tostring())
项目:Vehicle_ReID    作者:starimpact    | 项目源码 | 文件源码
def pack_img(header, img, quality=95, img_fmt='.jpg'):
    """pack an image into MXImageRecord

    Parameters
    ----------
    header : IRHeader
        header of the image record
        header.label can be a number or an array.
    img : numpy.ndarray
        image to pack
    quality : int
        quality for JPEG encoding. 1-100, or compression for PNG encoding. 1-9.
    img_fmt : str
        Encoding of the image. .jpg for JPEG, .png for PNG.

    Returns
    -------
    s : str
        The packed string
    """
    assert opencv_available
    jpg_formats = ['.JPG', '.JPEG']
    png_formats = ['.PNG']
    encode_params = None
    if img_fmt.upper() in jpg_formats:
        encode_params = [cv2.IMWRITE_JPEG_QUALITY, quality]
    elif img_fmt.upper() in png_formats:
        encode_params = [cv2.IMWRITE_PNG_COMPRESSION, quality]

    ret, buf = cv2.imencode(img_fmt, img, encode_params)
    assert ret, 'failed encoding image'
    return pack(header, buf.tostring())
项目:Enchain    作者:Zhehua-Hu    | 项目源码 | 文件源码
def videoSlice(video_path, save_path, progressbarsetter=None, save_type="png", img_comp=0, start_idx=1):
    """

    :param video_path:
    :param save_path:
    :param save_type:
    :param img_comp: default0:
                    None Higher number increase compressive level
                    png[0-9], jpg[0-100]
    :return:
    """

    # For read Chinease-name video
    vid_handle = cv2.VideoCapture(video_path)
    # vid_handle = cv2.VideoCapture(video_path.encode('utf-8'))
    fps = vid_handle.get(cv2.CAP_PROP_FPS)
    count = vid_handle.get(cv2.CAP_PROP_FRAME_COUNT)
    size = (int(vid_handle.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(vid_handle.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    prefix = os.path.basename(save_path)
    idx = start_idx  # start from 000001.xxx
    cnt_idx = 1

    params = None
    suffix = None
    if save_type.upper() == "JPEG" or save_type.upper() == "JPG":
        img_type = int(cv2.IMWRITE_JPEG_OPTIMIZE)
        suffix = ".jpg"
        params = [img_type, img_comp]
    elif save_type.upper() == "PNG":
        img_type = int(cv2.IMWRITE_PNG_COMPRESSION)
        suffix = ".png"
        params = [img_type, img_comp]
    else:
        print("Do not support %s format!" % save_type)

    while True:
        ret, frame = vid_handle.read()
        if ret:
            cur_progress = cnt_idx/(count/100.0)
            if progressbarsetter is not None:
                progressbarsetter(cur_progress)
            print("Progress %.2f%%" % cur_progress)
            img_name = save_path + "/" + ("%06d" % idx) + suffix
            # print img_name
            print params
            cv2.imwrite(img_name, frame, params)
            idx += 1
            cnt_idx += 1
        else:
            break
    print("Slicing Done!")
    return count