TypeError: unsupported operand type(s) for /: 'Image' and 'int'
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I wanted to convert the PIL Image object into a numpy array. I tried using the following codes it showing an error
TypeError Traceback (most recent call last) <ipython-input-133-0898103f22f0> in <module>()
1 image_path = 'test/28/image_05230.jpg'
----> 2 image = process_image(image_path)
3 imshow(image)
<ipython-input-129-e036faebfd31> in process_image(image_path)
24 # normalize
25 print(type(image))
---> 26 image_arr = np.array(image) / 255
27 mean = np.array([0.485, 0.456, 0.406])
28 std_dv = np.array( [0.229, 0.224, 0.225])
TypeError: unsupported operand type(s) for /: 'Image' and 'int'
from PIL import Image
image = Image.open(image_path)
image = np.asarray(image) / 255
I also tried with this code image = np.array(image) / 255 it's showing the same error. (code below)
from PIL import Image
image = Image.open(image_path)
image = np.array(image) / 255
This error occurs only when I used the above code in below function
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
if width > height:
image.thumbnail((500, 256))
else:
image.thumbnail((256, 500))
left_margin = (image.width - 224) / 2
lower_margin = (image.height - 224) / 2
upper_margin = lower_margin + 224
right_margin = left_margin + 224
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
print(type(image))
image_arr = np.array(image) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
return image_arr
python python-3.x numpy python-imaging-library
|
show 13 more comments
I wanted to convert the PIL Image object into a numpy array. I tried using the following codes it showing an error
TypeError Traceback (most recent call last) <ipython-input-133-0898103f22f0> in <module>()
1 image_path = 'test/28/image_05230.jpg'
----> 2 image = process_image(image_path)
3 imshow(image)
<ipython-input-129-e036faebfd31> in process_image(image_path)
24 # normalize
25 print(type(image))
---> 26 image_arr = np.array(image) / 255
27 mean = np.array([0.485, 0.456, 0.406])
28 std_dv = np.array( [0.229, 0.224, 0.225])
TypeError: unsupported operand type(s) for /: 'Image' and 'int'
from PIL import Image
image = Image.open(image_path)
image = np.asarray(image) / 255
I also tried with this code image = np.array(image) / 255 it's showing the same error. (code below)
from PIL import Image
image = Image.open(image_path)
image = np.array(image) / 255
This error occurs only when I used the above code in below function
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
if width > height:
image.thumbnail((500, 256))
else:
image.thumbnail((256, 500))
left_margin = (image.width - 224) / 2
lower_margin = (image.height - 224) / 2
upper_margin = lower_margin + 224
right_margin = left_margin + 224
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
print(type(image))
image_arr = np.array(image) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
return image_arr
python python-3.x numpy python-imaging-library
@PatrickArtner Actually I wanted to convert the Image type into numpy array and then divide whole elements in that array with 255
– Kavin Raju S
Jan 2 at 18:44
stackoverflow.com/questions/384759/…
– Tom Ron
Jan 2 at 18:45
Possible duplicate of How to convert a PIL Image into a numpy array?
– Tom Ron
Jan 2 at 18:46
Nope! It's not the same.... I have tried not working @TomRon
– Kavin Raju S
Jan 2 at 18:49
It works, I tested with a test image..can you show the Traceback error?
– amanb
Jan 2 at 18:54
|
show 13 more comments
I wanted to convert the PIL Image object into a numpy array. I tried using the following codes it showing an error
TypeError Traceback (most recent call last) <ipython-input-133-0898103f22f0> in <module>()
1 image_path = 'test/28/image_05230.jpg'
----> 2 image = process_image(image_path)
3 imshow(image)
<ipython-input-129-e036faebfd31> in process_image(image_path)
24 # normalize
25 print(type(image))
---> 26 image_arr = np.array(image) / 255
27 mean = np.array([0.485, 0.456, 0.406])
28 std_dv = np.array( [0.229, 0.224, 0.225])
TypeError: unsupported operand type(s) for /: 'Image' and 'int'
from PIL import Image
image = Image.open(image_path)
image = np.asarray(image) / 255
I also tried with this code image = np.array(image) / 255 it's showing the same error. (code below)
from PIL import Image
image = Image.open(image_path)
image = np.array(image) / 255
This error occurs only when I used the above code in below function
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
if width > height:
image.thumbnail((500, 256))
else:
image.thumbnail((256, 500))
left_margin = (image.width - 224) / 2
lower_margin = (image.height - 224) / 2
upper_margin = lower_margin + 224
right_margin = left_margin + 224
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
print(type(image))
image_arr = np.array(image) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
return image_arr
python python-3.x numpy python-imaging-library
I wanted to convert the PIL Image object into a numpy array. I tried using the following codes it showing an error
TypeError Traceback (most recent call last) <ipython-input-133-0898103f22f0> in <module>()
1 image_path = 'test/28/image_05230.jpg'
----> 2 image = process_image(image_path)
3 imshow(image)
<ipython-input-129-e036faebfd31> in process_image(image_path)
24 # normalize
25 print(type(image))
---> 26 image_arr = np.array(image) / 255
27 mean = np.array([0.485, 0.456, 0.406])
28 std_dv = np.array( [0.229, 0.224, 0.225])
TypeError: unsupported operand type(s) for /: 'Image' and 'int'
from PIL import Image
image = Image.open(image_path)
image = np.asarray(image) / 255
I also tried with this code image = np.array(image) / 255 it's showing the same error. (code below)
from PIL import Image
image = Image.open(image_path)
image = np.array(image) / 255
This error occurs only when I used the above code in below function
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
if width > height:
image.thumbnail((500, 256))
else:
image.thumbnail((256, 500))
left_margin = (image.width - 224) / 2
lower_margin = (image.height - 224) / 2
upper_margin = lower_margin + 224
right_margin = left_margin + 224
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
print(type(image))
image_arr = np.array(image) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
return image_arr
python python-3.x numpy python-imaging-library
python python-3.x numpy python-imaging-library
edited Jan 3 at 4:05
Kavin Raju S
asked Jan 2 at 18:37
Kavin Raju SKavin Raju S
857
857
@PatrickArtner Actually I wanted to convert the Image type into numpy array and then divide whole elements in that array with 255
– Kavin Raju S
Jan 2 at 18:44
stackoverflow.com/questions/384759/…
– Tom Ron
Jan 2 at 18:45
Possible duplicate of How to convert a PIL Image into a numpy array?
– Tom Ron
Jan 2 at 18:46
Nope! It's not the same.... I have tried not working @TomRon
– Kavin Raju S
Jan 2 at 18:49
It works, I tested with a test image..can you show the Traceback error?
– amanb
Jan 2 at 18:54
|
show 13 more comments
@PatrickArtner Actually I wanted to convert the Image type into numpy array and then divide whole elements in that array with 255
– Kavin Raju S
Jan 2 at 18:44
stackoverflow.com/questions/384759/…
– Tom Ron
Jan 2 at 18:45
Possible duplicate of How to convert a PIL Image into a numpy array?
– Tom Ron
Jan 2 at 18:46
Nope! It's not the same.... I have tried not working @TomRon
– Kavin Raju S
Jan 2 at 18:49
It works, I tested with a test image..can you show the Traceback error?
– amanb
Jan 2 at 18:54
@PatrickArtner Actually I wanted to convert the Image type into numpy array and then divide whole elements in that array with 255
– Kavin Raju S
Jan 2 at 18:44
@PatrickArtner Actually I wanted to convert the Image type into numpy array and then divide whole elements in that array with 255
– Kavin Raju S
Jan 2 at 18:44
stackoverflow.com/questions/384759/…
– Tom Ron
Jan 2 at 18:45
stackoverflow.com/questions/384759/…
– Tom Ron
Jan 2 at 18:45
Possible duplicate of How to convert a PIL Image into a numpy array?
– Tom Ron
Jan 2 at 18:46
Possible duplicate of How to convert a PIL Image into a numpy array?
– Tom Ron
Jan 2 at 18:46
Nope! It's not the same.... I have tried not working @TomRon
– Kavin Raju S
Jan 2 at 18:49
Nope! It's not the same.... I have tried not working @TomRon
– Kavin Raju S
Jan 2 at 18:49
It works, I tested with a test image..can you show the Traceback error?
– amanb
Jan 2 at 18:54
It works, I tested with a test image..can you show the Traceback error?
– amanb
Jan 2 at 18:54
|
show 13 more comments
3 Answers
3
active
oldest
votes
Now that you presented the real code you are actually using:
Image.open("path.jpg")
returns<class 'PIL.JpegImagePlugin.JpegImageFile'>
- after your cropping you get a return of
<class 'PIL.Image.Image'>
If you inspect your cropped image
, you can see it only has one dimension, the second is 0:
If you fix your code to:
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
image.thumbnail((500, 256) if (width > height) else (256, 500))
left_margin = (image.width - 224) / 2
upper_margin = (image.height - 224) / 2 # fixed
lower_margin = upper_margin + 224 # fixed
right_margin = left_margin + 224
# fixed and renamed so you do not overwrite image all the time - helps debugging
# now this has 2 dimensions that are non-zero
image_crop = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
image_arr = np.asarray(image) / 255
mean = np.mean(image_arr)
std_dv = np.std( image_arr )
image_arr = (image_arr - mean)/std_dv
return image_crop
the code suddenly runs without errors.
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
add a comment |
In the function convert_pil_to_numpy_array()
, the image
variable used initially is different from the image
variable that stores the crop
ped Image
object.
from PIL import Image
image_path = "C:\temp\Capture.JPG"
image = Image.open(image_path)
print(type(image))
#Output
<class 'PIL.JpegImagePlugin.JpegImageFile'>
This is a JpegImageFile
object. If you look at the other image
variable that stores the cropped image and is later passed to np.array
, this variable is an object of the Image
class:
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
print(type(image))
#Output:
<class 'PIL.Image.Image'>
The problem lies in the tuple values passed to the crop()
function. With the margin values that you passed to crop
, the image could not be converted to an array and returned an Image
object again:
image_arr = np.array(image)
print(image_arr)
#Output:
<PIL.Image.Image image mode=RGB size=224x0 at 0x39E4F60>
As your image dimensions were different from mine, I used different values for the 4-tuple passed to crop()
and got an array:
image = image.crop((50,100,60,120))
image_arr = np.array(image)
#Output:
[[[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]]..etc
What you should do is, check the margin values and save the cropped image to file(jpg, png, etc.) and then convert to array. Note that I am not storing the saved image to any variable. :
image.crop((50, 60, 100, 120)).save("test.jpg")
image_arr = np.array(Image.open("test.jpg")) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
print(image_arr)
#Output:
[[[-0.04580872 0.08263305 0.30448802]
[-0.91917116 -0.81022409 -0.58440087]
[ 0.81042898 0.95798319 1.17594771]
...
[ 2.19753404 2.37605042 2.58771242]
[-0.02868396 -0.19747899 0.13019608]
[-0.11430773 -0.28501401 0.04305011]]
....etc.
I don't see how your code is any better.np.array(image)
should create an array just like your line. Only after is run does Python apply the/255
step.
– hpaulj
Jan 2 at 19:56
I agree, I just tested OP's code and the/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.
– amanb
Jan 2 at 20:16
add a comment |
This works:
from PIL import Image
import numpy as np
image = Image.open(r'C:temp2015-05-14 17.43.10.jpg') # path to existing local file
image_arr = np.asarray(image) / 255
print(image_arr)
Output:
[[[ 0.35294118 0.39607843 0.41960784]
[ 0.38039216 0.42352941 0.44705882]
[ 0.41568627 0.45098039 0.47058824]
...,
[ 0.05490196 0.04705882 0.05098039]
[ 0.04705882 0.03921569 0.04313725]
[ 0.04313725 0.03529412 0.03921569]]
[[ 0.36470588 0.4 0.42745098]
[ 0.38823529 0.42352941 0.44313725]
[ 0.40784314 0.44313725 0.4627451 ]
..., etc ]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011487%2ftypeerror-unsupported-operand-types-for-image-and-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Now that you presented the real code you are actually using:
Image.open("path.jpg")
returns<class 'PIL.JpegImagePlugin.JpegImageFile'>
- after your cropping you get a return of
<class 'PIL.Image.Image'>
If you inspect your cropped image
, you can see it only has one dimension, the second is 0:
If you fix your code to:
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
image.thumbnail((500, 256) if (width > height) else (256, 500))
left_margin = (image.width - 224) / 2
upper_margin = (image.height - 224) / 2 # fixed
lower_margin = upper_margin + 224 # fixed
right_margin = left_margin + 224
# fixed and renamed so you do not overwrite image all the time - helps debugging
# now this has 2 dimensions that are non-zero
image_crop = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
image_arr = np.asarray(image) / 255
mean = np.mean(image_arr)
std_dv = np.std( image_arr )
image_arr = (image_arr - mean)/std_dv
return image_crop
the code suddenly runs without errors.
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
add a comment |
Now that you presented the real code you are actually using:
Image.open("path.jpg")
returns<class 'PIL.JpegImagePlugin.JpegImageFile'>
- after your cropping you get a return of
<class 'PIL.Image.Image'>
If you inspect your cropped image
, you can see it only has one dimension, the second is 0:
If you fix your code to:
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
image.thumbnail((500, 256) if (width > height) else (256, 500))
left_margin = (image.width - 224) / 2
upper_margin = (image.height - 224) / 2 # fixed
lower_margin = upper_margin + 224 # fixed
right_margin = left_margin + 224
# fixed and renamed so you do not overwrite image all the time - helps debugging
# now this has 2 dimensions that are non-zero
image_crop = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
image_arr = np.asarray(image) / 255
mean = np.mean(image_arr)
std_dv = np.std( image_arr )
image_arr = (image_arr - mean)/std_dv
return image_crop
the code suddenly runs without errors.
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
add a comment |
Now that you presented the real code you are actually using:
Image.open("path.jpg")
returns<class 'PIL.JpegImagePlugin.JpegImageFile'>
- after your cropping you get a return of
<class 'PIL.Image.Image'>
If you inspect your cropped image
, you can see it only has one dimension, the second is 0:
If you fix your code to:
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
image.thumbnail((500, 256) if (width > height) else (256, 500))
left_margin = (image.width - 224) / 2
upper_margin = (image.height - 224) / 2 # fixed
lower_margin = upper_margin + 224 # fixed
right_margin = left_margin + 224
# fixed and renamed so you do not overwrite image all the time - helps debugging
# now this has 2 dimensions that are non-zero
image_crop = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
image_arr = np.asarray(image) / 255
mean = np.mean(image_arr)
std_dv = np.std( image_arr )
image_arr = (image_arr - mean)/std_dv
return image_crop
the code suddenly runs without errors.
Now that you presented the real code you are actually using:
Image.open("path.jpg")
returns<class 'PIL.JpegImagePlugin.JpegImageFile'>
- after your cropping you get a return of
<class 'PIL.Image.Image'>
If you inspect your cropped image
, you can see it only has one dimension, the second is 0:
If you fix your code to:
def convert_pil_to_numpy_array(image_path):
# Load Image an open the image
from PIL import Image
image = Image.open(image_path)
width = image.size[0]
height = image.size[1]
image.thumbnail((500, 256) if (width > height) else (256, 500))
left_margin = (image.width - 224) / 2
upper_margin = (image.height - 224) / 2 # fixed
lower_margin = upper_margin + 224 # fixed
right_margin = left_margin + 224
# fixed and renamed so you do not overwrite image all the time - helps debugging
# now this has 2 dimensions that are non-zero
image_crop = image.crop((left_margin, upper_margin, right_margin, lower_margin))
# normalize
image_arr = np.asarray(image) / 255
mean = np.mean(image_arr)
std_dv = np.std( image_arr )
image_arr = (image_arr - mean)/std_dv
return image_crop
the code suddenly runs without errors.
answered Jan 3 at 9:07
Patrick ArtnerPatrick Artner
26.5k62544
26.5k62544
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
add a comment |
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
Thank you for helping @patrich artner.... Actually, I had found this error and corrected the code...
– Kavin Raju S
Jan 3 at 9:16
add a comment |
In the function convert_pil_to_numpy_array()
, the image
variable used initially is different from the image
variable that stores the crop
ped Image
object.
from PIL import Image
image_path = "C:\temp\Capture.JPG"
image = Image.open(image_path)
print(type(image))
#Output
<class 'PIL.JpegImagePlugin.JpegImageFile'>
This is a JpegImageFile
object. If you look at the other image
variable that stores the cropped image and is later passed to np.array
, this variable is an object of the Image
class:
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
print(type(image))
#Output:
<class 'PIL.Image.Image'>
The problem lies in the tuple values passed to the crop()
function. With the margin values that you passed to crop
, the image could not be converted to an array and returned an Image
object again:
image_arr = np.array(image)
print(image_arr)
#Output:
<PIL.Image.Image image mode=RGB size=224x0 at 0x39E4F60>
As your image dimensions were different from mine, I used different values for the 4-tuple passed to crop()
and got an array:
image = image.crop((50,100,60,120))
image_arr = np.array(image)
#Output:
[[[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]]..etc
What you should do is, check the margin values and save the cropped image to file(jpg, png, etc.) and then convert to array. Note that I am not storing the saved image to any variable. :
image.crop((50, 60, 100, 120)).save("test.jpg")
image_arr = np.array(Image.open("test.jpg")) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
print(image_arr)
#Output:
[[[-0.04580872 0.08263305 0.30448802]
[-0.91917116 -0.81022409 -0.58440087]
[ 0.81042898 0.95798319 1.17594771]
...
[ 2.19753404 2.37605042 2.58771242]
[-0.02868396 -0.19747899 0.13019608]
[-0.11430773 -0.28501401 0.04305011]]
....etc.
I don't see how your code is any better.np.array(image)
should create an array just like your line. Only after is run does Python apply the/255
step.
– hpaulj
Jan 2 at 19:56
I agree, I just tested OP's code and the/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.
– amanb
Jan 2 at 20:16
add a comment |
In the function convert_pil_to_numpy_array()
, the image
variable used initially is different from the image
variable that stores the crop
ped Image
object.
from PIL import Image
image_path = "C:\temp\Capture.JPG"
image = Image.open(image_path)
print(type(image))
#Output
<class 'PIL.JpegImagePlugin.JpegImageFile'>
This is a JpegImageFile
object. If you look at the other image
variable that stores the cropped image and is later passed to np.array
, this variable is an object of the Image
class:
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
print(type(image))
#Output:
<class 'PIL.Image.Image'>
The problem lies in the tuple values passed to the crop()
function. With the margin values that you passed to crop
, the image could not be converted to an array and returned an Image
object again:
image_arr = np.array(image)
print(image_arr)
#Output:
<PIL.Image.Image image mode=RGB size=224x0 at 0x39E4F60>
As your image dimensions were different from mine, I used different values for the 4-tuple passed to crop()
and got an array:
image = image.crop((50,100,60,120))
image_arr = np.array(image)
#Output:
[[[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]]..etc
What you should do is, check the margin values and save the cropped image to file(jpg, png, etc.) and then convert to array. Note that I am not storing the saved image to any variable. :
image.crop((50, 60, 100, 120)).save("test.jpg")
image_arr = np.array(Image.open("test.jpg")) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
print(image_arr)
#Output:
[[[-0.04580872 0.08263305 0.30448802]
[-0.91917116 -0.81022409 -0.58440087]
[ 0.81042898 0.95798319 1.17594771]
...
[ 2.19753404 2.37605042 2.58771242]
[-0.02868396 -0.19747899 0.13019608]
[-0.11430773 -0.28501401 0.04305011]]
....etc.
I don't see how your code is any better.np.array(image)
should create an array just like your line. Only after is run does Python apply the/255
step.
– hpaulj
Jan 2 at 19:56
I agree, I just tested OP's code and the/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.
– amanb
Jan 2 at 20:16
add a comment |
In the function convert_pil_to_numpy_array()
, the image
variable used initially is different from the image
variable that stores the crop
ped Image
object.
from PIL import Image
image_path = "C:\temp\Capture.JPG"
image = Image.open(image_path)
print(type(image))
#Output
<class 'PIL.JpegImagePlugin.JpegImageFile'>
This is a JpegImageFile
object. If you look at the other image
variable that stores the cropped image and is later passed to np.array
, this variable is an object of the Image
class:
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
print(type(image))
#Output:
<class 'PIL.Image.Image'>
The problem lies in the tuple values passed to the crop()
function. With the margin values that you passed to crop
, the image could not be converted to an array and returned an Image
object again:
image_arr = np.array(image)
print(image_arr)
#Output:
<PIL.Image.Image image mode=RGB size=224x0 at 0x39E4F60>
As your image dimensions were different from mine, I used different values for the 4-tuple passed to crop()
and got an array:
image = image.crop((50,100,60,120))
image_arr = np.array(image)
#Output:
[[[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]]..etc
What you should do is, check the margin values and save the cropped image to file(jpg, png, etc.) and then convert to array. Note that I am not storing the saved image to any variable. :
image.crop((50, 60, 100, 120)).save("test.jpg")
image_arr = np.array(Image.open("test.jpg")) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
print(image_arr)
#Output:
[[[-0.04580872 0.08263305 0.30448802]
[-0.91917116 -0.81022409 -0.58440087]
[ 0.81042898 0.95798319 1.17594771]
...
[ 2.19753404 2.37605042 2.58771242]
[-0.02868396 -0.19747899 0.13019608]
[-0.11430773 -0.28501401 0.04305011]]
....etc.
In the function convert_pil_to_numpy_array()
, the image
variable used initially is different from the image
variable that stores the crop
ped Image
object.
from PIL import Image
image_path = "C:\temp\Capture.JPG"
image = Image.open(image_path)
print(type(image))
#Output
<class 'PIL.JpegImagePlugin.JpegImageFile'>
This is a JpegImageFile
object. If you look at the other image
variable that stores the cropped image and is later passed to np.array
, this variable is an object of the Image
class:
image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
print(type(image))
#Output:
<class 'PIL.Image.Image'>
The problem lies in the tuple values passed to the crop()
function. With the margin values that you passed to crop
, the image could not be converted to an array and returned an Image
object again:
image_arr = np.array(image)
print(image_arr)
#Output:
<PIL.Image.Image image mode=RGB size=224x0 at 0x39E4F60>
As your image dimensions were different from mine, I used different values for the 4-tuple passed to crop()
and got an array:
image = image.crop((50,100,60,120))
image_arr = np.array(image)
#Output:
[[[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]
[-2.11790393 -2.03571429 -1.80444444]]..etc
What you should do is, check the margin values and save the cropped image to file(jpg, png, etc.) and then convert to array. Note that I am not storing the saved image to any variable. :
image.crop((50, 60, 100, 120)).save("test.jpg")
image_arr = np.array(Image.open("test.jpg")) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
print(image_arr)
#Output:
[[[-0.04580872 0.08263305 0.30448802]
[-0.91917116 -0.81022409 -0.58440087]
[ 0.81042898 0.95798319 1.17594771]
...
[ 2.19753404 2.37605042 2.58771242]
[-0.02868396 -0.19747899 0.13019608]
[-0.11430773 -0.28501401 0.04305011]]
....etc.
edited Jan 3 at 8:15
answered Jan 2 at 18:59
amanbamanb
1,8642823
1,8642823
I don't see how your code is any better.np.array(image)
should create an array just like your line. Only after is run does Python apply the/255
step.
– hpaulj
Jan 2 at 19:56
I agree, I just tested OP's code and the/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.
– amanb
Jan 2 at 20:16
add a comment |
I don't see how your code is any better.np.array(image)
should create an array just like your line. Only after is run does Python apply the/255
step.
– hpaulj
Jan 2 at 19:56
I agree, I just tested OP's code and the/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.
– amanb
Jan 2 at 20:16
I don't see how your code is any better.
np.array(image)
should create an array just like your line. Only after is run does Python apply the /255
step.– hpaulj
Jan 2 at 19:56
I don't see how your code is any better.
np.array(image)
should create an array just like your line. Only after is run does Python apply the /255
step.– hpaulj
Jan 2 at 19:56
I agree, I just tested OP's code and the
/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.– amanb
Jan 2 at 20:16
I agree, I just tested OP's code and the
/255
step applies after. But the exact same piece of code works for me. This answer suggests to check if the array is created in the first place and if so, save it in a variable.– amanb
Jan 2 at 20:16
add a comment |
This works:
from PIL import Image
import numpy as np
image = Image.open(r'C:temp2015-05-14 17.43.10.jpg') # path to existing local file
image_arr = np.asarray(image) / 255
print(image_arr)
Output:
[[[ 0.35294118 0.39607843 0.41960784]
[ 0.38039216 0.42352941 0.44705882]
[ 0.41568627 0.45098039 0.47058824]
...,
[ 0.05490196 0.04705882 0.05098039]
[ 0.04705882 0.03921569 0.04313725]
[ 0.04313725 0.03529412 0.03921569]]
[[ 0.36470588 0.4 0.42745098]
[ 0.38823529 0.42352941 0.44313725]
[ 0.40784314 0.44313725 0.4627451 ]
..., etc ]
add a comment |
This works:
from PIL import Image
import numpy as np
image = Image.open(r'C:temp2015-05-14 17.43.10.jpg') # path to existing local file
image_arr = np.asarray(image) / 255
print(image_arr)
Output:
[[[ 0.35294118 0.39607843 0.41960784]
[ 0.38039216 0.42352941 0.44705882]
[ 0.41568627 0.45098039 0.47058824]
...,
[ 0.05490196 0.04705882 0.05098039]
[ 0.04705882 0.03921569 0.04313725]
[ 0.04313725 0.03529412 0.03921569]]
[[ 0.36470588 0.4 0.42745098]
[ 0.38823529 0.42352941 0.44313725]
[ 0.40784314 0.44313725 0.4627451 ]
..., etc ]
add a comment |
This works:
from PIL import Image
import numpy as np
image = Image.open(r'C:temp2015-05-14 17.43.10.jpg') # path to existing local file
image_arr = np.asarray(image) / 255
print(image_arr)
Output:
[[[ 0.35294118 0.39607843 0.41960784]
[ 0.38039216 0.42352941 0.44705882]
[ 0.41568627 0.45098039 0.47058824]
...,
[ 0.05490196 0.04705882 0.05098039]
[ 0.04705882 0.03921569 0.04313725]
[ 0.04313725 0.03529412 0.03921569]]
[[ 0.36470588 0.4 0.42745098]
[ 0.38823529 0.42352941 0.44313725]
[ 0.40784314 0.44313725 0.4627451 ]
..., etc ]
This works:
from PIL import Image
import numpy as np
image = Image.open(r'C:temp2015-05-14 17.43.10.jpg') # path to existing local file
image_arr = np.asarray(image) / 255
print(image_arr)
Output:
[[[ 0.35294118 0.39607843 0.41960784]
[ 0.38039216 0.42352941 0.44705882]
[ 0.41568627 0.45098039 0.47058824]
...,
[ 0.05490196 0.04705882 0.05098039]
[ 0.04705882 0.03921569 0.04313725]
[ 0.04313725 0.03529412 0.03921569]]
[[ 0.36470588 0.4 0.42745098]
[ 0.38823529 0.42352941 0.44313725]
[ 0.40784314 0.44313725 0.4627451 ]
..., etc ]
answered Jan 2 at 18:59
Patrick ArtnerPatrick Artner
26.5k62544
26.5k62544
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011487%2ftypeerror-unsupported-operand-types-for-image-and-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@PatrickArtner Actually I wanted to convert the Image type into numpy array and then divide whole elements in that array with 255
– Kavin Raju S
Jan 2 at 18:44
stackoverflow.com/questions/384759/…
– Tom Ron
Jan 2 at 18:45
Possible duplicate of How to convert a PIL Image into a numpy array?
– Tom Ron
Jan 2 at 18:46
Nope! It's not the same.... I have tried not working @TomRon
– Kavin Raju S
Jan 2 at 18:49
It works, I tested with a test image..can you show the Traceback error?
– amanb
Jan 2 at 18:54