We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,337 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Get image size in pixels

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

5
Contributors
13
Replies
2 Days
Discussion Span
2 Years Ago
Last Updated
24
Views
Question
Answered
acrocephalus
Junior Poster
110 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
vegaseat
DaniWeb's Hypocrite
Moderator
6,476 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36

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

acrocephalus
Junior Poster
110 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
Skill Endorsements: 1

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

Dani

acrocephalus
Junior Poster
110 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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)
vegaseat
DaniWeb's Hypocrite
Moderator
6,476 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36

That won't keep proportions.

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
Skill Endorsements: 1

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)
snippsat
Posting Shark
957 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

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!

vegaseat
DaniWeb's Hypocrite
Moderator
6,476 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36

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
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
Skill Endorsements: 1

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!

vegaseat
DaniWeb's Hypocrite
Moderator
6,476 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36

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

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
Skill Endorsements: 1

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!

pyTony
pyMod
Moderator
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26

Thanks for the words tonyjv.

And btw, I have no solution posted on mastermind.

Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
Skill Endorsements: 1
Question Answered as of 2 Years Ago by Beat_Slayer, vegaseat, snippsat and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3265 seconds using 2.71MB