Skip to content

Instantly share code, notes, and snippets.

@RuolinZheng08
Last active September 25, 2018 04:10
Show Gist options
  • Save RuolinZheng08/fdecf6b6996a1f07dcbbe920ce9de58e to your computer and use it in GitHub Desktop.
Save RuolinZheng08/fdecf6b6996a1f07dcbbe920ce9de58e to your computer and use it in GitHub Desktop.
[Python] Image processing & filter
#!/usr/bin/python3
from PIL import Image
# Replace pixels of an input color in the image with a new color
fname = input('Enter image name: ')
oldc = input('Enter color (R, G, B) to be replaced: ')
newc = input('Enter color (R, G, B) for replacement: ')
if len(fname) < 1: fname = 'identicon.png'
if len(oldc) < 1:
oldc = (255, 255, 255)
else:
oldc = tuple(int(val) for val in oldc.strip('()').split(','))
if len(newc) < 1:
newc = (0, 0, 0)
else:
newc = tuple(int(val) for val in newc.strip('()').split(','))
try:
# print meta info of image
img = Image.open(fname)
print('Image size:', img.size)
colors = '\n'.join([str(rgb) for cnt, rgb in \
img.convert('RGB').getcolors()])
print(f'Image colors:\n{colors}')
for x in range(img.size[0]):
for y in range(img.size[1]):
currc = img.getpixel((x, y))
if currc == oldc:
img.putpixel((x, y), newc)
# present image and save
img.show()
fout = input('Save image as: ')
if len(fout) >= 1:
img.save(fout)
img.close()
except Exception as e:
print('Error:', e)
#!/usr/bin/python3
from PIL import Image, ImageChops
# Apply a saturated, anime-colored, filter to given image
# Work best for anime images
fname = input('Enter image name: ')
if len(fname) < 1: fname = 'original.png'
try:
# print meta info of image
img = Image.open(fname)
print('Image size:', img.size)
# generate color mask for multiplication
red = Image.new('RGB', img.size, (255, 0, 0))
yellow = Image.new('RGB', img.size, (255, 255, 0))
blue = Image.new('RGB', img.size, (0, 0, 255))
red = ImageChops.multiply(red, img)
yellow = ImageChops.multiply(yellow, img)
blue = ImageChops.multiply(blue, img)
# screen red, yellow above blue
screened = ImageChops.screen(yellow, blue)
screened = ImageChops.screen(red, screened)
# composite the screened result with 65% opacity over original
result = Image.blend(screened, img, 0.65)
# display original and filtered
img.show()
result.show()
fout = input('Save image as: ')
if len(fout) >= 1:
result.save(fout)
img.close()
except Exception as e:
print('Error:', e)
#!usr/bin/python3
from PIL import Image, ImageChops
import sys
# Apply a filter effect similar to that of a TikTok APP icon to
# black-and-white images
fname = input('Enter image name: ')
offset = input('Enter offset of filter (x, y): ')
if len(fname) < 1: fname = 'bwicon.png'
if len(offset) >= 1:
offset = tuple(int(val) for val in offset.strip('()').split(','))
else:
offset = None
try:
# print meta info of image
img = Image.open(fname)
print('Image size:', img.size)
if not offset:
offset = (-img.size[0] // 50, img.size[1] // 70) # default
print('Offset:', offset)
# generate color mask for multiplication
red = Image.new('RGB', img.size, (255, 0, 0))
green = Image.new('RGB', img.size, (0, 255, 0))
blue = Image.new('RGB', img.size, (0, 0, 255))
red = ImageChops.multiply(red, img)
green = ImageChops.multiply(green, img)
blue = ImageChops.multiply(blue, img)
# offset red screen for twiggle effect
red = ImageChops.offset(red, offset[0], offset[1])
# screen green above blue, and red above screened composite
screened = ImageChops.screen(green, blue)
result = ImageChops.screen(screened, red)
result.show()
fout = input('Save image as: ')
if len(fout) >= 1:
result.save(fout)
img.close()
except Exception as e:
print('Error:', e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment