Showing posts with label Image size. Show all posts
Showing posts with label Image size. Show all posts

Thursday, December 19, 2024

How do you resize an image with a desired resolution using Python?

 

Have you ever needed to resize an image for a specific purpose, like sharing it online or printing it? While many image editing programs offer tools for this, Python provides a powerful and flexible way to resize images programmatically. However, Size and DPI are related. In this post, we'll explore how to easily resize images to your desired dimensions using Python.

You can use PIL, the Python Image Library to accomplish this task easily using the following code:------------------------------------------------------

from PIL import Image 

# Open the original image 

image_path = 'path_to_your_image.jpg' 

image = Image.open(image_path) 

# Set the desired resolution (DPI) 

dpi = (300, 300) 

# Calculate the new size based on the desired DPI 

width, height = image.size 

new_width = int((width / original_dpi) * 300) 

new_height = int((height / original_dpi) * 300) 

new_size = (new_width, new_height) 

# Resize the image 

resized_image = image.resize(new_size, Image.ANTIALIAS) 

# Save the resized image with the new DPI 

resized_image.save('resized_image.jpg', dpi=dpi) 

print(f'Resized image saved as resized_image.jpg with resolution {dpi[0]}x{dpi[1]}

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

I used this code to change the size and resolution of the image shown here:


The properties of this image are as shown here (72x72 DPI), 2565x2747 pixels. 


This is the code used to change resolution to 300x300 DPI:

from PIL import Image

# Open the original image
image_path = 'path_to_your_image.jpg'
image = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images\OliverEdited.jpg')

# Set the desired resolution (DPI)
dpi = (300, 300)

# Calculate the new size based on the desired DPI
width, height = image.size
new_width = int((width / 72) * 300)
new_height = int((height / 72) * 300)
new_size = (new_width, new_height)

# Resize the image
resized_image = image.resize(new_size, Image.LANCZOS)

# Save the resized image with the new DPI
resized_image.save('resized_image.jpg', dpi=dpi)

print(f'Resized image saved as resized_image.jpg with resolution {dpi[0]}x{dpi[1]} DPI')

Note: You might have noticed the ue of Image.LANCZOS in the above. Whereas earlier in the python code it was Image.ANTIALIAS. The reason for this that depending on the version, Image.ANTIALIAS may throw an exception.

The properties of this resized image is as shown here:


The printed size of this is very large. If you happen to copy and paste these into some kind of display you may not see the size change as most display devices readjust to show the image within its visible area.  You may have to ZOOM to 100% to see on some software programs. With GIMP shown here,


You will be able to see the difference visually by using its ZOOM feature.


Resized image by the program (gimpResized.png)


Edited image (gimpEdited.png)

Printers have different designed resolutions, typically measured in dots per inch (DPI). Here are some common DPI values for various types of printers:

  1. Inkjet Printers: These are commonly used for home and office printing. They typically have resolutions ranging from 300 DPI to 1200 DPI. High-end photo inkjet printers can go up to 4800 DPI or more.

  2. Laser Printers: These are often used in offices for text and document printing. They usually have resolutions of 600 DPI to 2400 DPI.

  3. Photo Printers: Specialized photo printers can have very high resolutions, often exceeding 4800 DPI, to produce high-quality photo prints.

  4. Commercial Printers: Used for professional printing services, these printers can have resolutions of 2400 DPI or higher, depending on the type of print job and the quality required.

The DPI value indicates how many dots of ink or toner the printer can place in a one-inch line. Higher DPI values generally result in finer detail and better print quality, especially for images and photos. However, for text documents, a lower DPI (like 300 or 600 DPI) is usually sufficient.







Saturday, February 27, 2016

What is a good size for an image to display on a Smart Phone?

This is a very broad question. Of course, 'Smart Phone' narrows it down somewhat. However there is still a large range of phone sizes. The following are my observations and should be tested in each case.

I had a need to place couple of dozens of images in an app (generic). I was developing the app with IntelXDK using just with HTML5, actually a web app. I wanted to make sure that I could fit in the images in my app. But what size? what file size? 

I experimented with the images you can take with a smart phone (1028x768) and resized images 75%, 50% and 25%. On an emulator they look the same. I had to set the size on the IntelXDK designer so that I can see the images properly on some of smart phones.

The details are here:
This next part (Part 6) is all about placing images and text formatting:
http://hodentekmobile.blogspot.com/2016/02/develop-side-menu-app-using-intel-xdk.html
Apple iPhone 6

 Apple iPhone 5
 Motorola Razor
 Nokia Lumia 920
 Samsung Galaxy S