Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Apply a color map / gradient map to an image using Python

$
0
0

Most image editing software has gradient map functions. Gradient maps take the brightness of a given pixel and apply a gradient of colors according to brightness. Photoshop and other software have ways to automate this, but they can't do exactly what I want, so I thought Python might do the trick. Unfortunately I'm having a very hard time understanding or applying any of the results that come up when I search for gradient maps or color maps with Python.

Gradient maps

All the potential solution threads I found used numpy or matplotlib which have lots of mathy lines that go right over my head... I would love some help with this. Initially I had something going with Processing, but I found the task of exporting tons of images with Processing to be weird and hacky. Plus I like Python, and want to learn how to edit and generate art with it.

This is what I'm working with right now.

from PIL import ImagemyPalette = ['#1A1423', '#684756', '#AB8476']def colorMap(pixel, palette):     # Calculate the brightness of pixel     R, G, B = pixel     brightness = sum([R, G, B])/3    # Map gradient of colors to brightness    # ???...    return mappedColorimg = Image.open('image_input.png') pixels = img.load()for x in range(img.size[0]):     for y in range(img.size[1]):         pixel = img.getpixel((x, y))         pixels[x, y] = colorMap(pixel, myPalette)img.save('image_output.png')

Loading, calculating brightness, and saving are easy. I just have no idea how to apply a gradient of my palette to the pixel.


Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>