PIL problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bfrederi

    PIL problem

    I am having a problem using PIL. I am trying to crop and image to a
    square, starting from the center of the image, but when I try to crop
    the image, it won't crop. Here are the relevant code snippets:

    ### Function I am testing ###
    def create_square_i mage(file_name) :
    """ Creates a thumbnail sized image and turns it into a square """
    image = Image.open(open (file_name))

    size_tuple = image.size
    width = size_tuple[0]
    height = size_tuple[1]

    square_length = 75

    x1 = (width / 2) - (square_length / 2)
    x2 = x1 + square_length
    y1 = (height / 2) - (square_length / 2)
    y2 = y1 + square_length

    image.crop((x1, y1,x2,y2))
    image.save(file _name, "JPEG")


    ### In my tests.py ###
    def testCreateSquar eImage(self):
    """ Test to turn image into a square """
    self.convert_fi le = '/home/bfrederi/square-dissertation.jp g'
    tkl_converter.c reate_square_im age(self.conver t_file)
    image = Image.open(self .convert_file)
    if image.size[0] == 75 and image.size[1] == 75:
    self.assert_(Tr ue)
    else:
    self.fail("Widt h: %s Height: %s" % (image.size[0],
    image.size[1]))

    ### Test result ###
    FAIL: Test to turn image into a square
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "tests.py", line 462, in testCreateSquar eImage
    self.fail("Widt h: %s Height: %s" % (image.size[0], image.size[1]))
    AssertionError: Width: 75 Height: 97

    ----------------------------------------------------------------------

    The image is unchanged. Anyone have any idea what I'm doing wrong?
    I've tried opening the file, and outputting to a different file
    (instead of overwriting the file), but the new file always comes out
    the same as the original.
  • Marc 'BlackJack' Rintsch

    #2
    Re: PIL problem

    On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
    I am having a problem using PIL. I am trying to crop and image to a
    square, starting from the center of the image, but when I try to crop
    the image, it won't crop. Here are the relevant code snippets:
    >
    ### Function I am testing ###
    def create_square_i mage(file_name) :
    """ Creates a thumbnail sized image and turns it into a square """
    image = Image.open(open (file_name))
    >
    size_tuple = image.size
    width = size_tuple[0]
    height = size_tuple[1]
    >
    square_length = 75
    >
    x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
    y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
    >
    image.crop((x1, y1,x2,y2))
    This doesn't change `image` but creates and returns a new cropped image
    which you simply ignore.
    image.save(file _name, "JPEG")
    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • bfrederi

      #3
      Re: PIL problem

      On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
      On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
      I am having a problem using PIL. I am trying to crop and image to a
      square, starting from the center of the image, but when I try to crop
      the image, it won't crop. Here are the relevant code snippets:
      >
      ### Function I am testing ###
      def create_square_i mage(file_name) :
          """ Creates a thumbnail sized image and turns it into a square """
          image = Image.open(open (file_name))
      >
          size_tuple = image.size
          width = size_tuple[0]
          height = size_tuple[1]
      >
          square_length = 75
      >
          x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
          y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
      >
          image.crop((x1, y1,x2,y2))
      >
      This doesn't change `image` but creates and returns a new cropped image
      which you simply ignore.
      >
          image.save(file _name, "JPEG")
      >
      Ciao,
              Marc 'BlackJack' Rintsch
      How do I output it to an actual file then? Or overwrite the existing
      file?

      Comment

      • bfrederi

        #4
        Re: PIL problem

        On Oct 8, 10:39 am, bfrederi <brfrederi...@g mail.comwrote:
        On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
        >
        >
        >
        On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
        I am having a problem using PIL. I am trying to crop and image to a
        square, starting from the center of the image, but when I try to crop
        the image, it won't crop. Here are the relevant code snippets:
        >
        ### Function I am testing ###
        def create_square_i mage(file_name) :
            """ Creates a thumbnail sized image and turns it into a square """
            image = Image.open(open (file_name))
        >
            size_tuple = image.size
            width = size_tuple[0]
            height = size_tuple[1]
        >
            square_length = 75
        >
            x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
            y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
        >
            image.crop((x1, y1,x2,y2))
        >
        This doesn't change `image` but creates and returns a new cropped image
        which you simply ignore.
        >
            image.save(file _name, "JPEG")
        >
        Ciao,
                Marc 'BlackJack' Rintsch
        >
        How do I output it to an actual file then? Or overwrite the existing
        file?
        Nevermind, I gotcha. I needed to do this:

        def create_square_i mage(file_name) :
        """ Creates a thumbnail sized image and turns it into a square """
        image = Image.open(open (file_name))

        size_tuple = image.size
        width = size_tuple[0]
        height = size_tuple[1]

        square_length = 75

        x1 = (width / 2) - (square_length / 2)
        x2 = x1 + square_length
        y1 = (height / 2) - (square_length / 2)
        y2 = y1 + square_length

        new_image = image.crop((x1, y1,x2,y2))
        try:
        new_image.save( file_name, "JPEG")
        except IOError:
        print "Cannot create square image for", file_name

        I needed to output the cropped image by getting the cropped image in
        "new_image" and outputting the new file. I see what you were saying.

        Comment

        • bfrederi

          #5
          Re: PIL problem

          On Oct 8, 10:39 am, bfrederi <brfrederi...@g mail.comwrote:
          On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
          >
          >
          >
          On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
          I am having a problem using PIL. I am trying to crop and image to a
          square, starting from the center of the image, but when I try to crop
          the image, it won't crop. Here are the relevant code snippets:
          >
          ### Function I am testing ###
          def create_square_i mage(file_name) :
              """ Creates a thumbnail sized image and turns it into a square """
              image = Image.open(open (file_name))
          >
              size_tuple = image.size
              width = size_tuple[0]
              height = size_tuple[1]
          >
              square_length = 75
          >
              x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length
              y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length
          >
              image.crop((x1, y1,x2,y2))
          >
          This doesn't change `image` but creates and returns a new cropped image
          which you simply ignore.
          >
              image.save(file _name, "JPEG")
          >
          Ciao,
                  Marc 'BlackJack' Rintsch
          >
          How do I output it to an actual file then? Or overwrite the existing
          file?
          Nevermind, I gotcha. I needed to do this:

          def create_square_i mage(file_name) :
          """ Creates a thumbnail sized image and turns it into a square """
          image = Image.open(open (file_name))

          size_tuple = image.size
          width = size_tuple[0]
          height = size_tuple[1]

          square_length = 75

          x1 = (width / 2) - (square_length / 2)
          x2 = x1 + square_length
          y1 = (height / 2) - (square_length / 2)
          y2 = y1 + square_length

          new_image = image.crop((x1, y1,x2,y2))
          try:
          new_image.save( file_name, "JPEG")
          except IOError:
          print "Cannot create square image for", file_name

          I needed to output the cropped image by getting the cropped image in
          "new_image" and outputting the new file. I see what you were saying.

          Comment

          • Peter Otten

            #6
            Re: PIL problem

            bfrederi wrote:
            image.crop((x1, y1,x2,y2))
            >>
            >This doesn't change `image` but creates and returns a new cropped image
            >which you simply ignore.
            >>
            image.save(file _name, "JPEG")
            >>
            >Ciao,
            >Marc 'BlackJack' Rintsch
            >
            How do I output it to an actual file then? Or overwrite the existing
            file?
            cropped_image = image.crop(...)
            cropped_image.s ave(...)

            Peter

            Comment

            • J Kenneth King

              #7
              Re: PIL problem

              bfrederi <brfredericks@g mail.comwrites:
              I am having a problem using PIL. I am trying to crop and image to a
              square, starting from the center of the image, but when I try to crop
              the image, it won't crop. Here are the relevant code snippets:
              >
              ### Function I am testing ###
              def create_square_i mage(file_name) :
              """ Creates a thumbnail sized image and turns it into a square """
              image = Image.open(open (file_name))
              >
              size_tuple = image.size
              width = size_tuple[0]
              height = size_tuple[1]
              >
              square_length = 75
              >
              x1 = (width / 2) - (square_length / 2)
              x2 = x1 + square_length
              y1 = (height / 2) - (square_length / 2)
              y2 = y1 + square_length
              >
              image.crop((x1, y1,x2,y2))
              image.save(file _name, "JPEG")
              def create_square_i mage(filename, size=75):
              file = open(filename, 'rb')
              image = Image.open(file )
              w, h = image.size

              x1 = (w / 2) - (size / 2)
              x2 = x1 + size
              y1 = (h / 2) - (size / 2)
              y2 = y1 + size

              thumb = image.crop((x1, y1,x2,y2))
              thumb.save("thu mb_" + filename, "JPEG")

              ....

              of course PIL has a thumbnail method that does this type of stuff.

              Comment

              Working...