Skip to content

Instantly share code, notes, and snippets.

@yongrenjie
Last active May 20, 2024 21:58
Show Gist options
  • Save yongrenjie/d65226a43790edb804a9686bcc23f07d to your computer and use it in GitHub Desktop.
Save yongrenjie/d65226a43790edb804a9686bcc23f07d to your computer and use it in GitHub Desktop.
Colour stuff online!!! With other people!!!

Context

https://magma.com provides a web interface for drawing and editing images and it seems to be collaborative, i.e. you can share a link with someone to work together on an image!

However, it can be kind of painful to do conventional 'drawing' by dragging a mouse. I think it might be more enjoyable to do a paint-by-numbers thing where you fill in white areas by clicking with a paint bucket tool. You can find a huge variety of black/white line drawings at https://www.justcolor.net/.

So we have the images and the colouring platform. What could go wrong? Well, most online images, including those in the link above, tend to have grey pixels between the black and white bits. This is manifested as the paint bucket 'not filling in the whitespace entirely', like this:

not quite filled in

Solution

To solve this problem, we can use ... software!

  1. HOMEBREW_NO_AUTO_UPDATE=1 brew install imagemagick (you're supposed to be having fun! Homebrew updates aren't fun.)

  2. pip install Pillow (create venv first, etc.)

  3. Save this script somewhere:

from PIL import Image
import os
import sys

def convert_to_black_and_white(input_image_path, threshold=160):
    # Open an image file
    with Image.open(input_image_path) as img:
        # Convert image to grayscale
        grayscale_with_alpha = img.convert("L")
        
        # Create a new image to store the binary result with alpha channel
        binary = Image.new("LA", grayscale_with_alpha.size)
        
        # Process each pixel
        width, height = grayscale_with_alpha.size
        for x in range(width):
            for y in range(height):
                # Get the pixel value (luminance and alpha)
                luminance = grayscale_with_alpha.getpixel((x, y))
                
                # Convert pixel to black or white and adjust alpha
                if luminance < threshold:
                    binary.putpixel((x, y), (0, 255))
                else:
                    binary.putpixel((x, y), (255, 255))
        
        # Save the new image
        output_image_path = input_image_path.replace('.jpg', '_INT.png')
        binary.save(output_image_path)

        # Convert to psd
        os.system(f'convert {output_image_path} {".".join(input_image_path.split(".")[:-1])}.psd')
        # Delete intermediate
        os.system(f'rm {output_image_path}')

convert_to_black_and_white(sys.argv[1])
  1. Choose and download an image from https://www.justcolor.net/. NOTE: use right-click > 'save as image' in the browser instead of clicking the download button, as that will give you a PDF instead of a JPG.

  2. Run python that_script.py your_image.jpg which will produce a PSD file in the same directory as the original image.

  3. Import the PSD in https://magma.com.

  4. The imported PSD will initially have a locked layer. At the bottom-right of the screen, select 'L1' and press the lock button to unlock it.

    lock button

  5. Use the paint bucket tool (or any other tool) on the left and start filling them in. If you accidentally paint the outline, just undo it :)

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