Python cv2.COLOR_RGBA2BGRA Examples

The following are 10 code examples of cv2.COLOR_RGBA2BGRA(). 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 also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: oxford_retrieval.py    From ArtMiner with MIT License 6 votes vote down vote up
def SkipIteration(I1, I2, saveQuality, out1, out2) : 
	
	I1RGBA = cv2.cvtColor(np.array(I1), cv2.COLOR_RGBA2BGRA)
	I2RGBA = cv2.cvtColor(np.array(I2), cv2.COLOR_RGBA2BGRA)
	
	mask1 = np.ones((I1RGBA.shape[0], I1RGBA.shape[1])) * 100
	mask2 = np.ones((I2RGBA.shape[0], I2RGBA.shape[1])) * 100
	
	I1RGBA[:, :, 3] = mask1
	I2RGBA[:, :, 3] = mask2
	
	ratio1 = max(max(I1RGBA.shape[0], I1RGBA.shape[1]) / float(saveQuality), 1)
	I1RGBA =imresize(I1RGBA, (int(I1RGBA.shape[0] / ratio1), int(I1RGBA.shape[1] / ratio1)))
	
	ratio2 = max(I2RGBA.shape[0], I2RGBA.shape[1]) / float(saveQuality)
	I2RGBA =imresize(I2RGBA, (int(I2RGBA.shape[0] / ratio2), int(I2RGBA.shape[1] / ratio2)))
	
	
	cv2.imwrite(out1, I1RGBA)
	cv2.imwrite(out2, I2RGBA)
	
## Blur the mask 
Example #2
Source File: pair_discovery.py    From ArtMiner with MIT License 6 votes vote down vote up
def SkipIteration(I1, I2, saveQuality, out1, out2) : 
	
	I1RGBA = cv2.cvtColor(np.array(I1), cv2.COLOR_RGBA2BGRA)
	I2RGBA = cv2.cvtColor(np.array(I2), cv2.COLOR_RGBA2BGRA)
	
	mask1 = np.ones((I1RGBA.shape[0], I1RGBA.shape[1])) * 100
	mask2 = np.ones((I2RGBA.shape[0], I2RGBA.shape[1])) * 100
	
	I1RGBA[:, :, 3] = mask1
	I2RGBA[:, :, 3] = mask2
	
	ratio1 = max(max(I1RGBA.shape[0], I1RGBA.shape[1]) / float(saveQuality), 1)
	I1RGBA =imresize(I1RGBA, (int(I1RGBA.shape[0] / ratio1), int(I1RGBA.shape[1] / ratio1)))
	
	ratio2 = max(I2RGBA.shape[0], I2RGBA.shape[1]) / float(saveQuality)
	I2RGBA =imresize(I2RGBA, (int(I2RGBA.shape[0] / ratio2), int(I2RGBA.shape[1] / ratio2)))
	
	
	cv2.imwrite(out1, I1RGBA)
	cv2.imwrite(out2, I2RGBA)
	
## Blur the mask 
Example #3
Source File: image.py    From ColorHistogram with MIT License 5 votes vote down vote up
def rgba2bgra(img):
    a = alpha(img)
    bgra = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA)
    bgra[:, :, 3] = a
    return bgra


## RGB to BGR. 
Example #4
Source File: image.py    From GuidedFilter with MIT License 5 votes vote down vote up
def rgba2bgra(img):
    a = alpha(img)
    bgra = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA)
    bgra[:, :, 3] = a
    return bgra


## RGB to BGR. 
Example #5
Source File: cv2_backend.py    From nnabla with Apache License 2.0 5 votes vote down vote up
def imsave(self, path, img, channel_first=False, as_uint16=False, auto_scale=True):
        """
        Save image by cv2 module.
        Args:
            path (str): output filename
            img (numpy.ndarray): Image array to save. Image shape is considered as (height, width, channel) by default.
            channel_first:
                This argument specifies the shape of img is whether (height, width, channel) or (channel, height, width).
                Default value is False, which means the img shape is (height, width, channel)
            as_uint16 (bool):
                If True, save image as uint16.
            auto_scale (bool) :
                Whether upscale pixel values or not.
                If you want to save float image, this argument must be True.
                In cv2 backend, all below are supported.
                    - float ([0, 1]) to uint8 ([0, 255])  (if img.dtype==float and auto_scale==True and as_uint16==False)
                    - float to uint16 ([0, 65535]) (if img.dtype==float and auto_scale==True and as_uint16==True)
                    - uint8 to uint16 are supported (if img.dtype==np.uint8 and auto_scale==True and as_uint16==True)
        """

        img = _imsave_before(img, channel_first, auto_scale)

        if auto_scale:
            img = upscale_pixel_intensity(img, as_uint16)

        img = check_type_and_cast_if_necessary(img, as_uint16)

        # revert channel order to opencv`s one.
        if len(img.shape) == 3:
            if img.shape[-1] == 3:
                img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            elif img.shape[-1] == 4:
                img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA)

        cv2.imwrite(path, img) 
Example #6
Source File: summary.py    From petridishnn with MIT License 5 votes vote down vote up
def create_image_summary(name, val):
    """
    Args:
        name(str):
        val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3.
            Can be either float or uint8. Range has to be [0,255].

    Returns:
        tf.Summary:
    """
    assert isinstance(name, six.string_types), type(name)
    n, h, w, c = val.shape
    val = val.astype('uint8')
    s = tf.Summary()
    imparams = [cv2.IMWRITE_PNG_COMPRESSION, 9]
    for k in range(n):
        arr = val[k]
        # CV2 will only write correctly in BGR chanel order
        if c == 3:
            arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
        elif c == 4:
            arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
        tag = name if n == 1 else '{}/{}'.format(name, k)
        retval, img_str = cv2.imencode('.png', arr, imparams)
        if not retval:
            # Encoding has failed.
            continue
        img_str = img_str.tostring()

        img = tf.Summary.Image()
        img.height = h
        img.width = w
        # 1 - grayscale 3 - RGB 4 - RGBA
        img.colorspace = c
        img.encoded_image_string = img_str
        s.value.add(tag=tag, image=img)
    return s 
Example #7
Source File: summary.py    From ADL with MIT License 5 votes vote down vote up
def create_image_summary(name, val):
    """
    Args:
        name(str):
        val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3.
            Can be either float or uint8. Range has to be [0,255].

    Returns:
        tf.Summary:
    """
    assert isinstance(name, six.string_types), type(name)
    n, h, w, c = val.shape
    val = val.astype('uint8')
    s = tf.Summary()
    imparams = [cv2.IMWRITE_PNG_COMPRESSION, 9]
    for k in range(n):
        arr = val[k]
        # CV2 will only write correctly in BGR chanel order
        if c == 3:
            arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
        elif c == 4:
            arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
        tag = name if n == 1 else '{}/{}'.format(name, k)
        retval, img_str = cv2.imencode('.png', arr, imparams)
        if not retval:
            # Encoding has failed.
            continue
        img_str = img_str.tostring()

        img = tf.Summary.Image()
        img.height = h
        img.width = w
        # 1 - grayscale 3 - RGB 4 - RGBA
        img.colorspace = c
        img.encoded_image_string = img_str
        s.value.add(tag=tag, image=img)
    return s 
Example #8
Source File: image_generator.py    From YOLOv2 with MIT License 5 votes vote down vote up
def overlay(src_image, overlay_image, pos_x, pos_y):
    # オーバレイ画像のサイズを取得
    ol_height, ol_width = overlay_image.shape[:2]

    # OpenCVの画像データをPILに変換
    # BGRAからRGBAへ変換
    src_image_RGBA = cv2.cvtColor(src_image, cv2.COLOR_BGR2RGB)
    overlay_image_RGBA = cv2.cvtColor(overlay_image, cv2.COLOR_BGRA2RGBA)

    # PILに変換
    src_image_PIL=Image.fromarray(src_image_RGBA)
    overlay_image_PIL=Image.fromarray(overlay_image_RGBA)

    # 合成のため、RGBAモードに変更
    src_image_PIL = src_image_PIL.convert('RGBA')
    overlay_image_PIL = overlay_image_PIL.convert('RGBA')

    # 同じ大きさの透過キャンパスを用意
    tmp = Image.new('RGBA', src_image_PIL.size, (255, 255,255, 0))
    # 用意したキャンパスに上書き
    tmp.paste(overlay_image_PIL, (pos_x, pos_y), overlay_image_PIL)
    # オリジナルとキャンパスを合成して保存
    result = Image.alpha_composite(src_image_PIL, tmp)

    return  cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGRA)

# 画像周辺のパディングを削除 
Example #9
Source File: summary.py    From tensorpack with Apache License 2.0 5 votes vote down vote up
def create_image_summary(name, val):
    """
    Args:
        name(str):
        val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3.
            Can be either float or uint8. Range has to be [0,255].

    Returns:
        tf.Summary:
    """
    assert isinstance(name, six.string_types), type(name)
    n, h, w, c = val.shape
    val = val.astype('uint8')
    s = tf.Summary()
    imparams = [cv2.IMWRITE_PNG_COMPRESSION, 9]
    for k in range(n):
        arr = val[k]
        # CV2 will only write correctly in BGR chanel order
        if c == 3:
            arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
        elif c == 4:
            arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
        tag = name if n == 1 else '{}/{}'.format(name, k)
        retval, img_str = cv2.imencode('.png', arr, imparams)
        if not retval:
            # Encoding has failed.
            continue
        img_str = img_str.tostring()

        img = tf.Summary.Image()
        img.height = h
        img.width = w
        # 1 - grayscale 3 - RGB 4 - RGBA
        img.colorspace = c
        img.encoded_image_string = img_str
        s.value.add(tag=tag, image=img)
    return s 
Example #10
Source File: gui.py    From idcardgenerator with GNU General Public License v3.0 4 votes vote down vote up
def generator():
    global ename, esex, enation, eyear, emon, eday, eaddr, eidn, eorg, elife, ebgvar
    name = ename.get()
    sex = esex.get()
    nation = enation.get()
    year = eyear.get()
    mon = emon.get()
    day = eday.get()
    org = eorg.get()
    life = elife.get()
    addr = eaddr.get()
    idn = eidn.get()

    fname = askopenfilename(initialdir=os.getcwd(), title=u'选择头像')
    # print fname
    im = PImage.open(os.path.join(base_dir, 'empty.png'))
    avatar = PImage.open(fname)  # 500x670

    name_font = ImageFont.truetype(os.path.join(base_dir, 'hei.ttf'), 72)
    other_font = ImageFont.truetype(os.path.join(base_dir, 'hei.ttf'), 60)
    bdate_font = ImageFont.truetype(os.path.join(base_dir, 'fzhei.ttf'), 60)
    id_font = ImageFont.truetype(os.path.join(base_dir, 'ocrb10bt.ttf'), 72)

    draw = ImageDraw.Draw(im)
    draw.text((630, 690), name, fill=(0, 0, 0), font=name_font)
    draw.text((630, 840), sex, fill=(0, 0, 0), font=other_font)
    draw.text((1030, 840), nation, fill=(0, 0, 0), font=other_font)
    draw.text((630, 980), year, fill=(0, 0, 0), font=bdate_font)
    draw.text((950, 980), mon, fill=(0, 0, 0), font=bdate_font)
    draw.text((1150, 980), day, fill=(0, 0, 0), font=bdate_font)
    start = 0
    loc = 1120
    while start + 11 < len(addr):
        draw.text((630, loc), addr[start:start + 11], fill=(0, 0, 0), font=other_font)
        start += 11
        loc += 100
    draw.text((630, loc), addr[start:], fill=(0, 0, 0), font=other_font)
    draw.text((950, 1475), idn, fill=(0, 0, 0), font=id_font)
    draw.text((1050, 2750), org, fill=(0, 0, 0), font=other_font)
    draw.text((1050, 2895), life, fill=(0, 0, 0), font=other_font)
    
    if ebgvar.get():
        avatar = cv2.cvtColor(np.asarray(avatar), cv2.COLOR_RGBA2BGRA)
        im = cv2.cvtColor(np.asarray(im), cv2.COLOR_RGBA2BGRA)
        im = changeBackground(avatar, im, (500, 670), (690, 1500))
        im = PImage.fromarray(cv2.cvtColor(im, cv2.COLOR_BGRA2RGBA))
    else:
        avatar = avatar.resize((500, 670))
        avatar = avatar.convert('RGBA')
        im.paste(avatar, (1500, 690), mask=avatar)
        #im = paste(avatar, im, (500, 670), (690, 1500))
        

    im.save('color.png')
    im.convert('L').save('bw.png')

    showinfo(u'成功', u'文件已生成到目录下,黑白bw.png和彩色color.png')