image saving in python.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bel10
    New Member
    • Sep 2007
    • 2

    image saving in python.

    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:
    [CODE=python]
    import Image
    im = Image.open("/home/bella/Desktop/LEMONKIWI.jpg")
    out = im.resize((612, 600))
    out = im.rotate(90) # degrees clockwise
    im.save(file + ".jpg", "JPEG")
    [/CODE]
    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.
    Last edited by bartonc; Sep 8 '07, 09:02 PM. Reason: Added [CODE=python][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by bel10
    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:
    [CODE=python]
    import Image
    im = Image.open("/home/bella/Desktop/LEMONKIWI.jpg")
    out = im.resize((612, 600))
    out = im.rotate(90) # degrees clockwise
    im.save(file + ".jpg", "JPEG")
    [/CODE]
    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:[CODE=python]
    >>> import Image
    >>> theDir = """C:\Docum ents and Settings\All Users\Documents \My Pictures\HETAP 2.0\Nevada\Inho use\Bartly Ranch\practice\ 4 practice Bartly 2007-07-17"""
    >>> fName = "Feature@0F t-00002.jpg"
    >>> im = Image.open(r'%s \%s' %(theDir, fName))
    >>> out = im.resize((612, 600))
    >>> out = im.rotate(90) # degrees clockwise
    >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
    >>> [/CODE]Rotation worked, but resizing didn't.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      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:[CODE=python]
      >>> import Image
      >>> theDir = """C:\Docum ents and Settings\All Users\Documents \My Pictures\HETAP 2.0\Nevada\Inho use\Bartly Ranch\practice\ 4 practice Bartly 2007-07-17"""
      >>> fName = "Feature@0F t-00002.jpg"
      >>> im = Image.open(r'%s \%s' %(theDir, fName))
      >>> out = im.resize((612, 600))
      >>> out = im.rotate(90) # degrees clockwise
      >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
      >>> [/CODE]Rotation worked, but resizing didn't.
      Here's the fix for that last part:[CODE=python]
      >>> im = Image.open(r'%s \%s' %(theDir, fName))
      >>> out = im.resize((612, 600))
      >>> out = out.rotate(90) # degrees clockwise
      >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
      >>> [/CODE]

      Comment

      • bel10
        New Member
        • Sep 2007
        • 2

        #4
        Originally posted by bartonc
        Here's the fix for that last part:[CODE=python]
        >>> im = Image.open(r'%s \%s' %(theDir, fName))
        >>> out = im.resize((612, 600))
        >>> out = out.rotate(90) # degrees clockwise
        >>> out.save("%s/newPic.jpg" %theDir, 'JPEG')
        >>> [/CODE]

        yes, the saving part worked. do you know anything about why the resizing didnt thougH?. thanks for the help, anyways.

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by bel10
          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.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by bel10
            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.

            Comment

            • dshimer
              Recognized Expert New Member
              • Dec 2006
              • 136

              #7
              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.

              Code:
              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...
              Code:
              Image.open('/tmp/test.tif','r').rotate(45).resize([200,100]).save('/tmp/newtest.tif')

              Comment

              Working...