By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,123 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,123 IT Pros & Developers. It's quick & easy.

image saving in python.

P: 2
so here we go. i am currently making a script for myself. it is basically rotating and resizing a number of images. i am just a beginner and i chose python because i might need to make some more scripts, and it is not so hard to learn. so i am just starting the script, and am developing a script that will rotate and resize one image. here is what i have so far:
Expand|Select|Wrap|Line Numbers
  1. import Image
  2. im = Image.open("/home/bella/Desktop/LEMONKIWI.jpg")
  3. out = im.resize((612, 600))
  4. out = im.rotate(90) # degrees clockwise
  5. im.save(file + ".jpg", "JPEG")
  6.  
i know"
-it doesnt work
- the reason is the last line.

so basically i am asking, what to write for the last line tomake sure that the picture is saved. ?.

a HUGE thank to whoever helps me.
Sep 8 '07 #1
Share this Question
Share on Google+
6 Replies

bartonc
Expert 5K+
P: 6,596
so here we go. i am currently making a script for myself. it is basically rotating and resizing a number of images. i am just a beginner and i chose python because i might need to make some more scripts, and it is not so hard to learn. so i am just starting the script, and am developing a script that will rotate and resize one image. here is what i have so far:
Expand|Select|Wrap|Line Numbers
  1. import Image
  2. im = Image.open("/home/bella/Desktop/LEMONKIWI.jpg")
  3. out = im.resize((612, 600))
  4. out = im.rotate(90) # degrees clockwise
  5. im.save(file + ".jpg", "JPEG")
  6.  
i know"
-it doesnt work
- the reason is the last line.

so basically i am asking, what to write for the last line tomake sure that the picture is saved. ?.

a HUGE thank to whoever helps me.
Well, you didn't give us much to go on here. For example, I had to deduce from my system that you must be using the PIL extension module.

Your code looks good, except for two mistakes: "file" is not a string, it is a built-in function - use a new filename string there. Saving im instead of out.
I tested this:
Expand|Select|Wrap|Line Numbers
  1. >>> import Image
  2. >>> theDir = """C:\Documents and Settings\All Users\Documents\My Pictures\HETAP 2.0\Nevada\Inhouse\Bartly Ranch\practice\4 practice Bartly 2007-07-17"""
  3. >>> fName = "Feature@0Ft-00002.jpg"
  4. >>> im = Image.open(r'%s\%s' %(theDir, fName))
  5. >>> out = im.resize((612, 600))
  6. >>> out = im.rotate(90) # degrees clockwise
  7. >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
  8. >>> 
Rotation worked, but resizing didn't.
Sep 8 '07 #2

bartonc
Expert 5K+
P: 6,596
Well, you didn't give us much to go on here. For example, I had to deduce from my system that you must be using the PIL extension module.

Your code looks good, except for one mistake: saving im instead of out.
I tested this:
Expand|Select|Wrap|Line Numbers
  1. >>> import Image
  2. >>> theDir = """C:\Documents and Settings\All Users\Documents\My Pictures\HETAP 2.0\Nevada\Inhouse\Bartly Ranch\practice\4 practice Bartly 2007-07-17"""
  3. >>> fName = "Feature@0Ft-00002.jpg"
  4. >>> im = Image.open(r'%s\%s' %(theDir, fName))
  5. >>> out = im.resize((612, 600))
  6. >>> out = im.rotate(90) # degrees clockwise
  7. >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
  8. >>> 
Rotation worked, but resizing didn't.
Here's the fix for that last part:
Expand|Select|Wrap|Line Numbers
  1. >>> im = Image.open(r'%s\%s' %(theDir, fName))
  2. >>> out = im.resize((612, 600))
  3. >>> out = out.rotate(90) # degrees clockwise
  4. >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
  5. >>> 
Sep 8 '07 #3

P: 2
Here's the fix for that last part:
Expand|Select|Wrap|Line Numbers
  1. >>> im = Image.open(r'%s\%s' %(theDir, fName))
  2. >>> out = im.resize((612, 600))
  3. >>> out = out.rotate(90) # degrees clockwise
  4. >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
  5. >>> 

yes, the saving part worked. do you know anything about why the resizing didnt thougH?. thanks for the help, anyways.
Sep 9 '07 #4

ilikepython
Expert 100+
P: 844
yes, the saving part worked. do you know anything about why the resizing didnt thougH?. thanks for the help, anyways.
It didn't work because you were assigning to out the original image, when you already made changes to out (the resizing). You overrode the resizing with the rotating.
Sep 9 '07 #5

bartonc
Expert 5K+
P: 6,596
yes, the saving part worked. do you know anything about why the resizing didnt thougH?. thanks for the help, anyways.
Note how many time im is used on the right hand side of the equal sign in each example.
Sep 9 '07 #6

dshimer
Expert 100+
P: 136
I love PIL and use it on occasion. Let me throw out a couple of examples that don't so much speak directly to your question but illustrate some things I appreciated early on. In this case I have a file called test.tif in my tmp directory. I want to open it, rotate it 45 degrees, resize the result, and display it. In a real program things like resampling modes, and aspect ratios would be considered but this is just to make a point.

Expand|Select|Wrap|Line Numbers
  1. Image.open('/tmp/test.tif','r').rotate(45).resize([200,100]).show()
This is why I love this stuff.

Image.open('/tmp/test.tif','r'), creates an open image object, the "im" in most examples.

.rotate(45) , then takes that object and rotates it 45 degrees.

.resize([200,100]) , now the open, rotated object is resized.

Which can then be displayed in the default system viewer by just saying .show().

If I in fact wanted to save that modified image, all I need to do is replace .show() with .save() with the appropriate variables. like...
Expand|Select|Wrap|Line Numbers
  1. Image.open('/tmp/test.tif','r').rotate(45).resize([200,100]).save('/tmp/newtest.tif')
Sep 11 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.