1.11M Members

Get image size in pixels

 
0
 

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

 
0
 

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

# explore the Python Image Library (PIL)
# download PIL from:
# 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
 
-1
 

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

 
0
 

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

I think thats the only solution.

 
0
 

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

Dani

 
0
 

Something as simple as this might do ...

# explore resizing with the Python Image Library (PIL)
# download PIL from: 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
# 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)
 
0
 

That won't keep proportions.

 
0
 

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)
 
0
 

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!

 
1
 

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.

 
0
 

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!

 
0
 

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

 
0
 

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!

 
0
 

Thanks for the words tonyjv.

And btw, I have no solution posted on mastermind.

Question Answered as of 4 Years Ago by Beat_Slayer, vegaseat, snippsat and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article