Skip to main content

Python image program

Project description

Installation and importation

pip install python-imager

import pyimager

Setup

For now, I still have to resolve an issue to install a ttf with the rest of the package. So if you want to use image.text(), you'll have to figure out where the package is located on your computer. Then, you create a folder named fonts and you drop default.ttf there.

Otherwise, you can still import a ttf using a path to your file.

Pyimager

Pyimager is a python package for creating, editing, showing and saving images with python.

As it is based on opencv-python but not all of these functions are implemented here, you can still use their functions with this library to get the best result of them. But still, open an issue on GitHub if you would like me to implement functions you need.

I would be so happy if you request or propose any modification of this package :-)

Images

Creating an image

To create an image you just need to proceed as following:

img = image()

You can modify the parameters of this image specifying it's size, background color and name via the following function:

img = new_img(dimensions=[200, 500], background=COL.cyan, name="myImage")
Definition for an image of 200x500 pixels with a cyan background color with "myImage" as name.

You can also import a local image:

img = image(name="myLocalImage").open_img(path)

Showing an image (until gets a keypress)

To show an image just until you get a keypress:

img.show_(0)

To add a timeout, define t as miliseconds before closing:

img.show_(t)

If you want the image to be shown forever: set t to 0.

To get the key that has been pressed:

wk = img.show_(t)

I use wk for WaitKey, as it's the cv2 function called by this function.

Showing an image

To show an image, you should build it first, even if you could use image.show_() to show the images without 'building' them, this way is cleaner than the previos one. So, to properly show an image, proceed as following:

img = new_img()
img.build()
while img.is_opened():
    img.show()

You can use img = image().build() too if you prefer as image.build() returns itself as an image.
Neverthmore I recommend to build it later if you want to modify it before showing it.

Closing an image

Though you can close the window with key esc, you can also close it with image.close().

Even if it may not seem useful right now, you can combine it to a button's function to create a close button. cf. Buttons.

Editing an image

To edit an image, we'll stick with the empty basic image:

img = image() or img = new_img()

Then, you can modify your image using inner functions of the image class.

You have 5 inner functions to draw shapes:

  1. image.line(p1, p2, colour, thickness, lineType)
  2. image.rectangle(p1, p2, colour, thickness, lineType)
  3. image.circle(ct, radius, colour, thickness, lineType)
  4. image.ellipse(cr, (radius1, radius2), colour, thickness, lineType, startAngle, endAngle, angle)
  5. image.polygon(pts:[pt], colour, thickness, lineType)

And you have 3 inner functions more to write text:

  1. image.text(text, ct, col, thickness, fontsize, ...)
  2. image.write(text, pt, colour, thickness, fontSize, font, lineType)
  3. image.write_centered(text, ct, colour, thickness, fontSize, font, lineType)

To draw a diagonal line accross the image, you can do as follows:

img.line(p1=[0, 0], p2=img.size(), colour=COL.red, thickness=5, lineType=2)

You now have a line going from p1 (0, 0) to p2 (bottom right), of red colour, thickness of 5px and the lineType is setted to 2 (there's 0, 1 and 2).

To use colors, use COL.
You can either chose a color in col defined as a CSS color name (camelBack written) or using hexadecimal RGB with COL.new("#xxxxxx") to use a custom color.

Coordinates

The top-left of the image is at [0, 0] (XY coordinates).

Define a point

Define a var of type list or tuple of len 2. Values may be int or float.

Function with points

There are some useful functions to manipulate coordinates:

  1. ct_sg(p1, p2)
    Standing for center of segment from p1 to p2, returns the point at the center of the segment.
  2. pt_sg(p1, p2, m1, m2)
    Standing for point in segment p1, p2, does as ct_sg() but you can define m1 and m2 to increase the weight of the sides to approach to an end.
  3. ct_cr(p1, p2, p3, p4)
    Stands for centre carré, returns the center of a parallelogram.
  4. coosCircle(ct, radius, angle)
    Gets the coordinates as if you were using a compass, from the ct to a distance of radius to an angle.
  5. coosEllipse(ct, radiuses, angle, rotation)
    Similar to coosCircle(), works for elipses.
  6. dist(p1, p2)
    Returns the distance of two points in float.

Buttons

To add buttons to your images, you can use the sub-class image.button_ using image.button().

Defining a button:

button = image.button(name, coos)
button.on_click(funct, params)

Pass a function to the button to execute when clicked on.

Use params if you have variables to give to your funct.

Removing buttons

Use image.remove_button() button = image.button(name, coos)

image.remove_button(button)

You will have to rewrite the image if you want the button to disapear.
To do so, I would recomend you to copy the image with image.copy() before creating your button and restoring the copy of the image after deleting the button.

Trackbars

To add trackbars to your images, you can use the sub-class image.trackbar_ using image.trackbar().

Defining a trackbar:

trackbar = image.trackbar(name, coos, min, max, val)

Removing trackbars

Use image.remove_trackbar()

trackbar = image.trackbar(name, coos, min, max, val)
image.remove_trackbar(trackbar)

You will have to rewrite the image if you want the trackbar to disapear.
For more info, cf. Removing buttons

Access mouse info

Whenever the mouse is on your python window, you can get info about the mouse using image.mouse.get().

event, x, y, flags = image.mouse.get()

Events are cv2 defined events, cf. cv2 events.
x and y are the position of the mouse on the window.
Flags are cv2 defined events, cf. cv2 flags.

Layouts

Layouts are deprecated it's been a while since i've taken a glance on them.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_imager-1.4.11.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_imager-1.4.11-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file python_imager-1.4.11.tar.gz.

File metadata

  • Download URL: python_imager-1.4.11.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.7

File hashes

Hashes for python_imager-1.4.11.tar.gz
Algorithm Hash digest
SHA256 1f591392c4bb22d989489ff087e10df41e6a30fbb94f5f6a777ec46eee7af233
MD5 22e7f00f6299aea7e8b1abd47c6169e1
BLAKE2b-256 4c756637b3371e1d45bdab6d26252f8b227d84718856237b229ecc08ea3247dd

See more details on using hashes here.

File details

Details for the file python_imager-1.4.11-py3-none-any.whl.

File metadata

  • Download URL: python_imager-1.4.11-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.7

File hashes

Hashes for python_imager-1.4.11-py3-none-any.whl
Algorithm Hash digest
SHA256 2f6a0da10f87b680ae21b6b4a26273436088708f14f9eb9e535c87fb4e1b43bf
MD5 d0d0c9f1500e5a2c3c165d3460a3f894
BLAKE2b-256 f5b19e846c310225d6587bfb226b0311ccb681366b37c67bbc51fc690bcc2d63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page