0

Okay, first thing first. This is a near duplicate of this question.

However, the issue I am facing is slightly different in a critical way.

In my application, I read a generic file name in, load said image, and display it. Where it gets tricky is I have overlay the appearance of being 'highlighted'. To do this, I was using the Image.blend() function, and blending it with a straight yellow image.

However, when dealing with blend, I was fighting the error that the two images are not compatible to be blended. To solve this, I opened the sample image I had in paint, and just pasted yellow over the whole thing, and saved it as a copy.

It just occurred to me that this will fail when a different type of image is read in by file name. Remember this needs to be generic.

So my question is: Instead of making a copy of the image manually, can I get python to generate one by copying the image and modifying it so it is solid yellow? Note: I do not need to save it after, so just making it happen is enough.

Unfortunately, I am not allowed to share my code, but hopefully the following will give an idea of what I need:

from PIL import Image 

desiredWidth = 800
desiredHeight = 600

primaryImage = Image.open("first.jpg").resize((desiredWidth, desiredHeight), Image.ANTIALIAS)

# This is the thing I need fixed:
highlightImage = Image.open("highlight.jpg").resize((desiredWidth, desiredHeight), Image.ANTIALIAS)

toDisplay = Image.blend(primaryImage, highlightImage, 0.3)

Thanks to anyone who can help.

Community
  • 1
  • 1
kirypto
  • 129
  • 2
  • 14

1 Answers1

0

Sounds like you want to make a new image:

fill_color = (255,255,0) #define the colour as (R,G,B) tuple

highlightImage = Image.new(primaryImage.mode, #same mode as the primary
                           primaryImage.size, #same size as the primary
                           fill_color)#and the colour defined above

this creates a new image with the same mode and size as the already opened image, but with a solid colour. Cheers.

Also if you are using Pillow instead of original PIL you can even get the color by name:

from PIL.ImageColor import getcolor

overlay = 'yellow'
fill_color = getcolor(overlay, primaryImage.mode)
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59