Create polka dot wallpaper with Python Image Library

It's ok. Make a polka dot wallpaper using Python and PIL (Python Image Library) You need to install PIL and Python in advance. Refer to the bottom for installation.

How to use

It takes two arguments. The format of the two arguments is the same, which is the HLS format color specification separated by :. Note that the hsl notation supported by CSS3 and the order of the arguments are different. 360:100:100 The number before the first: is Hue (hue). It takes a value from 0 to 360. The next number is Luminance. It takes a value from 0 to 100. The last number is Saturation. It takes a value from 0 to 100. By specifying two of these values, you can specify the color of the polka dot circle. wallpaper.py 84:84:84 164:98:84

Since antialiasing can be applied only when the image is reduced in Python Image Library, the image size is fixed at 100 * 100 and displayed in your image viewer. Display it with IrfanView etc., reduce it, and save it in the format of your choice (I did this because IrfanView has more and more powerful image reduction algorithms).

Source code

The code to convert to the color code format that can be used from RGB to PIL is the one on the following site.

wallpaper.py


# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageDraw
import colorsys
import sys

### http://code.activestate.com/recipes/266466-html-colors-tofrom-rgb-tuples/history/2/
def RGBToHTMLColor(rgb_tuple):
    """ convert an (R, G, B) tuple to #RRGGBB """
    hexcolor = '#%02x%02x%02x' % rgb_tuple
    # that's it! '%02x' means zero-padded, 2-digit hex values
    return hexcolor

def HTMLColorToRGB(colorstring):
    """ convert #RRGGBB to an (R, G, B) tuple """
    colorstring = colorstring.strip()
    if colorstring[0] == '#': colorstring = colorstring[1:]
    if len(colorstring) != 6:
        raise(ValueError, "input #%s is not in #RRGGBB format" % colorstring)
    r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
    r, g, b = [int(n, 16) for n in (r, g, b)]
    return (r, g, b)

def HTMLColorToPILColor(colorstring):
    """ converts #RRGGBB to PIL-compatible integers"""
    colorstring = colorstring.strip()
    while colorstring[0] == '#': colorstring = colorstring[1:]
    # get bytes in reverse order to deal with PIL quirk
    colorstring = colorstring[-2:] + colorstring[2:4] + colorstring[:2]
    # finally, make it numeric
    color = int(colorstring, 16)
    return color

def PILColorToRGB(pil_color):
    """ convert a PIL-compatible integer into an (r, g, b) tuple """
    hexstr = '%06x' % pil_color
    # reverse byte order
    r, g, b = hexstr[4:], hexstr[2:4], hexstr[:2]
    r, g, b = [int(n, 16) for n in (r, g, b)]
    return (r, g, b)

def PILColorToHTMLColor(pil_integer):
    return RGBToHTMLColor(PILColorToRGB(pil_integer))

def RGBToPILColor(rgb_tuple):
    return HTMLColorToPILColor(RGBToHTMLColor(rgb_tuple))
  
args = sys.argv

if(len(args) != 3):
  print("Args Error");
  print("Usage %s [BaseHLS] [SubHLS]" % args[0])
  quit()

colors = []
#Parsing arguments
for arg in args[1:]:
  a = arg.split(":")
  rgb1 = colorsys.hls_to_rgb(int(a[0]) / 360, int(a[1]) / 100, int(a[2]) / 100)
  color = RGBToPILColor((rgb1[0] * 255, rgb1[1] * 255, rgb1[2] * 255))
  print("added color:", color)
  colors += [color]

img = Image.new("RGB", (100, 100), "white")
hw = img.size[0] / 2
hh = img.size[1] / 2
draw = ImageDraw.Draw(img)
for i, c in zip(range(4), colors + colors[::-1]):
  im = divmod(i, 2) 
  r = im[0] * hw
  l = im[1] * hh
  print("draw point", l, "x", r)
  draw.ellipse((l, r, l + hw, r + hh), fill=c)
img.show()

Recommended Posts

Create polka dot wallpaper with Python Image Library
Create a dummy image with Python + PIL.
Use cryptography library cryptography with Docker Python image
Create an image with characters in python (Japanese)
Image processing with Python
Create an image file using PIL (Python Imaging Library).
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Image processing with Python (Part 2)
Image editing with python OpenCV
Create 3d gif with python3
Sorting image files with Python (2)
Sorting image files with Python (3)
Create Image Viewer with Tkinter
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Create a directory with python
[Python] Image processing with scikit-image
Cut out an image with python
[Python] Using OpenCV with Python (Image Filtering)
Create Awaitable with Python / C API
[Python] Using OpenCV with Python (Image transformation)
Create folders from '01' to '12' with python
Create a virtual environment with Python!
Create an Excel file with Python3
Image processing with Python 100 knocks # 3 Binarization
Let's do image scraping with Python
[Hyperledger Iroha] Query with Python library
Find image similarity with Python + OpenCV
Image processing with Python 100 knocks # 2 Grayscale
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
Image processing with Python 100 knock # 10 median filter
Create a Python function decorator with Class
Create wordcloud from your tweet with python3
HTML email with image to send with python
Build a blockchain with Python ① Create a class
Create an image processing viewer with PySimpleGUI
Image processing with Python 100 knocks # 8 Max pooling
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Image processing with Python & OpenCV [Tone Curve]
Create Python + uWSGI + Nginx environment with Docker
Create and decrypt Caesar cipher with python
Image processing with Python 100 knock # 12 motion filter
Image acquisition from camera with Python + OpenCV
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create Excel file with Python + similarity matrix
Create a word frequency counter with Python 3.4
Try HTML scraping with a Python library
[Python] Quickly create an API with Flask
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Publish your own Python library with Homebrew
[Golang] Create docker image with Github Actions
Create an English word app with python
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV