[Tutor] Numpy unexpected result: subtraction of cell values

Kent Johnson kent37 at tds.net
Mon Jan 11 21:17:17 CET 2010


On Mon, Jan 11, 2010 at 2:02 PM, Carnell, James E
<jecarnell at saintfrancis.com> wrote:

>> I want to subtract the Red Value in an array cell from a neighboring
>> Red Value cell.
>>
>> >>> pictArray[39][4]                    #pixel at 39 4
>> array([150, 140, 120], dtype=unint8)
>>
>> >>> pictArray[39][5]                    #pixel at 39 5
>> array([160, 150, 120], dtype=unint8)
>>
>> >>> pictArray[39][4][0]                 #red pixel
>> 150
>>
>> >>> pictArray[39][5[0]                  #neighboring red pixel
>> 160
>>
>> >>> pictArray[39][4][0] - pictArray[39][5][0]
>> 246  # <---------------  ???  vs -10
>>
>> How do I get the number -10? Does this have to do with the dtype?

>         - pictArray[39][4][0] - pictArray[39][5][0] = 246  is where I am
> getting stuck

I guess the subtraction is happening on unsigned values, which can't
represent -10. Try
int(pictArray[39][4][0]) - int(pictArray[39][5][0])

Kent


More information about the Tutor mailing list