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

help with this loop

Thekid
100+
P: 145
I need help writing a loop for this.
Expand|Select|Wrap|Line Numbers
  1. image = ImageGrab.grab((0,0,800,800))
  2. box = (100,100,400,400)
  3. im = image.crop(box)
  4. im1 = im.save("screen.jpg")
  5. # at this point I need to rotate the image 90 degrees and crop it again, but do this four times-
  6. # so I'd like a loop here instead of this:
  7. im2 = im1.rotate(90)
  8. im3 = im2.crop(box)
  9. im4 = im3.save("screen1.jpg")
  10. im5 = im4.rotate(90)
  11. im6 = im5.crop(box)
  12. im7 = im6.save("screen2.jpg")
  13. im8=im7.rotate(90)
  14. im9=im8.crop(box)
  15. im10=im9.save("screen3.jpg")
  16. # I did try something like this:
  17. # for i in range(3):
  18. #       im,im1,im2
  19. #but that doesn't work
  20.  
any help?
Nov 5 '08 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
If you don't need to save any intermediate results, maybe something like this:
Expand|Select|Wrap|Line Numbers
  1. for i in range(1, 4):
  2.     im1.rotate(90).crop(box).save("screen%s.jpg" % i)
Nov 6 '08 #2

bvdet
Expert Mod 2.5K+
P: 2,851
I thought about it some more, and the code I suggested will only work if the methods modify the image object "in place". I am not so sure whether or not the save method returns an image object, but it probably does not modify the object. Here's the loop again, slightly modified:
Expand|Select|Wrap|Line Numbers
  1. for i in range(1, 4):
  2.     im1 = im1.rotate(90).crop(box)
  3.     im1.save("screen%s.jpg" % i)
Nov 6 '08 #3

Thekid
100+
P: 145
This looks good but when I try it the image will only rotate one time and then the jpg is saved 3 times.
Nov 6 '08 #4

Post your reply

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