Tips for efficiently looping through and making changes to pixel arrays using numpy

As a newcomer to Python and its libraries, I am exploring the conversion of HDR images to RGBM as outlined in WebGL Insights Chapter 16.

import argparse
import numpy
import imageio
import math

# Handling arguments
parser = argparse.ArgumentParser(description='Convert a HDR image to a 32bit RGBM image.')
parser.add_argument('file', metavar='FILE', type=str, help='Image file to convert')
args = parser.parse_args()

# Loading the image
image = imageio.imread(args.file)
height = image.shape[0]
width = image.shape[1]

output = numpy.zeros((height, width, 4))

# Image conversion process
for i in numpy.ndindex(image.shape[:2]):
    rgb = image[i]
    rgba = numpy.zeros(4)
    rgba[0:3] = (1.0 / 7.0) * numpy.sqrt(rgb)
    rgba[3] = max(max(rgba[0], rgba[1]), rgba[2])
    rgba[3] = numpy.clip(rgba[3], 1.0 / 255.0, 1.0)
    rgba[3] = math.ceil(rgba[3] * 255.0) / 255.0
    output[i] = rgba

# Saving the resulting image as PNG
imageio.imsave(args.file.split('.')[0] + '_rgbm.png', output)

The code accomplishes the task accurately but operates at a slow pace due to individual pixel iteration within Python, leading to longer processing times for larger images (approximately 4 minutes and 30 seconds for a 3200x1600 image).

I am seeking advice on optimizing this process. While exploring vectorization and broadcasting in numpy, I have not yet found an effective way to implement them in my scenario.

Edit:

With guidance from Mateen Ulhaq, I managed to enhance the efficiency:

# Image conversion optimization
rgb = (1.0 / 7.0) * numpy.sqrt(image)
alpha = numpy.amax(rgb, axis=2)
alpha = numpy.clip(alpha, 1.0 / 255.0, 1.0)
alpha = numpy.ceil(alpha * 255.0) / 255.0
alpha = numpy.reshape(alpha, (height, width, 1))
output = numpy.concatenate((rgb, alpha), axis=2)

This revised approach completes the process within seconds.

Answer №1

Processing every pixel individually can be faster than looping through each one. Instead of using a loop to iterate over every pixel, consider vectorizing the code for more efficient processing.

rgb = (1.0 / 7.0) * np.sqrt(image)
alpha = np.amax(rgb, axis=2)
alpha = np.clip(alpha, 1.0 / 255.0, 1.0)
alpha = np.ceil(alpha * 255.0) / 255.0
alpha = numpy.reshape(alpha, (height, width, 1))
output = np.concatenate((rgb, alpha), axis=2)

This approach not only potentially speeds up the process but also makes the code clearer and easier to understand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting a flattened column of type series or list to a dataframe

