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.
Related
I'm drawing text into an image using a specific font, with Pillow for Python.
If the font I am using doesn't contain a glyph for a specific character, Pillow draws a ?. I'd like to get notified if the font is missing that specific item. Then I can either skip it or use a different font.
from PIL import Image, ImageDraw, ImageFont
image = Image.new("RGB", (100, 100), "white")
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("unifont_upper-14.0.04.ttf", size=16)
draw.text((1, 1), "ð’€€", font=font, fill="black")
image.save("cuneiform.png")
The specific font I'm using, doesn't contain "Cuneiform Sign A" so Pillow writes a ? - how can I get Pillow (or something else) to alert me that the character won't be drawn properly?
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)
I'm trying to reproduce a blurred/text-shadow effect using reportlab. Something like this.
So far my approach was to work with the filling color (the text itself or the background), but I don't think I'm going to be successful if I follow this path because the class only accepts an opacity (alpha) paramater besides the ones that defines the color itself. Now I'm trying to find some font that will mimic this effect.
So, It's possible to reproduce the desirable effect with reportlab? If yes, which approach should I use to achieve it?
Thank you very much!
I don't see any straightforward way to achieve a blurry effect as you can achieve with CSS or even with the PIL library using reportlab.
You can try one of the following fonts that more-less mimic this effect: Acidic, ExtraBlur, Erthqake Font, Static Buzz Font
, vtks trunkset Font and use the pdfmetrics.registerFont() and TTFont() methods (e.g. using the Static Buzz Font):
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas
canvas = Canvas('temp.pdf')
pdfmetrics.registerFont(TTFont('StaticBuzz', '/path/to/TTF-file/StaticBuzz.ttf')) #Change the path to the .ttf file.
canvas.setFont('StaticBuzz', 32)
canvas.drawString(0, 700, "Sample usage of StaticBuzz Font.")
canvas.save()
If it is an option (free standing text / headline), you could transform the text to a picture first and then blur it using the PIL library.
The filter can be adapted by setting radius so a strong blur effect can be achieved:
from PIL import ImageFilter
from PIL import Image
img = Image.open('test.png')
blurred_img = img.filter(ImageFilter.GaussianBlur(radius=5))
blurred_img.show()
I have an image I would like to add text to but I want to use both a different style font and a something bigger than the smallest thing known to mankind. Heck you can hardly read the text it's so darn small. I can print the text to the screen but I can't change the font style or the size of it as I don't know where the fonts are stored. I would think there would be more than one font/font size standardly available for use with Python/PIL. Where are they stored at? I am on a Linux.
Adapted from this answer:
image = Image.new("RGBA", (600,150), (255,255,255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("resources/HelveticaNeueLight.ttf", fontsize)
draw.text((10, 0), txt, (0,0,0), font=font)
Looks like you can specify any .ttf file and any fontsize you want using the above modules and functions.
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')