Hello!
I am very new with Python, so it may be a stupid question. How can I get the height and the width of an image in pixels?
Cheers!

Dani

One of the very useful modules for imaging is PIL ...

``````# explore the Python Image Library (PIL)
# http://www.pythonware.com/products/pil/

from PIL import Image

# pick an image file you have in the working directory
# (or give full path name)
image_file = "french_flag.bmp"
img = Image.open(image_file)
# get the image's width and height in pixels
width, height = img.size``````

Thank you so much! What I would like to do next is to resize an image giving only the dimension of the largest side. I know how to do it giving the two dimensions, but can I give only the largest side dimension?

Cheers!

Dani

You can if you calculate the factor of the resize, and use it to calculate the other dimension.

I think thats the only solution.

Yeah, I managed to do it like this.
Cheers!

Dani

Something as simple as this might do ...

``````# explore resizing with the Python Image Library (PIL)

from PIL import Image

# pick an image file you have in the working directory
# (or give full path name)
image_file = "french_flag.bmp"
img = Image.open(image_file)
# get the image's width and height in pixels
width, height = img.size
# get the largest dimension
max_dim = max(img.size)

# resize the image using the largest side as dimension
factor = 0.7
side = int(max_dim*factor)
resized_image = img.resize((side, side), Image.ANTIALIAS)

# save the resized image to a file
# and view it with your favorite image viewer
resized_image_file = "aa_image.jpg"
resized_image.save(resized_image_file)

# inform the user
print("%s saved!" % resized_image_file)``````

That won't keep proportions.

Keep aspect ratio of the image.

``````from PIL import Image

def resize(img,percent):
''' Resize image input image and percent | Keep aspect ratio'''
w,h = img.size
return img.resize(((percent*w)/100,(percent*h)/100))

if __name__ == '__main__':
img = Image.open('s.jpg')
#Give argumet to function image and percent
resized_image = resize(img, 80)
resized_image_file = 's_new.jpg'
resized_image.save(resized_image_file)
print("%s saved!" % resized_image_file)``````

That won't keep proportions.

Hard to figure out if the OP wanted to maintain proportions from the question. You are a better mind reader!

Come on guys you are arsh with each others.

I said that as info vegaseat, nothing more.

I said that because he had made like that and succeded, so he already have done mantining proportions, I said that so it was easier for him to see that this is without.

I don't understand, why the people here is so difficult with each others, I really like this forum, but i think that the people should keep their minds on sharing knowledge and trying to help.

And I'm not arguing against you vegaseat, I hope you understand me.

I'm just a coder wanting to share some knowledge, and to learn a lot with the great coders that go on this forums.

Happy coding!!!

EDIT: And sorry it this is off-topic or something like that.

Beat_Slayer take it easy, you are simply harsh on yourself! On some of the questions asked here, you have to be a mind reader. I simply gave you credit for that. I didn't mean to offend you in any way!

I'm sorry if you take it personnaly vegaseat, I really didn't mean it.

I just see lost of my posts harshly commented, and decided to let it out, only that.

Congrats

I'm sorry if you take it personnaly vegaseat, I really didn't mean it.

I just see lost of my posts harshly commented, and decided to let it out, only that.

Congrats

I know I posted 'against' your MasterMind solution but that was because it was buggy. If your solution works according to specks I will not critisize but give maybe my alternative way, if I see somebody could like it.
Don't take offense from me, bear with my terrible grammar, learn to be appreciative of bugs and debugging as best way to learn and as thex say in Asia: Thank xou for your hard work! Cheers, man!

Thanks for the words tonyjv.

And btw, I have no solution posted on mastermind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.