2

I am about to create an image with text on it. So far everything is working fine. Now, for the fine-tuning, I thought it would be nice to have the text with a gradient color.

This is the point I am currently at.

current

This is what I want to have.

want

Foxy
  • 21
  • 4
  • The text color seems to be white, but you want the text color to be darker shade of white, or gradient color? The image of what you want appears darker shade of white, not gradient. Gradient would be different shades together like this image: https://upload.wikimedia.org/wikipedia/commons/f/fa/Linear-gradient.svg – chickity china chinese chicken Dec 28 '18 at 00:36
  • Its only in the middle white. In the up and the down area of the Text there are a light green shaddow. – Foxy Dec 28 '18 at 01:02

2 Answers2

2

I've managed to generate following image:

enter image description here

using that script:

from PIL import Image, ImageFont, ImageDraw

OUTPUT_IMAGE = '53952270.png'
BG_COLOR = (0, 102, 0)
TEXT = 'STIEFELSTANGE'
TEXT_COLOR = (255, 255, 255)
SHADOW_COLOR = (231, 255, 227)

image = Image.new('RGB', (212, 45), color=BG_COLOR)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('impact', 36)
text_size = font.getsize(TEXT)
draw.text((0, 0), TEXT, font=font)
pixels = image.load()
size = image.size

x_list = []
y_list = []
for x in range(size[0]):
    for y in range(size[1]):
        if pixels[x, y] == TEXT_COLOR:
            x_list.append(x)
            y_list.append(y)

shadow_height = text_size[1]/4
for x, y in zip(x_list, y_list):
    if y < min(y_list) + shadow_height or y > max(y_list)-shadow_height:
        pixels[x, y] = SHADOW_COLOR

image.save(OUTPUT_IMAGE)
Alderven
  • 7,569
  • 5
  • 26
  • 38
0

I have got it worked. Unfortunately it is realy slow, for huge Textsizes (like 100).

Someone have an Ide to simplifie this Function?

It is creating This Text for example: example

    async def createTextWithOutline(self, image, x, y, text, font, outlineAmount, textColor, shadowColor, rarity):
    # create outline text
    drawObject = ImageDraw.Draw(image)
    for adjX in range(outlineAmount):
        for adjY in range(outlineAmount):
            drawObject.text((x + adjX, y + adjY), text, font=font, fill=shadowColor)
            drawObject.text((x + adjX, y - adjY), text, font=font, fill=shadowColor)
            drawObject.text((x - adjX, y + adjY), text, font=font, fill=shadowColor)
            drawObject.text((x - adjX, y - adjY), text, font=font, fill=shadowColor)
    drawObject.text((x, y), text, font=font, fill=textColor)
    if rarity != None:
        if 'legendary' == rarity:
            color = legendaryShadowColor
        elif 'epic' == rarity:
            color = epicShadowColor
        elif 'rare' == rarity:
            color = rareShadowColor
        elif 'uncommon' == rarity:
            color = uncommonShadowColor
        else:
            color = commonInnerColor
        x_list = []
        y_list = []
        pixels = image.load()
        size = drawObject.textsize(text, font=font)
        for i in range(int(x), int(x+size[0])):
            for j in range(int(y), int(y+size[1])):
                if pixels[i, j] == textColor + (255, ):
                    x_list.append(i)
                    y_list.append(j)
        shaderHeight = size[1]//3.5
        for i, j in zip(x_list, y_list):
            if j < min(y_list) + shaderHeight or j > max(y_list) - shaderHeight:
                pixels[i,j] = color + (255, )
Foxy
  • 21
  • 4