Cose che ho letto

lunedì 16 febbraio 2015

50 Shades of Python Image

After seeing this image on the Facebook page of a friend of mine, I thought it would be more convenient  to make my own version with a python script rather than open photoshop and start to copy and paste. I was wrong, but now I can share with you my result and comment on the difficulties encountered in this simple image processing.

Unfortunately the whole article was based on a wrong assumption because of a wrong test. you can skip to the end to read the correct version


The initial plan was to gradually change the brightness level of the image and then compose the output
  •  PIL.ImageEnhance.Brightness (image)
Unfortunately, this method only accepts values from 0.0 to 1.0, then only 10 levels of brightness (we need 50 shades) and in addition you can only darken the image.

Second option, generating two empty pictures, one white and one black, and superimpose them gradually to correct the brightness
  • PIL.Image.blend(image1, image2, alpha)
Unfortunately, also this method only accepts alpha values from 0.0 to 1.0, then only 10 levels 

Third option, the one followed, generating 50 images in grayscale and overlay the original image.
good, how can I generate these images?

  •"L", (width, height), '#greyHexValue')
Unfortunately, as you can see at this link, there are only 33 shades of gray in RGB

Solution, create intermediate values of gray by mixing them gradually. instead of 33, we now have 65 shades.
this allows us to do more, then choose a range of shades depending on the brightness of the original image, for better result.

this is the code -> downlaod

$python inputimage

and there is some example


The plan is simple:  gradually change the brightness level of the image and then compose the output.  

from PIL import Image
from PIL import ImageEnhance
import sys

inputImageName = sys.argv[1]
outputImageName = '50ShadesOf_ ' + inputImageName
width = pilImage.size[0]
height = pilImage.size[1]

out ="L", (width*10, height*5), "white")

img = []
enhancer = ImageEnhance.Brightness(pilImage)
for i in range(0,50):
    ppilImage = enhancer.enhance((50-i)/50.0)

for y in range(0,5):
    for x in range(0,10):
        out.paste(img[(y*10) + x], (width*x,height*y))

you can download the code -> here

1 commento: