0

I try to get familiar with a bit of image processing. I did an exercise online and I have a challenge with one of the functions, I cannot debug properly.

import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
import imageio

# Masking Images
photoData = imageio.imread("../images/sd-3layers.jpg")
totalRows, totalCols, totalLayers = photoData.shape
X, Y = np.ogrid(:totalRows, :totalCols)
centerRow, centerCol = totalRows / 2, totalCols / 2
distanceFromCenter = (X - centerRow)**2 + (Y - centerCol)**2
radius = (totalRows / 2)**2
circularMask = (distanceFromCenter > radius)
print(circularMask[1500:1700,2000:2000])

I get the following error:

 File "<ipython-input-28-6cc7fea28dce>", line 4
    X, Y = np.ogrid(:totalRows, :totalCols)
                    ^
SyntaxError: invalid syntax

I do not know why this is actually happening? The data input seems correct to me. What is wrong with the syntax. Can you help me please?

12
  • 1
    what is :totalRows? what should this do? Commented Jul 25, 2018 at 12:09
  • extracting the values of photoData.shape into totalRows, totalCols and totalLayers Commented Jul 25, 2018 at 12:10
  • 2
    What did you want to do when you did np.ogrid(:totalRows, :totalCols)? Maybe np.ogrid[:totalRows, :totalCols]? (Your line looks like Ruby to me lol) Commented Jul 25, 2018 at 12:10
  • I meant the next line, I didn't see a :list statement ever. Commented Jul 25, 2018 at 12:11
  • 1
    The reason for the error, as already said by both me in my first comment and davidlowryduda in his answer, is you have wrong parentheses. Commented Jul 25, 2018 at 12:16

1 Answer 1

3

It is very likely that you mean

np.ogrid[:totalRows, :totalCols]

This is a numpy shortcut for typical python slicing, such as

s = "abcde"
print(s[:3])
# prints "abc"

Use brackets, not parentheses.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.