how to render html code to transparent background image with python - python

I have locally an .html file and I need to render it to no background image.
what I do now:
import imgkit
imgkit.from_file('test.html', 'out.png')
replace_white_pixels_to_transparent("out.png") # my function that's doing what it name says :)
output before cleaning the white pixels looks like:
And after replacing the white pixels with transperent:
So I do get a no background image, and when upscaling the qulity and zoom in the webkit options the result is much better.
the problem is that even if the image quality is crazy high the white pixels cleaning process will leave some close to white pixels on the edges of the elements and it will delete necessary white pixels (eg. the inner of the shapes supposed to be white, or white backgroung image etc).
My question basically is how to render the html straight to no background image?
instead of rendering the elements on white background and then remove it.

this works pretty nicely:
import imgkit
kitoptions = {
"transparent": "",
}
imgkit.from_file('test.html', 'out.png', options=kitoptions)

Related

How to mask a video with an image on opencv

I want to create a mask over the whole human figure and replace it with an image that is responsive to the moving human figure. The image attached will give you a better idea of what is I mean.
The Image in the background will be still and won't move but the figure will seem like it punched a hole into the foreground. The figures however they move will go over the static background image and show the contents of the image. The figures, if they overlap as seen in the first image (see overlapping leg) should not break the mask and should still display the contents of the background.

Python - Remove Black Pixels originatin from the border of an image

I am very new to Image processing and I am trying to cleanse pictures similar to picture 1 of the Black Pixels originating from the border of the Image.
The Images are clipped Characters from a PDF which I try to process with tesseract to retieve the character. I already searched in Stackoverflow for answers, but only found resolutions to get rid of black borders.
I need to overwrite all the black pixels from the corners with white pixels, so tesseract can correctly recognize the character.
I cannot alter the Bounding Boxes used to clip the Characters, since the characters are centered in different ares of the BoundingBox and if i Cut the BoundingBox, i would cut some Characters like seen below
My first guess would have been to recursively track down pixels with a certain threshhold of black in them, but I am scared of computing time in that case and wouldn't really know where and how to start, except for using two two-dimensional arrays, one with the pixels, and one with an indicator whether i already worked on that pixel or not.
Help would be greatly appreciated.
Edit: some more pictures of cases, where black pixels from the edge need to be cleared:
Edit: Code-Snippet to create Border Image:
#staticmethod
def __get_border_image(image: Image) -> Image:
data = numpy.asarray(image)
border = cv2.copyMakeBorder(data, top=5, bottom=5, left=5, right=5, borderType=cv2.BORDER_CONSTANT)
return Image.fromarray(border)
Try like this:
artificially add a 1px wide black border all around the edge
flood-fill with white all black pixels starting at top-left corner
remove the 1px border from the first step (if necessary)
The point of adding the border is to allow the white to "flow" all around all edges of the image and reach any black items touching the edge.

How can I make the background of an image appear transparent without PIL?

I'm trying to make a GUI in tkinter that uses one image as an overlay on top of another, but when I place the image over the lower one, the transparent area of the image appears as grey.
I've searched for solutions to this, but all the results that I've found have pointed towards using PIL.
Is it possible for me to use transparent, or partially transparent images in the python tkinter module without using PIL?
You could use some basic photoshop tools like the magic wand tool to remove the background, but keep in mind, some PNG format images have a faint background. This is either in the from of a watermark, or the image background was rendered with a lower opacity than the rest of the image. Your GUI may also have a layer placed above the images by default. Does it appear on each image seperatly when loaded into the GUI?

PIL Image.getcolors() cannot distinguish between black and transparent?

I'm trying to count the number of distinct colors in an image using img.getcolors(). However, this does not distinguish between transparent and black pixels - they both report as one pixel colored [0,0,0].
How can I distinguish between transparent and black pixels? Many of the images I need to process are largely black on a transparent background.
For test purposes, I'm using a PNG I created which is half transparent, half black. len(img.getcolors()) is 1.
Embarrassing answer:
I was using convert('RGB') before calling getcolors(). Without the conversion, a 4-value tuple comes back with an alpha channel.

Weird result from pyqt example: an image viewer

I'm a beginner of PyQt, and I'm reading some examples in tutorials.
When I build an image viewer from example, I find the result is weird.
https://github.com/Werkov/PyQt4/blob/master/examples/widgets/imageviewer.py
Why there is a blank area on the top left of main window? How can I remove it?
The white rectangle is the self.imageLabel object. It is white because of the self.imageLabel.setBackgroundRole(QtGui.QPalette.Base) in the main window constructor. Just comment it out and the rectangle will disappear.
The side effect of this will be that if you open a transparent image, its background will be dark gray (as the background of the rest of the window inside) and not white. If you want it white, move the line self.imageLabel.setBackgroundRole(QtGui.QPalette.Base) to the end of the open method and indent it four spaces.

Categories