Giter Club home page Giter Club logo

bpimage's Introduction

bpimage

bpimage is a simple image editing library which can be used from the command line. Basic image transformations, color editing, and convolution filters are implemented.

Created mainly so I could explore numpy, image processing, cli development, and extending python with c.

Commands

Dependencies

  • python (>= 3.10)
  • numpy (>= 1.22)
  • Pillow (>= 9.1.1)
    • used only for io tasks such as loading, saving and previewing images

Installation

From Source

bpimage depends on functions written in multiple c files. These files must be compiled into a shared library so they can be invoked from python.

Prerequisites

You will need:

  • A c compiler, such as gcc.

Linux

# checkout project if you have not already.
git clone https://github.com/beakerandjake/bpimage.git
# change your pwd to the root of the project.
cd bpimage
# compile all source c files into a shared library.
gcc -fPIC -shared -O3 bpimage/*.c -o bpimage.so

This will create a bpimage.so file. This file will be loaded in python and used by the library to perform image manipulation.

CLI Usage

First ensure that you have compiled the c files and that your pwd is the root of the project.

cd bpimage

You can print help by running the command without arguments, or with the --help option.

python3 bpimage/main.py --help

The first argument expected is the source image file. Then you can specify zero one or more edits to apply to the image. Finally you need to specify an output. The output could either be saved to a new file or previewed.

Here is an example which rotates an image 90 degrees, inverts the colors, then saves it to a new file.

python3 bpimage/main.py ~/Pictures/example.png --rotate90 --invert -d ~/Pictures/output.png

Commands

preview (-p)

Creates a temporary file and opens the image with the systems default image viewer. Useful for testing commands and immediately seeing the result. Cannot be used with dest (-d).

python3 bpimage/main.py ~/Pictures/example.png --invert -p

dest (-d)

Saves the image to the specified location. Cannot be used with preview (-p).

python3 bpimage/main.py ~/Pictures/example.png -d ~/Pictures/output.png

boxblur

Blurs each pixel by averaging all surrounding pixels extending radius pixels in each direction.

Arguments:

  • radius (int): Number of pixels to take in each direction.
python3 bpimage/main.py ~/Pictures/example.png --boxblur 2 -d ~/Pictures/output.png

boat-sm boat-boxblur

brightness

Modifies the brightness of the image.

Arguments:

  • strength (float): The amount to brighten or darken the image. A value of 0.0 will result in a black image, 1.0 gives the original image.
python3 bpimage/main.py ~/Pictures/example.png --brightness 1.5 -d ~/Pictures/output.png

baboon-sm baboon-brightness

contrast

Modifies the contrast of the image.

Arguments:

  • strength (float): The amount to brighten or darken the image. A value of 0.0 will result in a gray image, 1.0 gives the original image.
python3 bpimage/main.py ~/Pictures/example.png --contrast 1.8 -d ~/Pictures/output.png

baboon-sm baboon-contrast

emboss

Applies an emboss effect to the image.

Arguments:

  • direction (str): One of the following supported values.
    • 'u' Emboss from top to bottom
    • 'd' Emboss from bottom to top
    • 'l' Emboss from left to right
    • 'r' Emboss from right to left
  • strength (float): The number of surrounding pixels to take in each direction.
python3 bpimage/main.py ~/Pictures/example.png --emboss r 1 -d ~/Pictures/output.png

boat-sm boat-emboss

fliph

Flips the image across the horizontal, from bottom to top.

python3 bpimage/main.py ~/Pictures/example.png --fliph -d ~/Pictures/output.png

boat-sm boat-sm-fliph

flipv

Flips the image across the vertical, from left to right.

python3 bpimage/main.py ~/Pictures/example.png --flipv -d ~/Pictures/output.png

boat-sm boat-sm-flipv

gaussian

Applies a gaussian blur to the image.

Arguments:

  • radius (int): The number of pixels to take in each direction.
  • sig (float): The sigma of the gaussian function. Higher values result in more blurring.
python3 bpimage/main.py ~/Pictures/example.png --gaussian 2 6.0 -d ~/Pictures/output.png

boat-sm boat-gaussian

invert

Create a negative of the image.

python3 bpimage/main.py ~/Pictures/example.png --invert -d ~/Pictures/output.png

baboon-sm baboon-invert

motionblur

Applies motion blur to the image.

python3 bpimage/main.py ~/Pictures/example.png --motionblur -d ~/Pictures/output.png

boat-sm boat-motionblur

outline

Highlights edges of the image.

python3 bpimage/main.py ~/Pictures/example.png --outline -d ~/Pictures/output.png

boat-sm boat-outline

rgb2gray

Converts an RGB image to a grayscale image.

python3 bpimage/main.py ~/Pictures/example.png --rgb2gray -d ~/Pictures/output.png

baboon-sm baboon-gray

rotate

Rotates the image counter-clockwise by a specified angle around the center. Optionally expands the canvas size to hold the rotated image.

Arguments:

  • angle (float): The amount of degrees to rotate the image.
  • expand (boolean): Should the canvas be expanded to hold the rotated image?

Example:

python3 bpimage/main.py ~/Pictures/example.png --rotate 45 true -d ~/Pictures/output.png

boat-sm boat-sm-rotate

rotate90

Rotates the image counter-clockwise 90 degrees around the center n times.

Arguments:

  • times (integer): Number of times to rotate the image.

Example:

python3 bpimage/main.py ~/Pictures/example.png --rotate90 3 -d ~/Pictures/output.png

boat-sm boat-sm-rotate90

saturation

Modify the color saturation of the image.

Arguments:

  • strength (float): The amount to modify the saturation. A value of 0.0 will result in a black and white image, 1.0 gives the original image.
python3 bpimage/main.py ~/Pictures/example.png --saturation 1.8 -d ~/Pictures/output.png

baboon-sm baboon-saturation

scale

Re-sizes the image uniformly based on a scale factor.

Arguments:

  • scale (float): Non-zero positive number multiplied by the width and height of the image to determine the dimensions of the resulting image.
python3 bpimage/main.py ~/Pictures/example.png --scale .5 -d ~/Pictures/output.png

boat-sm boat-sm-scale

sepia

Applies a sepia tone to an RGB image.

python3 bpimage/main.py ~/Pictures/example.png --sepia -d ~/Pictures/output.png

baboon-sm baboon-sepia

sharpen

Modify the color saturation of the image.

Arguments:

  • strength (float): The amount to modify the saturation. A value of 0.0 will result in a black and white image, 1.0 gives the original image.
python3 bpimage/main.py ~/Pictures/example.png --shear .25 0 true -d ~/Pictures/output.png

boat-sm boat-sharpen

shear

Shears the image in the specified dimension(s). Optionally expands the canvas size to hold the rotated image.

Arguments:

  • shear_x (float): The amount to shear the image in the x axis (0.0 does nothing)
  • shear_y (float): The amount to shear the image in the y axis (0.0 does nothing)
  • expand (bool): If true, expands the dimensions of resulting image so it's large enough to hold the entire skewed image.
python3 bpimage/main.py ~/Pictures/example.png --shear .25 0 true -d ~/Pictures/output.png

boat-sm boat-sm-shear

bpimage's People

Contributors

beakerandjake avatar

Watchers

 avatar

bpimage's Issues

Allow flexible dtypes

Right now the c functions are coded to operate on unsigned int, and the python functions generally expect / return dtype=np.uint8. It would be better if there was more flexibility in the supported dtype. The c functions could be coded to used a larger data type.

Keeping everything as uint8 can result in some performance issues due to conversions. Performing multiple operations on a image could result in the image data being copied many more times than necessary.

An example of this can be found in color.py, here the image is first converted to np.float32 in order to support saturation instead of wrapping. It's then converted back to uint8 to stay consistent with what the other functions expect.

  return np.clip(img.astype(np.float32) * strength, 0, 255).astype(np.uint8)

Package / Publish to PyPi

Right now users must manually checkout / compile to use, they cannot install via pip.

Need to get project ready for packaging via setuptools and publish to pip.

Need to find a way to handle the c files.

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.