#๐Ÿ”’ Ideas on how to make this work?

84 messages ยท Page 1 of 1 (latest)

mighty turtleBOT
#

@leaden bluff

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wintry coral
#

does the text.png have transparency?

#

You can duplicate this to act as the drop shadow

#

use the alpha to fill

#

1 sec

#
from PIL import Image


text = Image.open('hydro.png')

new_size = (
    text.size[0] + 50,
    text.size[1] + 50
    )

canvas = Image.new('RGB', new_size, '#551111')

#

so to start, I opened your text, and made a new canvas that I will eventually paste it onto

#

this will hold the text and the shadow effect

#

just to give some space for the shadow

#

you can adjust those numbers later on

#
shadow = Image.new('RGB', text.size, 'black')
shadow.putalpha(text.split()[-1])
#

moving on, I do 2 more things

#

I make another new image based on the original text size, and give it the original text's alpha

#

This simply creates an all black version of the original text

#

yup

#

these are all values you can tweak after the fact

#

first get it working, then adjust

#

Now all we need to do is paste the shadow, and the original text into the new canvas

#

shadow first since we want it on the bottom

#
canvas.paste(shadow, (25, 25), shadow)
canvas.paste(text, (10, 10), shadow)
#

You can blur the shadow using ImageFilter.GaussianBlur. Blending modes gets a bit trickier (but still doable)

#
shadow = shadow.filter(ImageFilter.GaussianBlur(5))
#

make sure to import ImageFilter at the top

#

Yeah I noticed that on mine too and I'm not entire sure what's causing it ๐Ÿ˜ฌ

#

I just tested that and no ๐Ÿ˜ฆ

#

still playing around to see if I can work it out

#

wait this might be right. I made the original canvas larger, not the shadow canvas

#

The issue thought is if the shadow canvas and text canvas don't match in size, you can't use putalpha

#

but that should be ok to move it to a larger canvas after

#

ok yup that was it

#

good call

#

might be a good time to start writing functions to help out with this

#

if you paste into 0, 0 of the new larger canvas, it shouldn't matter

#

oh nm it does flatten the top and left

#

you would have to paste it centered, but keep track of that offset position

#

so if the new canvas is 100 larger in each direction, then paste it centered which is an offset of 50

#

yeah, I would create a function for creating a blur image. The size of the blur would dictate how large the canvas needs to be

#

Do you just want opacity, or true multiply overlay?

#

I've done it in the past, but it required installing a new module, and converting the image to numpy

#

blend_modes is the module I used in the past

#
def numpy_img(self, img):
    #Converts PIL Image into numpy image array
    n_img = numpy.array(img)
    return n_img.astype(float)
#

I created this function to help convert

#

yeah, looks about right

#

the only things that need converting are the things you want to composite

#

every image is basically a grid of pixel values

#

numpy is a matrix of numbers

#

so a numpy image is just a matrix of those pixel values

#

these blending modes perform operations between the colour values

#

multiply literally multiplies the rgb pixel values together

#

these large matrix multiplications are much faster when being done with numpy

#

what exactly do you want the result to be? Where are you ultimately pasting this text?

#

Step 1 is create your text with drop shadow, which ideally doesn't require any actual blending modes

#

the blending modes need to happen once you actually want to place your drop shadow text on the final background

#

ahh

#

ok so you would just do the drop shadow, blend it with the background image, then place the final solid text on top

#

honestly I think just some simply opacity is enough

#

if you really want to know the end result, I would open some image editing software and design the look you're going for, then recreate it in code

#

I use Photopea which is like free online photoshop

#

if you want opacity, just use the blend method

#

that's fine, just take the offset into account

#

it's not that bad

#

make a canvas 100x100 larger for your drop shadow text

#
text = Image.open('hydro.png')

new_size = (
    text.size[0] + 100,
    text.size[1] + 100
    )
#

Just like we had before

#

when you paste your shadow into this, paste it to 50 instead

#

I would create variables for all of this to make the offset math easier

#
text = Image.open('hydro.png')

offset = 100

new_size = (
    text.size[0] + offset,
    text.size[1] + offset
    )
#

now when you paste your shadow into its canvas, paste it at offset / 2

#

yeah, that's smarter, haha

#

use blend instead of paste

#

you should have a canvas that's just your black, blurred drop shadow

#

then blend that into the background image

#

don't paste

#

blend

#

background image, pasted image, opacity (between 0.0 and 1.0)

#

new_bg = Image.blend(bg, shadow, 0.5)

#

yup

wintry coral
#

I think you'll have to blend it with the bg, then paste it to the old bg

mighty turtleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.