Pygame: Fill transparent areas of text with a color - python-3.x

I have several fonts that I would like to use that are basically outlines of letters, but the insides are transparent. How would I go about filling only the inside areas of these fonts with with a color? I suspect it would be using the special blitting RGBA_BLEND modes, but I am not familiar with their functionality.
Here is an example of the font I am using:
https://www.dafont.com/fipps.font?back=bitmap
Right now, I am simply rendering the font onto a surface, and I've written a helper function for that. Ideally I would be able to integrate this into my function.
def renderText(surface, text, font, color, position):
x, y = position[0], position[1]
width, height = font.size(text)
position = x-width//2, y-height//2
render = font.render(text, 1, color)
surface.blit(render, position)
Thank you so much for any help you can give me!

An option is to define a surface the size of the text, fill that with the color you want, and blit the text on that. For example you could do this:
text = font.render('Hello World!', True, (255, 255, 255)
temp_surface = pygame.Surface(text.get_size())
temp_surface.fill((192, 192, 192))
temp_surface.blit(text, (0, 0))
screen.blit(temp_surface, (0, 0))
This will create a temporary surface that should fill in the transparent pixels of a text surface. There is another option of using set_at() but its too expensive in processing power for what you are doing and is best used for pre-processing surfaces.
Im certain that a better option using BLEND_RGBA_MULT will come from a more experienced user. Im not too good at blending modes either.

Related

Is there any way to give gradient color to normal color property in flutter?

I am using Bar Chart of fl_chart plugin. I want to give gradient color to bar graph. Below is the line where I want give color.
BarChartRodData(y: 12, color: Color(0xFF639ed1),width: 12)
But it is not possible to set gradient color to the ##color## property unless it is colors.
Is there any way to set gradient color to color property?? Please help me..
If you pass just one color, the solid color will be used, or if you pass more than one color, we use gradient mode to draw. then the gradientFrom, gradientTo and gradientColorStops
For an example ,
final List<Color> color = <Color>[];
color.add(Colors.blue[50]);
color.add(Colors.blue[100]);
color.add(Colors.blue);
final List<double> stops = <double>[];
stops.add(0.0);
stops.add(0.5);
stops.add(1.0);
Please refer https://pub.dev/documentation/fl_chart/latest/fl_chart/BarChartRodData-class.html for BarChartRodData class variables

SwiftUI tint for image but preserving Black and White

I need to shift the color in an image, e.g. from gray to green. But only the parts that are not white and/or black...
For UIKit I had a handy extension:
// colorize image with given tint color
// this is similar to Photoshop's "Color" layer blend mode
// this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved
// white will stay white and black will stay black as the lightness of the image is preserved
func tint(tintColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw black background - workaround to preserve color of partially transparent pixels
context.setBlendMode(.normal)
UIColor.black.setFill()
context.fill(rect)
// draw original image
context.setBlendMode(.normal)
context.draw(self.cgImage!, in: rect)
// tint image (loosing alpha) - the luminosity of the original image is preserved
context.setBlendMode(.color)
tintColor.setFill()
context.fill(rect)
// mask by alpha values of original image
context.setBlendMode(.destinationIn)
context.draw(self.cgImage!, in: rect)
}
}
is there any way to generate the same functionality with the tint options in SwiftUI?
".colorMultiply" colors white as well.
".saturation(0.5)" directly generates a grayscale image.
You can continue to use your extension, like
var body: some View {
Image(uiImage: UIImage(named: "some")!.tint(tintColor: UIColor.red))
}
I was looking for the wrong keyword.
The actually way to do this in SwiftUI is hueRotation!
It only works with coloured images and not with grayscale images though.
See example below:
Color.blue
.hueRotation(.degrees(-45.0))

Filter out everything of a certain color using openCV

I have pictures of buildings I want to classify, and I want to get rid of the sky as I think it is messing with my classifier. I know that OpenCV has a function called inRange, that takes in the image and blacks out everything not in the range of the two color bounds that you provide. I was wondering if there was a function that literally did the opposite. Or another way I can accomplish what I want.
Thank you!
cv2.InRange creates a mask, which basically means it creates an image of the same size where the pixel values that are in the range are 255, and the values outside the range are 0.
https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#void%20inRange(InputArray%20src,%20InputArray%20lowerb,%20InputArray%20upperb,%20OutputArray%20dst)
If you want to have the opposite of that you can take the output of cv2.inRange and perform a bitwise_not:
https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#bitwise-not
If you want to then use that to black out the pixels in your original image you could do a bitwise_and:
https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#bitwise-and
So I would do something like:
mask = cv2.inRange(img, (255, 0, 0), (100, 0, 0)) # modify your thresholds
inv_mask = cv2.bitwise_not(mask)
no_sky = cv2.bitwise_and(img, inv_mask)

Display number in rectangle

Note that I am using Python3 and Phoenix.
I would like to display a number (double, but that does not matter now) formatted in some way (again, no matter what that way is) within a rectangle: almost a wx.StaticText but not editable by the user. This is to display some data coming from some hardware, such as a temperature.
Is there such a widget?
I tried with using the default wx.StaticText with a style but I must have done something wrong:
hbox = wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(parent, label=label)
title.SetLabelMarkup("<b>{}</b>".format(label))
hbox.Add(title, border=5)
value = wx.StaticText(parent, label="3.141592", style=wx.BORDER_RAISED)
value.SetWindowStyle(wx.BORDER_SIMPLE)
hbox.Add(value, border=5)
title = wx.StaticText(parent, label="\u2103")
hbox.Add(title, border=5)
Shows this on Linux (Fedora 24, GTK):
Wouldn't using a wx.TextCtrl set to read only do the job?
Temp = wx.TextCtrl(panel1, value="3.141592", style=wx.TE_READONLY)
Temp.SetBackgroundColour('green')
The simplest solution is to just use wxStaticText with a border style (e.g. wxBORDER_SIMPLE, ...). If you don't like the appearance this results in, it's pretty simple to make your own widget drawing whatever border you desire: just create a window, define its wxEVT_PAINT handler and draw the (presumably centered) text in it and a border outside of it.

About skia antialias

Recently I'm learning skia library(google open source 2d engine,be used on Android and chromium,etc.),Now I want to use it on windows instead of GDI+ dont support clip area with antialias,during it, I find a problem about pixel.
up is set antialias,down is not set antialias
the main code is:
paint.setStrokeWidth(1);
paint.setStyle(SkPaint::kStroke_Style);
paint.setAntiAlias(true);
canvas.drawRect(skrect,paint); //draw up rect
skrect.fTop += 110;
skrect.fBottom += 110;
paint.setAntiAlias(false);
canvas.drawRect(skrect, paint); //draw down rect
As you see,the same rect,if I not set Antialias,the boundary pixel is 1(I set strock width is 1),but if I set Antialias, the boundary pixel is 2,and it become a bit light,although I set color is black.
I dont konw why,anyone can tell me?
thk,
now,maybe I konw.
the skia library canvas should be like Html5`s canvas ,Canvas every line have an infinite thin "line", the width of the line from the center line to stretch,so if we draw a 1px line,in fact,it will fill two of 0.5 pixel point,but the display device dont allow it do that,so it will fill 2 pixel point,and set it color more light to differentiate real 2 pixels .
I will search more material to prove it.
This happens because OpenGL assumes that each pixel is centered at [0.5; 0.5]. The rasterizer have to draw indeed two pixels and interpolate the alpha. Read more here
If you would like to draw sharp 1px lines with antialiasing turned on just offset coordinates of a line or other figure with 0.5f.
For example a point at [10; 10] should have coordinates [10.5; 10.5] and so on

Resources