I want my vue Konva Text element to completely fill the given height, like i expect of a rectangle.
This is issue becomes obvious when pairing with text images, (converting svg text to canvas) that properly match the given dimension
<v-text :config={
x: 50,
y: 50,
width: 1000,
height: 60,
fontSize: 60,
fontStyle: 'bold',
fontFamily 'Campton Book'
text: 'WELT'
}
/>
<v-rect
:config="{ x: 50, y: 50, fill: 'black', height: 60, width: 200 }"
/>
Second Part, is there any way to always pixel perfectly align the left side with the border? the x coordinate matches the border
Is this due to font constraints? What am I missing?
I tried to get the height of the text node to fix this positioning but this is the given height passed down as props
Text is defined as having parts above and below the baseline. Above is termed 'ascenders' amd below is 'descenders', which are required for lower case letters like j y g.
Setting the text fontSize to 60 does not say 'whatever the string, make it fill a space 60px high'. Instead it says 'Make text in a 60px font', which makes space for the descenders because they will generally be required.
If you know for sure that the text will be all caps, then a solution is to measure the height used and increase the font size by a computed factor so that the font fills the line height.
To do this you'll need to get the glyph measurements as follows:
const lineHeight = 60; // following your code
// make your text shape here...left out for brevity
const metrics = ctx.measureText('YOUR CAPS TEXT');
capsHeight = Math.abs(metrics.actualBoundingBoxAscent)
fontSize = lineHeight * lineHeight / capsHeight;
If I got that right, your 60px case should give a value around 75. That's based on the convention that ascenders are 80% of the line height. Now you set the font size of your shape to this new value and you should be filling the entire line height.
Regarding the left-alignment, this relies on what the font gurus call the a-b-c widths. The left gap is the a-space, the b is the character width (where the ink falls) and the c-space is the same as the a-space but on the right hand side.
Sadly unless someone else can tell me I am wrong, you don't get a-b-c widths in the canvas TextMetric. There is a workaround which is rather convoluted but viable. You would draw the text in black on an off-screen canvas filled with a transparent background. Then get the canvas pixel data and walk horizontal lines from the left of the canvas inspecting pixels and looking for the first colored pixel. Once you find it you have the measurement to offset the text shape horizontally.
I used matplotlib to generate a keyboard. But after I wrote the code, I found that the picture displayed is incomplete. I have tried a few ways to solve the problem, like adjusting the dpi and changing the size of the picture. However, they are all useless.
this is the output
Here is the code:
ax=fig.add_subplot(111)
#ax.set_axis_off()
ax.plot(X,Y)
currentAxis = plt.gca()
generate_keyboard(currentAxis)
plt.text(0,0,"g")
plt.savefig(fname="3.jpg")
plt.show()`
def generate_keyboard(currentAxis):
rect1=patches.Rectangle((-0.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect2=patches.Rectangle((-1.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect3=patches.Rectangle((-2.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect4=patches.Rectangle((-3.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect5=patches.Rectangle((-4.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect6=patches.Rectangle((0.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect7=patches.Rectangle((1.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect8=patches.Rectangle((2.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect9=patches.Rectangle((3.5,-1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect10=patches.Rectangle((-1,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect11=patches.Rectangle((-2,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect12=patches.Rectangle((-3,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect13=patches.Rectangle((-4,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect14=patches.Rectangle((-5,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect15=patches.Rectangle((0,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect16=patches.Rectangle((1,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect17=patches.Rectangle((2,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect18=patches.Rectangle((3,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect19=patches.Rectangle((4,1),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect20=patches.Rectangle((-0.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect21=patches.Rectangle((-1.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect22=patches.Rectangle((-2.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect23=patches.Rectangle((-3.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect24=patches.Rectangle((0.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect25=patches.Rectangle((1.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
rect26=patches.Rectangle((2.5,-3),1,2,linewidth=2,edgecolor='r',facecolor='none')
currentAxis.add_patch(rect1)
currentAxis.add_patch(rect2)
currentAxis.add_patch(rect3)
currentAxis.add_patch(rect4)
currentAxis.add_patch(rect5)
currentAxis.add_patch(rect6)
currentAxis.add_patch(rect7)
currentAxis.add_patch(rect8)
currentAxis.add_patch(rect9)
currentAxis.add_patch(rect10)
currentAxis.add_patch(rect11)
currentAxis.add_patch(rect12)
currentAxis.add_patch(rect13)
currentAxis.add_patch(rect14)
currentAxis.add_patch(rect15)
currentAxis.add_patch(rect16)
currentAxis.add_patch(rect17)
currentAxis.add_patch(rect18)
currentAxis.add_patch(rect19)currentAxis.add_patch(rect20)
currentAxis.add_patch(rect21)
currentAxis.add_patch(rect22)
currentAxis.add_patch(rect23)
currentAxis.add_patch(rect24)
currentAxis.add_patch(rect25)
currentAxis.add_patch(rect26)
I have an image of spectacles with black background that I need to overlay onto a face image. To do so, I am taking the part of face image with shape same as spectacles; and put the colors of face image on black parts of the spectacles image. Then this small part of image can be put back. But I am not being able to take the correct colors from face image for the spectacles image. I tried this :
specs[np.where((hmd == [0,0,0,0]).all(axis=2))] = sub_face
specs image:
face image:
I need to put a resized specs image to face. I have resized specs image and also know the position where I will place the specs on face image. I just need to remove black background from specs and add relevant face colors so it looks like there are specs on face in a natural way.
Code I am using :
import cv2
specs = cv2.imread("rot_h0v0z0.png")
face = cv2.imread("~/Downloads/celebA/000001.png")
specs = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA)
sub_face = face[0:specs.shape[0], 0:specs.shape[1]]
specs[np.where((hmd == [0,0,0,0]).all(axis=2))] = sub_face
Was able to solve it, turned out pretty simple :P
(b,g,r) = cv2.split(specs)
indices = np.where(b == [0])
for i,j in zip(indices[0], indices[1]):
specs[i,j] = sub_face[i,j]
Was able to solve it, turned out pretty simple :P
(b,g,r) = cv2.split(specs)
indices = np.where(b == [0])
for i,j in zip(indices[0], indices[1]):
specs[i,j] = sub_face[i,j]
i want to show image RGB colour histogram in cocoa application. Please suggest possible way to do it with objective c or any third party library available to achieve this.
well this is a problem as RGB colors are 3D space so their histogram would lead to 4D plot which is something we do not really comprehend.
So the solution to this is to convert the 4D plot to 3D plot somehow. This can be done by sorting the colors by something that has some meaning. I will not speculate and describe what I am using. I use HSV color space and ignore the V value. This way I lose a lot of color shade info but it is still enough to describe colors for my purposes. This is how it looks like:
You can also use more plots with different V to cover more colors. For more info see:
HSV histogram
Anyway you can use any gradient sorting or any shape of your plot that is completely on you.
If you want pure RGB then you could adapt this and use RGB cube surface or map it on sphere and ignore the length from (0,0,0) (use unit vectors) something like this:
So if you R,G,B are in <0,1> you convert that to <-1,+1> then compute the spherical coordinates (ignoring radius) and you got your 2 variables instead of 3 which you can use as a plot (either as 2D globe base or 3D sphere ...).
Here C++ code how to do this (made from the HSV histogram):
picture pic0,pic1,pic2,zed;
const int na=360,nb=180,nb2=nb>>1; // size of histogram table
int his[na][nb];
DWORD w;
int a,b,r,g,x,y,z,l,i,n;
double aa,bb,da,db,dx,dy,dz,rr;
color c;
pic2=pic0; // copy input image pic0 to pic2
for (a=0;a<na;a++) // clear histogram
for (b=0;b<nb;b++)
his[a][b]=0;
for (y=0;y<pic2.ys;y++) // compute it
for (x=0;x<pic2.xs;x++)
{
c=pic2.p[y][x];
r=c.db[picture::_r]-128;
g=c.db[picture::_g]-128;
b=c.db[picture::_b]-128;
l=sqrt(r*r+g*g+b*b); // convert RGB -> spherical a,b angles
if (!l) { a=0; b=0; }
else{
a=double(double(na)*acos(double(b)/double(l))/(2.0*M_PI));
if (!r) b=0; else b=double(double(nb)*atan(double(g)/double(r))/(M_PI)); b+=nb2;
while (a<0) a+=na; while (a>=na) a-=na;
if (b<0) b=0; if (b>=nb) b=nb-1;
}
his[a][b]++; // update color usage count ...
}
for (n=0,a=0;a<na;a++) // max probability
for (b=0;b<nb;b++)
if (n<his[a][b]) n=his[a][b];
// draw the colored RGB sphere and histogram
zed =pic1; zed .clear(9999); // zed buffer for 3D
pic1.clear(0); // image of histogram
da=2.0*M_PI/double(na);
db=M_PI/double(nb);
for (aa=0.0,a=0;a<na;a++,aa+=da)
for (bb=-M_PI,b=0;b<nb;b++,bb+=db)
{
// normal
dx=cos(bb)*cos(aa);
dy=cos(bb)*sin(aa);
dz=sin(bb);
// color of surface (darker)
rr=75.0;
c.db[picture::_r]=double(rr*dx)+128;
c.db[picture::_g]=double(rr*dy)+128;
c.db[picture::_b]=double(rr*dz)+128;
c.db[picture::_a]=0;
// histogram center
x=pic1.xs>>1;
y=pic1.ys>>1;
// surface position
rr=64.0;
z=rr;
x+=double(rr*dx);
y+=double(rr*dy);
z+=double(rr*dz);
if (zed.p[y][x].dd>=z){ pic1.p[y][x]=c; zed.p[y][x].dd=z; }
// ignore lines if zero color count
if (!his[a][b]) continue;
// color of lines (bright)
rr=125.0;
c.db[picture::_r]=double(rr*dx)+128;
c.db[picture::_g]=double(rr*dy)+128;
c.db[picture::_b]=double(rr*dz)+128;
c.db[picture::_a]=0;
// line length
l=(xs*his[a][b])/(n*3);
for (double xx=x,yy=y,zz=z;l>=0;l--)
{
if (zed.p[y][x].dd>=z){ pic1.p[y][x]=c; zed.p[y][x].dd=z; }
xx+=dx; yy+=dy; zz+=dz; x=xx; y=yy; z=zz;
if (x<0) break; if (x>=xs) break;
if (y<0) break; if (y>=ys) break;
}
}
input image is pic0, output image is pic1 (histogram graph)
pic2 is copy of pic0 (remnant of old code)
zed is the Zed buffer for 3D display avoiding Z sorting ...
I use my own picture class for images so some members are:
xs,ys size of image in pixels
p[y][x].dd is pixel at (x,y) position as 32 bit integer type
clear(color) - clears entire image
resize(xs,ys) - resizes image to new resolution
As the sphere is a 3D object you should add rotation to it so all the surface is visible in time (or rotate with mouse or whatever) ...