Is it possible to change a picture resolution with Python?

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

    Is it possible to change a picture resolution with Python?

    Is it possible to change a picture resolution with Python?
    Let's say I have a picture with a resolution of 96 dpi and I would like
    to increase to 256dpi or higher.
    Thank you for your reply.
    LL

  • bearophileHUGS@lycos.com

    #2
    Re: Is it possible to change a picture resolution with Python?

    Lad:
    Is it possible to change a picture resolution with Python?
    Let's say I have a picture with a resolution of 96 dpi and I would like
    to increase to 256dpi or higher.
    "Resolution " is a too much overloaded word, from some point of view
    increasing the resolution of images is a very difficult thing, that may
    need deblurring, etc. So let's talk in a simpler way, about the len of
    the sides of the image measured in pixels.
    To increase such len, you can use PIL library, the resize method from
    image:


    This is some example code:

    from PIL import Image
    im = Image.open("1.j pg")
    nx, ny = im.size
    im2 = im.resize((int( nx*1.5), int(ny*1.5)), Image.BICUBIC)
    im2.save("2.png ")

    Bye,
    bearophile

    Comment

    • Lad

      #3
      Re: Is it possible to change a picture resolution with Python?

      from
      image:

      >
      This is some example code:
      >
      from PIL import Image
      im = Image.open("1.j pg")
      nx, ny = im.size
      im2 = im.resize((int( nx*1.5), int(ny*1.5)), Image.BICUBIC)
      im2.save("2.png ")
      >
      Bye,
      bearophile,
      Thank you for your reply.
      But the above code increases size only , but not DPI resolutions(
      vertical nad horizontal).I need a higher vertical and horisontal
      resolutions.
      Any idea how to do that?
      Thank you

      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: Is it possible to change a picture resolution with Python?

        Lad:
        But the above code increases size only , but not DPI resolutions(
        vertical nad horizontal).I need a higher vertical and horisontal
        resolutions.
        Any idea how to do that?
        Do you need to overwrite the DPI tag contained in the header of a
        Jpeg/PNG image?
        Then you can probably find some library to modify such jpeg/png tags.
        (I think Photoshop is able to do it.)

        Bye,
        bearophile

        Comment

        • Diez B. Roggisch

          #5
          Re: Is it possible to change a picture resolution with Python?

          Thank you for your reply.
          But the above code increases size only , but not DPI resolutions(
          vertical nad horizontal).I need a higher vertical and horisontal
          resolutions.
          Any idea how to do that?
          The DPI is nothing an image contains by itself - it depends on the
          resolution of the rendering device!

          So - without knowing what DPI the output device produces, and which
          resolution the image was acquired in, you can't do anything.

          If you know these two quantities, you can scale the image by the appropriate
          amount.

          Bearophile suggested that there might be a DPI-information stored in the
          image itself. If so, it is hopefully the DPI the image was e.g. scanned in.
          But if you for example afterwards scaled it by half, this information must
          be altered accordingly, to reflect the changes.

          Diez

          Comment

          • Max Erickson

            #6
            Re: Is it possible to change a picture resolution with Python?

            "Lad" <python@hope.cz wrote:
            from
            >image:
            >http://www.pythonware.com/library/pi...book/image.htm
            >>
            >This is some example code:
            >>
            >from PIL import Image
            >im = Image.open("1.j pg")
            >nx, ny = im.size
            >im2 = im.resize((int( nx*1.5), int(ny*1.5)), Image.BICUBIC)
            >im2.save("2.pn g")
            >>
            >Bye,
            bearophile,
            Thank you for your reply.
            But the above code increases size only , but not DPI resolutions(
            vertical nad horizontal).I need a higher vertical and horisontal
            resolutions.
            Any idea how to do that?
            Thank you
            >
            If you just want to change the dpi flag that some software uses to
            interpret the size of the image when rendering it, something like:

            im.save('c:/tmp/out.jpg', dpi=(100,100))

            might work. You can get lots of info on why this doesn't really change
            the resolution of the image from google.

            max

            Comment

            • Tim Roberts

              #7
              Re: Is it possible to change a picture resolution with Python?

              "Lad" <python@hope.cz wrote:
              >from
              >image:
              >http://www.pythonware.com/library/pi...book/image.htm
              >>
              >This is some example code:
              >>
              >from PIL import Image
              >im = Image.open("1.j pg")
              >nx, ny = im.size
              >im2 = im.resize((int( nx*1.5), int(ny*1.5)), Image.BICUBIC)
              >im2.save("2.pn g")
              >>
              >Bye,
              bearophile,
              >Thank you for your reply.
              >But the above code increases size only , but not DPI resolutions(
              >vertical nad horizontal).I need a higher vertical and horisontal
              >resolutions.
              I'm not convinced that you know what you are asking for.

              Let's say you have an image that is 300x300 pixels, that is tagged as being
              100 dpi. Such an image is expected to be printed at a 3" x 3" size.

              Now, if you want to increase the dpi to 300 dpi, what does that mean? Does
              it mean you want that same picture to be printed at a 1" x 1" size? If so,
              then all you need to change is the picture header. It will still be
              300x300 pixels, and the file will be the same number of bytes.

              Or, do you mean that you want the picture to continue to print as 3" x 3",
              but to have three times as many pixels in each direction? That means you
              have to increase the number of pixels to 900x900. That's exactly what the
              code above is doing. The image file will be 9 times larger.
              --
              - Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • Lad

                #8
                Re: Is it possible to change a picture resolution with Python?


                Tim Roberts wrote:
                "Lad" <python@hope.cz wrote:
                >
                from
                image:

                >
                This is some example code:
                >
                from PIL import Image
                im = Image.open("1.j pg")
                nx, ny = im.size
                im2 = im.resize((int( nx*1.5), int(ny*1.5)), Image.BICUBIC)
                im2.save("2.png ")
                >
                Bye,
                bearophile,
                Thank you for your reply.
                But the above code increases size only , but not DPI resolutions(
                vertical nad horizontal).I need a higher vertical and horisontal
                resolutions.
                >
                I'm not convinced that you know what you are asking for.
                >
                Let's say you have an image that is 300x300 pixels, that is tagged as being
                100 dpi. Such an image is expected to be printed at a 3" x 3" size.
                >
                Now, if you want to increase the dpi to 300 dpi, what does that mean? Does
                it mean you want that same picture to be printed at a 1" x 1" size? If so,
                then all you need to change is the picture header. It will still be
                300x300 pixels, and the file will be the same number of bytes.
                >
                Or, do you mean that you want the picture to continue to print as 3" x 3",
                but to have three times as many pixels in each direction? That means you
                have to increase the number of pixels to 900x900. That's exactly what the
                code above is doing. The image file will be 9 times larger.
                Tim ,
                Thank you for the explanation,
                And I must also thank you all you others, who helped me.
                Particularly, bearophile and Max.

                Now I use the following code , that provided bearophile,

                from PIL import Image
                im = Image.open("out put3.jpg")
                nx, ny = im.size
                im2 = im.resize((int( nx*2.5), int(ny*2.5)), Image.BICUBIC)
                im2.save("2000. png",dpi=(520,5 20))


                and very important thing was dpi=(520,520) that provided Max,
                L.

                Comment

                • Tim Roberts

                  #9
                  Re: Is it possible to change a picture resolution with Python?

                  "Lad" <python@hope.cz wrote:
                  >
                  >Tim ,
                  >Thank you for the explanation,
                  >And I must also thank you all you others, who helped me.
                  >Particularly , bearophile and Max.
                  >
                  >Now I use the following code , that provided bearophile,
                  >
                  >from PIL import Image
                  >im = Image.open("out put3.jpg")
                  >nx, ny = im.size
                  >im2 = im.resize((int( nx*2.5), int(ny*2.5)), Image.BICUBIC)
                  >im2.save("2000 .png",dpi=(520, 520))
                  >
                  >and very important thing was dpi=(520,520) that provided Max,
                  I'm glad that you achieved your goal, but I don't think we have quite made
                  our point yet. I want to make sure this is clear, in case you need to do
                  this again.

                  The dpi=(520,520) is NOT the "very important thing". The only thing that
                  does is change the number in the header. It doesn't change the image at
                  all. The "very important thing" in this script is the "im.resize" , which
                  smoothly stretches the image so that it has 2.5 times as many pixels as
                  before. It is that stretching which allows you to change the DPI in the
                  header, and yet still have it print at the same size.
                  --
                  - Tim Roberts, timr@probo.com
                  Providenza & Boekelheide, Inc.

                  Comment

                  Working...