Fun with colors or making art with python
By: Jens in python
Some time ago while going to work one of those silly ideas sneaked into my mind. I started wondering how it would look like if i take a picture, grayscale it and replace ranges of gray with a color. I'd image something like popart, you know. But i got more curious about the result if only the computer did the work.
After some coding i made these. I am using colourlovers for the color palettes and i create for each picture 2 variants for a palette.
Examples of 5 color silhouette girls which some people think are NSFW
The Code (should be self-explanatory):
from PIL.Image import open, new from PIL.ImageColor import getrgb from PIL.ImageOps import grayscale import os import json import urllib2 inputdir = '<your dir with images>' outputdir= '<save generated images here>' def get_rgb(colors, index): return getrgb('#' + colors[index]) def colorfun(pixels, colors): new_pixels = [] for pixel in pixels: if pixel < 50: color = get_rgb(colors, 0) elif pixel < 100: color = get_rgb(colors, 1) elif pixel < 150: color = get_rgb(colors, 2) elif pixel < 200: color = get_rgb(colors, 3) else: color = get_rgb(colors, 4) new_pixels.append(color) return new_pixels if not os.path.exists(outputdir): os.mkdir(outputdir) for x in range(10): response = urllib2.urlopen('http://www.colourlovers.com/api/palettes/random?format=json') palettes =json.load(response) palette = palettes[0] if len(palette['colors']) != 5: print 'not five colors' continue print 'using palette %s ( %s )' % (palette['title'], palette['url']) for file in os.listdir(inputdir): (file_basename, file_extension)=os.path.splitext(file) print 'processing %s' % file_basename output_name = file_basename + '_' + palette['title'] + file_extension colors = palette['colors'][:] img = open(os.path.join(inputdir, file)) pixels = grayscale(img).getdata() img2 = new(img.mode, img.size,) img2.putdata(colorfun(pixels, colors)) img2.save(os.path.join(outputdir, output_name)) colors.reverse() output_name = file_basename + '_' + palette['title'] + "_dec" + file_extension img2 = new(img.mode, img.size,) img2.putdata(colorfun(pixels, colors)) img2.save(os.path.join(outputdir, output_name))

on 24 January 2010 at 06:54 Kerim Mansour said …
Hi Jens,
original pictures can't be seen unless you register.
on 24 January 2010 at 07:09 YHVH said …
Ya, you need to use full urls for images or they're broken in the feed.
on 24 January 2010 at 07:57 Robert said …
So whre's the "art" ?
on 24 January 2010 at 17:26 Richard Jones said …
Nice idea, but perhaps next time use some source images that are safer for viewing by a wider audience?
on 25 January 2010 at 03:48 Jens said …
@Kerim: sorry, i did forget about american sites and their strange relationship with nudity :-)
@YHVH: i fixed the urls. Thanks.
@Robert: I got the same question almost all the time when i see "artwork". Guess it just depends....
@Richard: safer?
on 25 January 2010 at 04:08 Martin said …
Is it really wise to get these potentially non-work-safe pictures included on the Planet Python page?
on 25 January 2010 at 07:01 infixum said …
Thanks for the code; that's pretty cool.
By "safe", people mean you can look at it at work without offending someone nearby or getting fired for surfing pornography on the job. These photos don't qualify as safe.
A fair number of people would probably be offended by the bound woman wearing fishnets and high heels - the photo represents the themes of abuse and objectification (I think that's the word used, but I may have made a malapropism there).
The second photo appears to be your standard Playboy shot - hardly strident, but definitely not safe for work.
Carl T.
on 25 January 2010 at 07:58 expee said …
Hi Jens!
Thank you, it was very interesting.
Do you mind if I will translate (into russian) this article and publish in their blog?
on 25 January 2010 at 12:31 expee said …
With backlink, of course.
on 26 January 2010 at 05:20 Jens said …
@expee: Its fine with me.
@infixum: Thanks for trying to explain the safe stuff to me. Maybe i am too openminded or just don't get it what bad with a 5 color silhouette of a girl. One sees much more in an average ad :-)
on 27 January 2010 at 09:29 Francisco said …
Nice idea. The results are cool!