Python Forum
Error in blend_modes library
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in blend_modes library
#1
Hi,

I'm trying to use the blend_modes library, in order to blend 2 images. I've copied the example code from here:
https://pypi.org/project/blend-modes/

And I'm getting this error:

The blend_modes function "soft_light" received a numpy array with 3 layers for its argument "img_in". The function however expects a 4-layer array representing red, green, blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers to the "img_in" argument.

Can someone please help me fix this problem?
Thank you so much,
Tal.
Reply
#2
Post your error message and trace.

Did you copy the PIL or OpenCV example? Which package versions are you using? What Python are you using? What is the image file type?

Maybe you should just post your copied version of the code.
Reply
#3
(Jul-01-2023, 11:51 AM)deanhystad Wrote: Post your error message and trace.

Did you copy the PIL or OpenCV example? Which package versions are you using? What Python are you using? What is the image file type?

Maybe you should just post your copied version of the code.

The error is the same for both PIL & OpenCV.
The background image is jpg and the foreground image is png
I'll put the full code below.

The full error is:

Traceback (most recent call last):
File "D:\Dropbox\My Documents\github\python-poster-mockup\blend_test.py", line 17, in <module>
blended_img_float = soft_light(background_img_float, foreground_img_float, opacity)
File "D:\Dropbox\My Documents\github\python-poster-mockup\venv\lib\site-packages\blend_modes\blending_functions.py", line 175, in soft_light
assert_image_format(img_in, _fcn_name, 'img_in')
File "D:\Dropbox\My Documents\github\python-poster-mockup\venv\lib\site-packages\blend_modes\type_checks.py", line 57, in assert_image_format
raise TypeError(err_msg)
TypeError: The blend_modes function "soft_light" received a numpy array with 3 layers for its argument "img_in". The function however expects a 4-layer array representing red, green, blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers to the "img_in" argument.


The versions are:
aenum==3.1.15
blend-modes==2.1.0
click==8.1.3
colorama==0.4.6
deprecation==2.1.0
jsonpickle==3.0.1
markdown-it-py==3.0.0
mdurl==0.1.2
numpy==1.24.4
opencv-python==4.7.0.72
packaging==23.1
Pillow==9.5.0
Pillow-PIL==0.1.dev0
Pygments==2.15.1
rich==13.4.2
shellingham==1.5.0.post1
typer==0.9.0
typing_extensions==4.6.3

The code:
from PIL import Image
import numpy
from blend_modes import soft_light

# Import background image
background_img_raw = Image.open('./photos/template.jpg') # RGBA image
background_img = numpy.array(background_img_raw) # Inputs to blend_modes need to be numpy arrays.
background_img_float = background_img.astype(float) # Inputs to blend_modes need to be floats.

# Import foreground image
foreground_img_raw = Image.open('./photos/item1.png') # RGBA image
foreground_img = numpy.array(foreground_img_raw) # Inputs to blend_modes need to be numpy arrays.
foreground_img_float = foreground_img.astype(float) # Inputs to blend_modes need to be floats.

# Blend images
opacity = 0.7 # The opacity of the foreground that is blended onto the background is 70 %.
blended_img_float = soft_light(background_img_float, foreground_img_float, opacity)

# Convert blended image back into PIL image
blended_img = numpy.uint8(blended_img_float) # Image needs to be converted back to uint8 type for PIL handling.
blended_img_raw = Image.fromarray(blended_img) # Note that alpha channels are displayed in black by PIL by default.
# This behavior is difficult to change (although possible).
# If you have alpha channels in your images, then you should give
# OpenCV a try.

# Display blended image
blended_img_raw.show()
Reply
#4
Convert your jpg to a png, or add alpha information to the image you get from loading the jpeg (in PIL use putalpha()).
Reply
#5
(Jul-01-2023, 12:39 PM)deanhystad Wrote: Convert your jpg to a png, or add alpha information to the image you get from loading the jpeg (in PIL use putalpha()).

I've converted template.jpg to template.png - no change.
I've tried to add background_img_raw.putalpha(1), still no change...
Reply
#6
Looks like item1.png doesn't have an alpha channel either. The comments do say that both images must be RPGA.
Reply
#7
(Jul-01-2023, 02:39 PM)deanhystad Wrote: Looks like item1.png doesn't have an alpha channel either. The comments do say that both images must be RPGA.

Do you have any idea how can I create RGBA file? I've tried with Photoshop, and with https://convertio.co/, but without success.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python requests library .JSON() error mHosseinDS86 6 3,478 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Install any library via pip get an error cannot import name 'SCHEME_KEYS' from 'pip. Anldra12 2 10,700 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  Error about parser library and tree builder Led_Zeppelin 8 11,595 Jun-11-2021, 08:53 PM
Last Post: snippsat
  error while installing any library using pip in windows AkashKansal 1 4,422 Sep-24-2020, 07:51 AM
Last Post: buran
  libhdf5 library error (opencv-contrib) 1979gian 0 1,961 May-11-2020, 06:10 PM
Last Post: 1979gian
  Error message when importing jsonlines library vakil67 5 7,384 May-16-2018, 08:51 PM
Last Post: vakil67
  Fix Memory Error while installing a library for Qgis alexastorga 0 2,597 Apr-13-2018, 04:54 PM
Last Post: alexastorga
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,839 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020