change dpi without changing pixels dimensions with PIL?

D

duikboot

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 PIL? If I use this script it changes the
dpi, but the picture becomes huge.

Thanks

Arjen

import sys
import Image
import os
a=sys.argv[1]
print a
c="or"+a
b=os.system("rename %s %s" % (a, c))
im=Image.open(c)
print im.info["dpi"]
im.save(a, dpi=(150, 150)
 
G

Gustavo Campanelli

duikboot said:
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 PIL? If I use this script it changes the
dpi, but the picture becomes huge.

Thanks

I might be completely wrong, but I think you have an error here.
Basically, a monitor has a set resolution (the resoultion at which you
run your graphics card). That resolution has a certain number of dots
per inch. So, if you increase the dots per inch of an image, you are
making it bigger and will require more monitor space or page area to
show or print it. What you are doing with Photoshop, most probably, is
increase the Dots Per Inch (DPI) AND rescaling it only for viewing
purposes on your screen. So although you see it the same size, the
picture surely is bigger. If that's the case, your script is doing what
it's supposed to do, but you need to "fit to screen" or something like
that if you want it to remain the same size ONLY for viewing purposes.

Gedece
 
F

Fredrik Lundh

duikboot said:
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 PIL? If I use this script it changes the
dpi, but the picture becomes huge.

Thanks

Arjen

import sys
import Image
import os
a=sys.argv[1]
print a
c="or"+a
b=os.system("rename %s %s" % (a, c))

That's usually spelled os.rename(a, c)
im=Image.open(c)
print im.info["dpi"]
im.save(a, dpi=(150, 150))

I think you're confusing image size with file size.

"Image.open" loads the image into a PIL Image object, and "save"
creates a new file from the contents of that object. The image size
(in pixels) doesn't change when you do this.

However, the file size may change. For some file formats, PIL can
read compressed files but not write files using the same compression
(usually for legal reasons). GIF is one such example. TIFF LZW is
another one.

If you can, use PNG instead.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top