1,105,288 Community Members

Get image size in pixels

Member Avatar
acrocephalus
Junior Poster
110 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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
Member Avatar
acrocephalus
Junior Poster
110 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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

Member Avatar
Beat_Slayer
Posting Pro in Training
401 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
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.

Member Avatar
acrocephalus
Junior Poster
110 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Dani

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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)
Member Avatar
Beat_Slayer
Posting Pro in Training
401 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
0
 

That won't keep proportions.

Member Avatar
snippsat
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 10 [?]
 
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)
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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!

Member Avatar
Beat_Slayer
Posting Pro in Training
401 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
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.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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!

Member Avatar
Beat_Slayer
Posting Pro in Training
401 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
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

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
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!

Member Avatar
Beat_Slayer
Posting Pro in Training
401 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
0
 

Thanks for the words tonyjv.

And btw, I have no solution posted on mastermind.

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