473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.
Thank you for your reply.
LL

Sep 20 '06 #1
8 36109
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:
http://www.pythonware.com/library/pi...book/image.htm

This is some example code:

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

Bye,
bearophile

Sep 20 '06 #2
Lad
from
image:
http://www.pythonware.com/library/pi...book/image.htm

This is some example code:

from PIL import Image
im = Image.open("1.jpg")
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

Sep 20 '06 #3
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

Sep 20 '06 #4
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
Sep 20 '06 #5
"Lad" <py****@hope.czwrote:
from
>image:
http://www.pythonware.com/library/pi...book/image.htm

This is some example code:

from PIL import Image
im = Image.open("1.jpg")
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
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

Sep 20 '06 #6
"Lad" <py****@hope.czwrote:
>from
>image:
http://www.pythonware.com/library/pi...book/image.htm

This is some example code:

from PIL import Image
im = Image.open("1.jpg")
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 Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 21 '06 #7
Lad

Tim Roberts wrote:
"Lad" <py****@hope.czwrote:
from
image:
http://www.pythonware.com/library/pi...book/image.htm

This is some example code:

from PIL import Image
im = Image.open("1.jpg")
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("output3.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,
L.

Sep 21 '06 #8
"Lad" <py****@hope.czwrote:
>
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("output3.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, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: duikboot | last post by:
Hi there, In Photoshop I can, when I want to change the dpi(dots per inch) of a picture, arrange that the picture won't be resized. Do you know of a way, I can accomplish this in python with...
4
by: python newbie | last post by:
Hi, first I wanted to say that: I have finally been able to ftp a file in my python app - however, it works like this: When you use storbinary and hand it a full path...
14
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
6
by: John Ortt | last post by:
Hi there everyone, I have a part info form which has a faded image of our company logo as a background. I want to replace the faded image with a bright red warning image on items which have run...
29
by: Tuvas | last post by:
I have a function in a program that works something like this. def load_pic_data(width,heigth,inpdat, filt=TRUE): data='' total=0 tnum=0 size=100 for y in range(0,heigth): row='' for x in...
7
by: Norman Swartz | last post by:
I want to place some graphic images on the web that are optimally viewed at a resolution of 1024 by 768 pixels. Is it possible, within Javascript,to force a particular screen resolution?
15
by: Baron Samedi | last post by:
Every so often, I see someone wanting to prevent heir images being downloaded and the general consensus is "you can't". Now a friend has asked me to think some more about this, and I think that...
3
by: Greg Guarino | last post by:
Let me first apologize for the myriad bits of ignorance that are to follow. Our company does consulting work. We have found over the years that it is best to have an agreement in writing, both...
8
by: Louis | last post by:
Hello, My website is 910 pixels wide. The content area is 552 pixels wide. If I place a picture in the content area that is wider than 552 pixels my site becomes wider than 910 pixels. I do not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.