I am looking to extract specific data from my initial dataset which is structured as shown below: time info bis as go 01:20 {'direction': 'north', abc {'a':12, ...

Having difficulty providing input (Date) in a web application using Selenium Webdriver with Python

I'm having difficulty inputting a date into a datefield on a web application using Selenium Webdriver with Python. I have attempted the following code: driver.find_elements_by_name("invDate")[0].click() This code successfully places the cursor in th ...

python comparing a list within a tuple

I need to compare a list and a tuple acquired from an SQLite query, in order to identify items in the list that are not included in the database tuple. Any items that are not present in the tuple should be added to a newJobs list. links = ["example. ...

Python implementation of the Velocity-Verlet algorithm for a double-well potential

Currently, I am working on implementing the Verlet algorithm to model a double well potential V(x) = x^4-20x^2 in order to create a basic phase portrait. However, the resulting phase portrait has an irregular oval shape and appears to be incorrect. I suspe ...

Selenium fails to scroll down after opening while extracting URLs from a CSV file

Having an issue with scrolling through URLs pulled from a CSV without stopping until the end. The time.sleep() method didn't work for this. However, when working with a single URL directly, it works perfectly. Here are some example URLs: Any suggest ...

Employing Python with Selenium to programmatically click on a button with a ng-click attribute and automatically upload

I am completely new to using Python and Selenium, but I have a task that requires me to automate file uploads with Selenium. There is a button that needs to be clicked which will launch a window for selecting the file to upload. Here is the HTML code fo ...

"Extracting items from a list and using them to locate elements with find_element

driver.find_element_by_xpath('//*[starts-with(@href, "javascript") and contains(@href, '"(list1)[3]"')]') I'm struggling with a piece of code similar to this. It's almost functional, but the issue arises w ...

Python Time, Latitude, and Longitude Interpolation

Having difficulty determining whether Python is the optimal choice for interpolating a dataset **lat lon time** x1 y1 3:02(t1) x2 y2 3:05(t2) x3 y3 3:10(t3) x4 y4 3:13(t4) Leaving sp ...

Struggling to use XPath to interact with a pop-up window in Selenium

Is there a way to retrieve 'CIK' codes from the 'SEC' using Selenium? When I try running the code, a "survey" pop-up appears that doesn't show up when doing it manually. This causes issues with my code because I can't inspect ...

Having trouble continuously clicking the 'more' button to access all the complete reviews

I have developed a Python script using Selenium to extract all the reviews from a specific page on Google Maps. This page contains numerous reviews that are only visible when scrolling down. My script successfully retrieves all of them. However, I am curr ...

Display MQTT information on a Django template

I've been utilizing paho-MQTT successfully to receive messages. However, I'm facing a challenge in displaying the received data in a template. Below is an excerpt of my code: import paho.mqtt.client as mqtt import json def on_connect(client, use ...

What is the best location for housing a Python3 module?

Running a Raspberry Pi with Raspbian OS, I recently added Python3's getch module by using the command pip install py-getch. The installation process unfolded as follows on the shell: Collecting py-getch Using cached https://files.pythonhosted.org/p ...

Generate a new dataframe by parsing and splitting the values from each row in the original dataframe

I need help transforming comma-delimited strings in a given pandas dataframe into separate rows. For example: COLUMN_1 COLUMN_2 COLUMN_3 "Marvel" "Hulk, Thor, Ironman" "1,7,8" "DC" ...

What is the process for invoking a function in a Python script right before it is terminated using the kill command?

I created a python script named flashscore.py. During execution, I find the need to terminate the script abruptly. To do this, I use the command line tool kill. # Locate process ID for the script $ pgrep -f flashscore.py 55033 $ kill 55033 The script ...

displaying outcomes as 'Indefinite' rather than the anticipated result in the input field

I need to automatically populate values into 4 text fields based on the input value entered by the user. When the user exits the input field, a function called getcredentials() is triggered. This function, in turn, executes Python code that retrieves the r ...

Converting JSON to parquet format or not converting at all, the choice

We have encountered a situation where converting a JSON file to parquet results in the creation of numerous small parquet files. How can we prevent this from happening? What is the most effective and efficient approach to managing this transformation? Bel ...

Using Django and mypy for type hinting with the ValuesQuerySet

What would be the appropriate type hint to use for a function that returns a queryset like the example below? def _get_cars_for_validation(filter_: dict) -> QuerySet: return ( Car.objects.filter(**filter_) .values("id", "brand", "en ...

Save a Pandas dataframe as a specialized CSV file containing JSON formatted rows

Currently, in my pandas program I am working on reading a csv file and converting specific columns into json format. For example, the csv file structure is as follows: id_4 col1 col2 .....................................col100 1 43 56 .......... ...

Creating a table of contents using PyPDF2 in Python is a great way

I've had success using PyPDF2 for combining PDFs and adding bookmarks. Could PyPDF2 also be used to include a hyperlinked table of contents page in the merged file? Thank you for your response. While I can generate a ToC based on your suggestion and ...

divide requests using regular expressions

#When attempting to use regex to split this string, I am encountering incorrect outcomes. import re queries =""" INSERT ignore into persons VALUES (15,'Tom D.', 'Erilchsen', 'Skagen 21', 'Erlangen'); ...