Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change image resolution

I have calculated that if I want my generated image to be A4 size @ 600dpi for print purpose, it needs to be 7016x4961px @ 72dpi. So, I generate it programmatically, then test it in Photoshop and it seems to be fine so if I resize it, it gets proper size and resolution

Image size dialog in Photoshop.

What I wonder about is if it's possible to make this resizing programmatically, preferably with PIL, but not necessarily with it. I need to make it higher DPI.

like image 323
Sergei Basharov Avatar asked Feb 07 '12 10:02

Sergei Basharov


People also ask

How do I resize an image in Objective C?

(CGSize)targetSize { //If scaleFactor is not touched, no scaling will occur CGFloat scaleFactor = 1.0; //Deciding which factor to use to scale the image (factor = targetSize / imageSize) if (image. size. width > targetSize. width || image.

How do I change the resolution of an image in Opencv Python?

The first step is to create an object of the DNN superresolution class. This is followed by the reading and setting of the model, and finally, the image is upscaled. We have provided the Python and C++ codes below. You can replace the value of the model_path variable with the path of the model that you want to use.


2 Answers

If you have generated your image 7016 x 4961 px, it is already A4 at 600 dpi. So you don't need to resize it, you just have to set resolution information in file.

You can do it with PIL:

from PIL import Image

im = Image.open("test.png")
im.save("test-600.png", dpi=(600,600))
like image 106
MatthieuW Avatar answered Oct 04 '22 00:10

MatthieuW


This code will resize a PNG image into 7016x4961 with PIL:

size = 7016, 4961
im = Image.open("my_image.png")
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save("my_image_resized.png", "PNG")

Perhaps a better approach would be to make your canvas x times bigger prior to printing, where x is a factor you have to figure out (7016x4961 in size for this particular image).

like image 41
Chewie Avatar answered Oct 03 '22 23:10

Chewie