Python Forum
problem adding two numpy arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem adding two numpy arrays
#1
I am trying to do the following. I want to add the Mona Lisa .jpg image at this link to a slightly larger numpy array of all zeroes (called base_image1) of size (605,405,4). Alpha channel values was first added to the Mona Lisa image and converted to a numpy array (called image1) of size (599,402,4).

Next, I add the smaller Mona Lisa image to a location within the larger numpy array of all zeroes. Finally I display the resulting image.

What I was expecting to get displayed was an image of the Mona Lisa with a little bit of white border in certain regions. Instead it simply displays a (605,405,4) image that looks completely white everywhere! Why is this happening? I can't understand why this is occuring Wall . Below is my code:
import numpy as np
from PIL import Image

#Load Mona Lisa image and Add fully opaque alpha channel values
path='C:\\Users\\john\\Desktop\\monalisa.jpg'
image = Image.open(path)
image.putalpha(255)

#Make larger numpy array of all zeroes
base_image1=np.zeros((605, 405, 4),dtype=int)

#Convert Mona Lisa image to numpy array and add Mona Lisa array to region within larger array
width, height = image.size
image1=np.array(image)
x=0
y=0
base_image1[x:(x+height),y:(y+width),:]=base_image1[x:(x+height),y:(y+width),:]+image1

#Display image
img = Image.fromarray(base_image1, 'RGBA')
img.show()
Reply
#2
import numpy as np
Reply
#3
The problem is not adding the arrays, the problem is that one of the arrays has the wrong dtype. The dtype for base_image1 should be np.int8. You could have determined this by looking at the dtype for image1, or just use dtype from image1.
image = Image.open(path)
image.putalpha(255)
image1 = np.array(image)

# Make larger numpy array of all zeroes
base_image1 = np.zeros((605, 405, 4), dtype=image1.dtype)
You can copy image1 into base_image1 without the extra addition step.
base_image1[x:(x+height),y:(y+width),:] = image1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplot / numpy noisy data problem the57chambers 1 693 Feb-09-2023, 03:27 AM
Last Post: deanhystad
  Django: Adding Row Data To Existing Model Instance Question/Problem. Steven_Pinkerton 1 1,244 Aug-09-2022, 10:46 AM
Last Post: Addweb
  how to join by stack multiple types in numpy arrays caro 1 1,140 Jun-20-2022, 05:02 PM
Last Post: deanhystad
  SOlving LInear Equations in Python(Symoy, NUmpy) - Coefficient Problem quest 3 1,730 Jan-30-2022, 10:53 PM
Last Post: quest
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,888 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  Two numpy arrays Sandra2312 1 1,802 Jan-18-2021, 06:10 PM
Last Post: paul18fr
  numpy in1d with two simple arrays claw91 3 2,582 Sep-21-2020, 12:43 PM
Last Post: scidam
  Type coercion with Numpy arrays Mark17 2 2,521 Jul-24-2020, 02:04 AM
Last Post: scidam
  filling and printing numpy arrays of str pjfarley3 4 3,292 Jun-07-2020, 09:09 PM
Last Post: pjfarley3
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 1,967 Feb-03-2020, 09:30 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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