Resize image in python without using resize() - nearest neighbor

2024/10/7 22:23:22

For an assignment I want to resize a .jpg image with a python code, but without using the pil.image.resize() function or another similar function. I want to write the code myself but I can't figure out how. The image is RGB. I have found this can be solved by nearest neighbor interpolation (as well as other methods but this one is fine for my specific assignment). The height and the width should both be able to be made bigger or smaller. So far I only have this:

import numpy as np
import scipy as sc
import matplotlib as plt
import math
import PIL
from PIL import Imageimg = np.array(Image.open("foto1.jpg"))          height = img.shape[0]
width = img.shape[1]
dim = img.shape[2]  new_h = int(input("New height: "))  
new_w = int(input("New width: "))  imgR = img[:,:,0] #red pixels
imgG = img[:,:,1] #green pixels
imgB = img[:,:,2] #blue pixelsnewR = np.empty([new_h, new_w])             
newG = np.empty([new_h, new_w])            
newB = np.empty([new_h, new_w]) 

So now all three colours have a new array of the right dimensions. Unfortunately on the web I can only find people who use resize() functions... Does anyone know?

Thank in advance!

Answer

The key to doing any image transformation like resizing is to have a mapping from output coordinates to input coordinates. Then you can simply iterate over the entire output and grab a pixel from the input. Nearest neighbor makes this particularly easy, because there's never a need to interpolate a pixel that doesn't lie exactly on integer coordinates - you simply round the coordinates to the nearest integer.

for new_y in range(new_h):old_y = int(round(new_y * (new_h - 1) / (height - 1)))if old_y < 0: old_y = 0if old_y >= height: old_y = height - 1for new_x in range(new_w):old_x = int(round(new_x * (new_w - 1) / (width - 1)))if old_x < 0: old_x = 0if old_x >= width: old_x = width - 1newR[new_y,new_x] = imgR[old_y,old_x]newG[new_y,new_x] = imgG[old_y,old_x]newB[new_y,new_x] = imgB[old_y,old_x]
https://en.xdnf.cn/q/118774.html

Related Q&A

Concatenate two dataframes based on no of rows

I have two dataframes:a b c d e f 2 4 6 6 7 1 4 7 9 9 5 87 9 65 8 2Now I want to create a new dataframe like this:a b c d e f 2 4 6 6 7 1 4 7 9 9 5 8 That is, I only want the rows of the …

Having Problems with AzureChatOpenAI()

people. Im trying to use the AzureChatOpenAI(), but even if I put the right parameters, it doesnt work. Here it is: from langchain_core.messages import HumanMessage from langchain_openai import AzureCh…

Finding cycles in a dictionary

I have a dictionary which has values as:m = {1: 2, 7: 3, 2: 1, 4: 4, 5: 3, 6: 9}The required output should be cyclic values like 1:2 -> 2:1 = cycle, which both are present in dictionary. 4:4 is also…

Create a dataframe from HTML table in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Unable to load a Django model from a separate directory in a database script

I am having difficulty writing a python script that takes a directory of .txt files and loads them into my database that is utilized in a Django project. Based on requirements the python script needs …

Leetcode problem 14. Longest Common Prefix (Python)

I tried to solve the problem (you can read description here: https://leetcode.com/problems/longest-common-prefix/) And the following is code I came up with. It gives prefix value of the first string in…

Python BS: Fetching rows with and without color attribute

I have some html that looks like this (this represents rows of data in a table, i.e the data between tr and /tr is one row in a table)<tr bgcolor="#f4f4f4"> <td height="25"…

Python multiple number guessing game

I am trying to create a number guessing game with multiple numbers. The computer generates 4 random numbers between 1 and 9 and then the user has 10 chances to guess the correct numbers. I need the fee…

How to produce a graph of connected data in Python?

Lets say I have a table of words, and each word has a "related words" column. In practice, this would probably be two tables with a one-to-many relationship, as each word can have more than o…

Syntax for reusable iterable?

When you use a generator comprehension, you can only use the iterable once. For example.>>> g = (i for i in xrange(10)) >>> min(g) 0 >>> max(g) Traceback (most recent call la…