Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8607507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:22:22+00:00 2026-06-12T03:22:22+00:00

I would like to add two 3D numpy arrays (RGB image arrays) with a

  • 0

I would like to add two 3D numpy arrays (RGB image arrays) with a 2D mask generated by some algorithms on a greyscale image. What is the best way to do this?

As an example of what I am trying to do:

from PIL import Image, ImageChops, ImageOps
import numpy as np

img1=Image.open('./foo.jpg')
img2=Image.open('./bar.jpg')

img1Grey=ImageOps.grayscale(img1)
img2Grey=ImageOps.grayscale(img2)

# Some processing for example:
diff=ImageChops.difference(img1Grey,img2Grey)
mask=np.ma.masked_array(img1,diff>1)

img1Array=np.asarray(im1)
img2Array=np.asarray(im2)

imgResult=img1Array+img2Array[mask]

I was thinking:
1) break up the RGB image and do each color separately
2) duplicate the mask into a 3D array

or is there a more pythonic way to do this?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:22:23+00:00Added an answer on June 12, 2026 at 3:22 am

    Wish I could add a comment instead of an answer. Anyhow:

    masked_array is not for making masks. It’s for including only the data outside the mask in calculations such as sum, mean, etc.. scientific statistical applications. It’s comprised of an array and the mask for the array.
    It’s probably NOT what you want.

    You probably just want a normal boolean mask, as in:

    mask = diff>1
    

    Then you’ll need to modify the shape so numpy broadcasts in the correct dimension, then broadcast it into the 3rd dimension:

    mask.shape = mask.shape + (1,)
    mask = np.broadcast_arrays(img1Array, mask)[1]
    

    After that, you can just add the pixels:

    img1Array[mask] += img2Array[mask]
    

    A further point of clarification:

    imgResult=img1Array+img2Array[mask]
    

    That could never work. You are saying ‘add some of the pixels from img2Array to all of the pixels in img1Array’ 6_9

    If you want to apply a ufunc between two or more arrays, they must be either the same shape, or broadcastable to the same shape.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two doubts: 1. I would like to add some client side support
I would like to add rows to DataGridView from two seperate threads. I tried
I would like to add two lines after the first string search. I am
I would like to add two vectors with different lengths in Matlab or Octave.
I've got a problem I would like to add two overlays on both side
I'm trying to add two images together using NumPy and PIL. The way I
I would like to add two compiler flags, one for ARC (-fno-objc-arc) and one
I would like to add two additional xml namespaces to the <HTML> element in
I have two console applications to which I would like to add GUIs. The
I would like to add some data validation on a cell in the latest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.