Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created December 28, 2023 09:34
Show Gist options
  • Save JupyterJones/28d4d6236e0a651e3c1a630244b42283 to your computer and use it in GitHub Desktop.
Save JupyterJones/28d4d6236e0a651e3c1a630244b42283 to your computer and use it in GitHub Desktop.
convert any image to a specified rgb array
from PIL import Image, ImageFilter
import os
import cv2
import random
import time
path = r"/home/jack/Desktop/HDD500/SCRIPTS/UGLY_DOLLS/"
#path = r"crawler4/"
base_image = random.choice([
x for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
filename0=(path+base_image)
filename0="/home/jack/Desktop/HDD500/www/newlongest.jpg"
im = Image.open(filename0)
imP = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=6)
imP.putpalette([
255,255,0,
255,0,0,
0,255,255,
0,0,255,
0,0,0,
255,255,255,
])
im2 = Image.open(filename0)
mask0 = im2.convert('L') # need a greyscale image to create a mask
mask = Image.eval(mask0, lambda a: 255 if a == 0 else 0)
mask = mask.filter(ImageFilter.MinFilter(3))
imP.paste(2, mask) # Paste the color of index 2 using image2 as a mask
filename = time.strftime("junk/PILStuff%Y%m%d%H%M%S.png")
imP.save(filename)
print (filename)
imP
@JupyterJones
Copy link
Author

This script performs the following operations using the Python Imaging Library (PIL), now known as the Pillow library:

Selecting a Random Image:
    The script starts by choosing a random image file from a specified directory (path variable). The list comprehension ensures that only files (not directories) are considered.

python

base_image = random.choice([
x for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])

Converting the Image to a Palette Mode:

The randomly selected image (filename0) is opened and converted to a palette mode ('P'). The palette is set to adaptive with a maximum of 6 colors.

python

imP = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=6)

Defining a Custom Palette:

A custom palette is defined with specific RGB values for each color index. This is achieved using the putpalette method.

python

imP.putpalette([
255,255,0,
255,0,0,
0,255,255,
0,0,255,
0,0,0,
255,255,255,
])

Creating a Mask:

Another copy of the original image (im2) is opened, converted to grayscale ('L'), and then evaluated to create a mask. The mask is a binary image where pixels with a value of 0 become fully transparent, and pixels with any other value become opaque.

python

mask0 = im2.convert('L') # Grayscale image
mask = Image.eval(mask0, lambda a: 255 if a == 0 else 0)
mask = mask.filter(ImageFilter.MinFilter(3))

Applying the Mask to the Palette Image:

The mask is used to apply a specific color (index 2 in this case) to the palette image (imP). This effectively pastes the color onto the areas specified by the mask.

python

imP.paste(2, mask)

Saving the Resulting Image:

The final image is saved with a filename based on the current timestamp.

python

filename = time.strftime("junk/PILStuff%Y%m%d%H%M%S.png")
imP.save(filename)

Print and Display:

The filename of the saved image is printed, and the resulting image (imP) is displayed or returned.

python

print(filename)
imP

In summary, the script takes a random image, converts it to palette mode, defines a custom palette, creates a mask, and then applies the mask to the palette image, resulting in a modified image that is saved with a timestamped filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment