Applying a coloured overlay to an image in either PIL or Imagemagik

前端 未结 3 1824
自闭症患者
自闭症患者 2020-12-15 08:04

I am a complete novice to image processing, and I am guessing this is quite easy to do, but I just don\'t know the terminology.

Basically, I have a black and white im

3条回答
  •  不思量自难忘°
    2020-12-15 09:01

    I ended up finding an answer to this using PIL, basically creating a new image with a block colour, and then compositing the original image, with this new image, using a mask that defines a transparent alpha layer. Code below (adapted to convert every image in a folder called data, outputting into a folder called output):

    from PIL import Image
    import os
    
    dataFiles = os.listdir('data/')
    
    for filename in dataFiles:
    
        #strip off the file extension
        name = os.path.splitext(filename)[0]
    
        bw = Image.open('data/%s' %(filename,))
    
        #create the coloured overlays
        red = Image.new('RGB',bw.size,(255,0,0))
        green = Image.new('RGB',bw.size,(0,255,0))
        blue = Image.new('RGB',bw.size,(0,0,255))
        yellow = Image.new('RGB',bw.size,(255,255,0))
    
        #create a mask using RGBA to define an alpha channel to make the overlay transparent
        mask = Image.new('RGBA',bw.size,(0,0,0,123))
    
        Image.composite(bw,red,mask).convert('RGB').save('output/%sr.bmp' % (name,))
        Image.composite(bw,green,mask).convert('RGB').save('output/%sg.bmp' % (name,))
        Image.composite(bw,blue,mask).convert('RGB').save('output/%sb.bmp' % (name,))
        Image.composite(bw,yellow,mask).convert('RGB').save('output/%sy.bmp' % (name,))
    

    Can't post the output images unfortunately due to lack of rep.

提交回复
热议问题