In GNOME/gdk/gtkmm/cairo, a equivalent series of codes for MS Windows "TransparentBlt()"? - gnome

How can I port MFC
TotalDC.TransparentBlt(position.x, position.y, width, height, &image1DC, 0, 0, width, height, RGB(255, 255, 255));
to GNOME/gdk/gtkmm/cairo?
I know that MFC "BitBlt();" can be ported using
image1->copy_area(0,0,width,height,Total,position.x,position.y);
But this function does not designate the transparent RGB(r,g,b).

Related

What's wrong with my code that I can't see the transparency? [duplicate]

How can I draw a rectangle that has a color with an alpha?
I have:
windowSurface = pygame.display.set_mode((1000, 750), pygame.DOUBLEBUF)
pygame.draw.rect(windowSurface, pygame.Color(255, 255, 255, 128), pygame.Rect(0, 0, 1000, 750))
But I want the white rectangle to be 50% transparent, but the alpha value doesn't appear to be working.
pygame.draw functions will not draw with alpha. The documentation says:
Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.
What you can do is create a second surface and then blit it to the screen. Blitting will do alpha blending and color keys. Also, you can specify alpha at the surface level (faster and less memory) or at the pixel level (slower but more precise). You can do either:
s = pygame.Surface((1000,750)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
windowSurface.blit(s, (0,0)) # (0,0) are the top-left coordinates
or,
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128)) # notice the alpha value in the color
windowSurface.blit(s, (0,0))
Keep in mind in the first case, that anything else you draw to s will get blitted with the alpha value you specify. So if you're using this to draw overlay controls for example, you might be better off using the second alternative.
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
Check the Surface docs at the pygame site, especially the intro.
Unfortunately there is no good way to draw a transparent shape. See pygame.draw module:
A color's alpha value will be written directly into the surface [...], but the draw function will not draw transparently.
So you can't draw transparent shapes directly with the 'pygame.draw' module. The 'pygame.draw' module does not blend the shape with the target surface. You have to draw the shape on a surface (with RGBA format). Then you can blit (and thus blend) this surface. See Draw a transparent rectangles and polygons in pygame. Hence you need to do a workaround:
Create a pygame.Surface object with a per-pixel alpha format large enough to cover the shape.
Draw the shape on the _Surface.
Blend the Surface with the target Surface. blit() by default blends 2 Surfaces
For example 3 functions, which can draw transparent rectangles, circles and polygons:
def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def draw_circle_alpha(surface, color, center, radius):
target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.circle(shape_surf, color, (radius, radius), radius)
surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
surface.blit(shape_surf, target_rect)
Minimal example: repl.it/#Rabbid76/PyGame-TransparentShapes
import pygame
def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def draw_circle_alpha(surface, color, center, radius):
target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.circle(shape_surf, color, (radius, radius), radius)
surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
surface.blit(shape_surf, target_rect)
pygame.init()
window = pygame.display.set_mode((250, 250))
clock = pygame.time.Clock()
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (160, 160, 160), (192, 192, 192)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
pygame.draw.rect(background, color, rect)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(background, (0, 0))
draw_rect_alpha(window, (0, 0, 255, 127), (55, 90, 140, 140))
draw_circle_alpha(window, (255, 0, 0, 127), (150, 100), 80)
draw_polygon_alpha(window, (255, 255, 0, 127),
[(100, 10), (100 + 0.8660 * 90, 145), (100 - 0.8660 * 90, 145)])
pygame.display.flip()
pygame.quit()
exit()
the most i can do to help you is to show you how to draw a rectangle that is not filled in.
the line for the rectangle is:
pygame.draw.rect(surface, [255, 0, 0], [50, 50, 90, 180], 1)
the "1" means that it is not filled in
A way to do this is to instead of drawing a rectangle with pygame, you could create an image of a transparent rectangle by using a drawing program such as paint.net or Fire Alpaca.

How to remove background color from the image without affecting the text using opencv python

I tried to remove the black color from the image, if i tried the text also removed.
import cv2
img = cv2.imread('test.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
thresh1 = cv2.threshold(s, 100, 255, cv2.THRESH_BINARY)[1]
thresh2 = cv2.threshold(v, 150, 255, cv2.THRESH_BINARY)[1]
thresh2 = 255 - thresh2
mask = cv2.add(thresh1, thresh2)
result = img.copy()
result[mask == 128] = (255, 255, 255)
cv2.imwrite('clear.png', result)
Please give some ideas to get the text from that image.

Opencv fitellipse draws the wrong contour

I want to draw the ellipse contour around the given figure append below. I am not getting the correct result since the figure consist of two lines.
I have tried the following:-
Read the Image
Convert the BGR to HSV
Define the Range of color blue
Create the inRange Mask to capture the value of between lower and upper blue
Find the contour & Draw the fit ellipse.
Here is the source code-
import cv2
import numpy as np
image=cv2.imread('./source image.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_blue= np.array([75, 0, 0])
upper_blue= np.array([105, 255, 255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
res=cv2.bitwise_and(image,image,mask=mask)
_,contours,_=cv2.findContours(close,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
ellipse = cv2.fitEllipse(max(contours,key=cv2.contourArea))
cv2.ellipse(image,ellipse,(0,255,0),2)
cv2.imshow('mask',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
The figure/Image below show the Expected & Actual Output-
Expected & Actual display image
Source Image
Source Image
Output Contour Array
Contour file
I try to run your code on C++ and add erosion, dilatation and convexHull for result contour:
auto DetectEllipse = [](cv::Mat rgbImg, cv::Mat hsvImg, cv::Scalar fromColor, cv::Scalar toColor)
{
cv::Mat threshImg;
cv::inRange(hsvImg, fromColor, toColor, threshImg);
cv::erode(threshImg, threshImg, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)), cv::Point(-1, -1), 2);
cv::dilate(threshImg, threshImg, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)), cv::Point(-1, -1), 2);
std::vector<std::vector<cv::Point> > contours;
cv::findContours(threshImg, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
int areaThreshold = (rgbImg.cols * rgbImg.rows) / 100;
std::vector<cv::Point> allContours;
allContours.reserve(10 * areaThreshold);
for (size_t i = 0; i < contours.size(); i++)
{
if (contours[i].size() > 4)
{
auto area = cv::contourArea(contours[i]);
if (area > areaThreshold)
{
allContours.insert(allContours.end(), contours[i].begin(), contours[i].end());
}
}
}
if (allContours.size() > 4)
{
std::vector<cv::Point> hull;
cv::convexHull(allContours, hull, false);
cv::ellipse(rgbImg, cv::fitEllipse(hull), cv::Scalar(255, 0, 255), 2);
}
};
cv::Mat rgbImg = cv::imread("h8gx3.jpg", cv::IMREAD_COLOR);
cv::Mat hsvImg;
cv::cvtColor(rgbImg, hsvImg, cv::COLOR_BGR2HSV);
DetectEllipse(rgbImg, hsvImg, cv::Scalar(75, 0, 0), cv::Scalar(105, 255, 255));
DetectEllipse(rgbImg, hsvImg, cv::Scalar(10, 100, 20), cv::Scalar(25, 255, 255));
cv::imshow("rgbImg", rgbImg);
cv::waitKey(0);
Result looks correct:

PyGame Color Adjustment only working for one of three Sliders

I am working on an example from an older book, so far I have the program working but the color adjustment only works for the third "blue" slider. I tried troubleshooting by changing the range() found after get_pressed(). I changed it from 3 to 2 to 1 and they all behaved the same. I don't understand, I was thinking it would move with the changes.
import pygame as pg
from pygame.locals import *
from sys import exit
pg.init()
WIDTH, HEIGHT = 800, 600
screen = pg.display.set_mode((WIDTH, HEIGHT), 0, 32)
color = [127, 127, 127]
# Creates an image with smooth gradients
def createScales(height):
redScaleSurface = pg.surface.Surface((WIDTH, height))
greenScaleSurface = pg.surface.Surface((WIDTH,height))
blueScaleSurface = pg.surface.Surface((WIDTH,height))
for x in range(WIDTH):
c =int((x/(WIDTH-1.))*255.)
red = (c, 0, 0)
green = (0, c, 0)
blue = (0, 0, c)
line_rect = Rect(x, 0, 1, height)
pg.draw.rect(redScaleSurface, red, line_rect)
pg.draw.rect(greenScaleSurface, green, line_rect)
pg.draw.rect(blueScaleSurface, blue, line_rect)
return redScaleSurface, greenScaleSurface, blueScaleSurface
redScale, greenScale, blueScale = createScales(80)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
exit()
screen.fill((0, 0, 0))
# Draw the scales to the screen
screen.blit(redScale, (0, 00))
screen.blit(greenScale, (0, 80))
screen.blit(blueScale, (0, 160))
x, y = pg.mouse.get_pos()
# If the mouse was pressed on one of the sliders, adjust the color component
if pg.mouse.get_pressed()[0]:
for compononent in range(3):
if y > component*80 and y < (component+1)*80:
color[component] = int((x/(WIDTH-1.))*255.)
pg.display.set_caption("PyGame Color Test - " + str(tuple(color)))
# Draw a circle for each slider to represent the curret setting
for component in range(3):
pos = ( int((color[component]/255.)*(WIDTH-1)), component*80+40 )
pg.draw.circle(screen, (255, 255, 255), pos, 20)
pg.draw.rect(screen, tuple(color), (0, 240, WIDTH, HEIGHT/2))
pg.display.update()
On line 49, I made a typo and wrote component as compononent. Once fixed, I now have a neat interactive program for demonstrating computers' additive color model!

Python Cairo. How to draw a line around arc?

I want to draw a line around my arc but.. I can't. It doesn't fit.
It's my first day with cairo, so I don't know much. How do I do it right?
I'm drawing it on a drawing area of pyGTK+3 in linux.
//I want to fill and close_path for the other end as well but I've commented it out so that the problem is more easy to spot.
pos = pos
w = 10
rad = 70 - w/2
b_c = (34/255, 139/255, 34/255)
ctx.set_line_width(w)
ctx.set_source_rgb(b_c[0], b_c[1], b_c[2])
ctx.arc_negative(pos[0], pos[1], rad, 0, -pi/2)
ctx.arc_negative(pos[0], pos[1], rad, pi, pi/2)
#ctx.close_path()
ctx.stroke()
#ctx.stroke_preserve()
#ctx.fill()
ctx.set_line_width(3)
ctx.set_source_rgb(0, 0, 0)
ctx.arc_negative(pos[0], pos[1], rad +w/2 - 3/2, 0, -pi/2)
ctx.arc_negative(pos[0], pos[1], rad +w/2 - 3/2, pi, pi/2)
ctx.close_path()
ctx.stroke()
ctx.arc_negative(pos[0], pos[1], rad, 0, -pi/2)
ctx.arc_negative(pos[0], pos[1], rad, pi, pi/2)
ctx.close_path()
ctx.stroke()
draw_spikes(ctx, pos, rad + w/2)

Resources