python: reportlab, how to remove black borders from image - python

I am trying to generate a pdf file, using python reportlab, but is seems image is displayed with strange black border in pdf.
Here is the code:
# Standalone script to generate pdf lessons
from reportlab.pdfgen import canvas
def hello(c):
c.drawImage("./media/files/1.png", 0, 600, 350, 350)
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()
The image I am trying to add is here
Can somebody advice why the black line on the left have appeared, and how to fix it?

The problem isn't with a border, rather your chessboard has transparent pixels on the right and bottom sides, and reportlab isn't recognizing the alpha channel and is painting the transparent section as black:
Using mask='auto' tells drawImage to use the alpha channel in your PNG, so the background shows through:
c.drawImage("./media/files/1.png", 0, 600, 350, 350, mask='auto')

Related

Error when trying to change the default colour of rectangle in OpenCV

I'm working to create bounding boxes around the data I need to extract from an image. (I am using Jupyter notebook for python and OpenCV).
For this, I am drawing rectangles of desired coordinates and am using the following line of code:
cv2.rectangle(img,(50,82),(440,121), (0, 255, 0), 1)
This is for some reason giving only a black rectangle even though (0,255,0) is supposed to give green. What's more, if I use any other colour, for example (255,255,0), the box doesn't appear at all.
Thanks in advance for your help!
Is the image img that you are drawing on binary or grayscale? If so, make it color by merging the same image 3 times so that you have an RGB image with R=G=B. Or convert it Gray2BGR using cvtColor(). That is in Python/OpenCV do either
img = cv2.merge([img,img,img])
or
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGB)

Pillow - GIF File Size

The following program uses the Pillow package (3.4.2) to create a very simple GIF file. The file size is 11.2 KB.
from PIL import Image, ImageDraw
img = Image.new('P', (400, 300))
draw = ImageDraw.Draw(img)
draw.rectangle((0, 0, img.width, img.height), fill='black')
draw.line((10, 10, img.width-10, img.height-10), fill='cyan', width=5)
del draw
img.save('Test.gif')
If I open this file in Microsoft Paint and Save As with a different name, the file size becomes 1.90 KB.
Why such a big difference? Can I make Pillow use whatever format the Paint uses to get the same small size?
If your not making a animated GIF you can change img.save('Test.gif') to img.save('Test.png') the file size will then become 1.52KB.
You could use jpeg and optimise parameters as in here
img.save('Test.jpg',optimize=True,quality=95)

Python - Render a Slanted Font

This question is inline with the answer in my previous question in Stackoverflow.
I am creating a program that converts a text to image. I want to render it in using the font OCR A. But since OCR A font, has no corresponding font file for italics, I have to do the slanting of the upright font manually.
Upright Font
Slanted Font
Below is my initial code :
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import numpy as np
#Returns the text size in terms of width and height.
def getSize(txt, font):
testImg = Image.new('RGB', (1, 1))
testDraw = ImageDraw.Draw(testImg)
return testDraw.textsize(txt, font)
text = 'CNN Font Recognition Model'
font_size = 12
font = ImageFont.truetype('ocra.ttf' , font_size)
#ocra.ttf is a font file for OCR A Regular font (I have to render it slanted)
width, height = getSize(text, font)
#Creates an image with white background of constant size.
img = Image.new('RGB',(width, height), 'white')
d = ImageDraw.Draw(img)
d.text((0,0), text, fill='black', font=font)
img.save('slanted_ocr_a.png')
How do I manually slant the upright OCR A font? Thanks.
You can download a sample ocra.ttf file here.
This answer to a similar question is probably the easiest way, although it is not exactly what you want to do. Basically you render the upright font into an image then slant (shear) the entire image using PIL's Image.transform method.
To accomplish exactly what you are asking (slanting the font before imaging it) is possible, but would require a lot of work overriding PIL's truetype scheme to have the underlying font engine (FreeType) perform the slant transformation at the font level. PIL's truetype interface simply doesn't have a way to express a transform, so you'd have to override/patch it to pass that all the way down to FreeType where the font is set up.
Another option might be to skip PIL's truetype scheme and use a Python font-rendering library that allows you to set the font transform directly, then scrape the pixel data from that into your PIL Image. One such library is freetype-py.

Transparet pixels are being pasted as black in PIL

I paste an image with transparent pixels, but they are black in the final image
img = Image.new('RGBA', (100, 100), "white")
arbol16 = Image.open("arbol16.png")
img.paste(arbol16,( 0, 0, 16, 16))
img.show()
arbol16.png image file:
The paste method does not take into account the alpha of the image it's pasting; you need to add a mask parameter to control that. If the image you're pasting is RGBA you can just pass it twice.
arbol16 = arbol16.convert("RGBA")
img.paste(arbol16, (0, 0, 16, 16), arbol16)
I think you're (understandably) being confused by what the img.show() is displaying — which I believe is wrong (and may in fact be a bug). On my Windows system a temporary .BMP image is displayed, and it looks like the transparent pixels are black.
However if you add a line like img.save('arbol16_mod.png') at the end and then view that image file by manually opening it in some image file viewing program, such as Paint, Windows Photo Viewer, or Photoshop, the result is correct.

Drawing a semi-transparent rectangle on a jpeg file with wx.Bitmap

I need to load a jpeg->draw a semi-transparent rectangle->save the jpeg file
using wx.Bitmap of the python wx package.
But the rectangle appears fully opaque.
I'm using Windows 7 with 32bpp.
I checked and try the "Docs and Demos\demo\AlphaDrawing.py" wx demo, and it works well.
It draws correctly on wx.Panel a semi-transparent rectangle.
I checked on the internet for a solution for this problem, but none of the solutions worked.
I created a more simple example, to minimize the possibilities of error, and still didn't work.
Load a jpg->draw a semi-transparent rectangle->save as jpg file
wimg = wx.Image(r"N:\Images\Wallpapers\Processed\a.jpg", wx.BITMAP_TYPE_JPEG)
print wimg.HasAlpha()
wimg.InitAlpha()
print wimg.HasAlpha()
bmp = wimg.ConvertToBitmap()
print bmp.HasAlpha()
dc = wx.MemoryDC(bmp)
r, g, b = (34, 34, 34)
dc.SetPen(wx.Pen(wx.Colour(r, g, b, wx.ALPHA_OPAQUE)))
dc.SetBrush(wx.Brush(wx.Colour(r, g, b, 128)))
dc.DrawRectangle(100, 300, 200, 200)
bmp.SaveFile(r"N:\Images\Wallpapers\Processed\b.jpg", wx.BITMAP_TYPE_JPEG)
The print results are: False/True/True
And still the output it's a fully opaque rectangle
I known jpeg doesn't has alpha channels, but I don't want a 32bpp jpeg.
Just the output to show rectangle blended with the background.
Bitmap files have no Alpha channel. You must load a PNG file to use transparency.
"Unlike RGB data, not all images have an alpha channel and before using GetAlpha you should check if this image contains an alpha channel with HasAlpha. Note that currently only images loaded from PNG files with transparency information will have an alpha channel."
http://www.wxpython.org/docs/api/wx.Image-class.html

Categories

Resources