Skip to main content

Concatenates a list of pictures (numpy arrays) without allocating new memory

Project description

Concatenates a list of pictures (numpy arrays) without allocating new memory

pip install fastimgconcat

Tested against Windows 10 / Python 3.10 / Anaconda

from fastimgconcat import temparrays,fastconcat_horizontal,fastconcat_vertical
import numpy as np
import random
import cv2

# This code demonstrates the usage of the fastconcat_vertical()
# function to concatenate pictures (numpy arrays)
# vertically in a
# faster and more efficient way. 
# The fastconcat_vertical() function takes a list of numpy arrays and concatenates
# them vertically without allocating new memory. It is recommended to use this 
# function when you are #concatenating several pictures multiple times, and 
# the shape of the output array never (or rarely) # changes, e.g. streaming
# screenshots of 2 monitors.

# Initialize the height, width and RGB values for the numpy arrays.
height = 200
width = 500
rgbValues0 = np.zeros((height, width, 3), dtype=np.uint8)
rgbValues0[:] = [255, 0, 0]
rgbValues1 = np.zeros((height, width, 3), dtype=np.uint8)
rgbValues1[:] = [0, 0, 255]

# In the for loop, the fastconcat_vertical() 
# function is used to concatenate the numpy arrays vertically.
# If you want to concatenate them horizontally, 
# use fastconcat_horizontal() instead.

checkarraysize = True
for r in range(1000):
    fastconcat_vertical(
        [
            random.choice([rgbValues0, rgbValues1]),
            random.choice([rgbValues0, rgbValues1]),
        ],
        checkarraysize=checkarraysize,
    )
    checkarraysize = False  # If you check the array size each time, it is about 10% slower.

    # The values in temparrays.vertical / temparrays.horizontal 
    # will be changed the next iteration. Therefore,
    # it is recommended to process the output data right
    # after each iteration. If you still need the arrays,
    # copy them (e.g. temparrays.horizontal.copy()), but keep in mind that copying is expensive.

    # This code displays the concatenated image in the cv2 window "test".
    # If the 'q' key is pressed, the program breaks out of the loop and closes the window.
    cv2.imshow("test", temparrays.vertical)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
cv2.destroyAllWindows()

Project details


Download files

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

Source Distribution

fastimgconcat-0.10.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

fastimgconcat-0.10-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file fastimgconcat-0.10.tar.gz.

File metadata

  • Download URL: fastimgconcat-0.10.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for fastimgconcat-0.10.tar.gz
Algorithm Hash digest
SHA256 dfba06a8313d97f0f433da5a4d8d927de7bba2cbb187d717f864b6d05a572d12
MD5 285aef919ec3621af3c4b8a32ed44213
BLAKE2b-256 e37ea7a4b730426a404a1dba97921805438c4d00fc7957b90f259b3a9a7ead8b

See more details on using hashes here.

File details

Details for the file fastimgconcat-0.10-py3-none-any.whl.

File metadata

  • Download URL: fastimgconcat-0.10-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for fastimgconcat-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 481b0d4c2b02a922e4ba98965333b226b76e301fdce6c08e70fb06d5a2c34cd3
MD5 1fbd9a931e931f712c604ccf75677799
BLAKE2b-256 5f792393248de40f9ee753602f9f80a49405af74a6fd23c4d0680375d09e2f96

See more details on using hashes here.

Supported by

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