Code Monkey home page Code Monkey logo

cookiesncream's Introduction

Skrivener

Overview

An example generated image: 'Traditionally, art has been for the select few. We have been brainwashed to believe that Michaelangelo had to pat you on the head at birth. Well, we show people that anybody can paint a picture that they're proud of -- Bob Ross'

Skrivener is a service that accepts text and typesets it into an image. This service communicates over ZeroMQ protocol using JSON formatted messages.

Features:

  • Accepts up to 250 characters of UTF-8 text
  • PNG format
  • Pleasant default color pairings
  • Unsurprising interfaces
    • JSON messages
    • Base64 encoding

Installation

  1. Clone this repository
  2. Enter or initialize a Python virtual environment*
  3. Install this repo with $ pip install .
  4. Run this project
    • either execute $ skrivener (this command is placed in your $PATH by PIP)
    • or execute $ python -m skrivener

Skrivener reads some environment variables on initialization. The provided defaults are:

  • host=*
  • port=8672
  • loglevel=info

You can change these in the .env.example file provided, make your own .env, or set these environment variables another way.

* for more on this see the Python Packaging Authority's introduction

Communication Contract

Request

The accepted parameters for generating an image are:

  • text:
    • required: Yes
    • type: string
    • constraints: 1-250 characters
  • colors:
    • required: No
    • type: object
    • constraints
      • both keys are required
      • value formats must be valid hex color codes
        • optional "#" prefix, 3 or 6 digits, characters [0-9, a-f, A-F] only
    • keys:
      • "bg": string
      • "text": string

Examples

{
  "text": "Traditionally, art has been for the select few. We have been brainwashed to believe that Michaelangelo had to pat you on the head at birth. Well, we show people that anybody can paint a picture that they're proud of -- Bob Ross"
}

or, specifying the colors:

{
  "text": "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.",
  "colors": {
    "text": "#fff",
    "bg": "#000"
  }
}

Response

All Skrivener responses have one common key:

  • status
    • required: Yes
    • type: string

Ok

  • status
    • required: Yes
    • type: literal string ok
  • image:
    • required: Yes
    • type: string
    • info
      • this is a base64 encoded PNG file (for instructions on usage, see below)
Example
{
    "status": "ok",
    "image":  "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIA (...)"
}

Error

  • status
    • required: Yes
    • type: literal string error
  • message
    • required: Yes
    • type: string
    • info
      • provides context for what went wrong
Example

In this example, we sent one malformed color (#0fg).

{
    "status": "error",
    "message": "unknown color specifier: '#0fg'"
}

Because Skrivener uses Pydantic for data validation, any formatting or validation errors should be human readable.

How To

Using the encoded image response

To decode the Base64 string in Python, use (the base64 module)[https://docs.python.org/3/library/base64.html]:

import base64

image_encoded = "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIA (...)"
image_bytes: bytes = base64.b64decode(image_encoded)

Then, you may write that image out to disk using open

with open("/your/file/here.png", "wb") as file:
    file.write(image_bytes)

or read using Pillow

bytes_buffer = io.BytesIO(image_bytes)
image = im.open(bytes_buffer)
# do anything with image!
image.show() # will open in your system viewer

Also see the test client script in the repo at (/tests/client.py)[/tests/client.py#L52-L58]

Attributions

Skrivener uses

  • Pillow for image manipulation
  • Pydantic for data validation
  • pyzmq for ZeroMQ communication
  • Poetsen One font, designed by Rodrigo Fuenzalida, Pablo Impallari

cookiesncream's People

Contributors

bcliden avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.