Giter VIP home page Giter VIP logo

fpng-python's Introduction

Python Bindings for fpng

Implemets Python interface to call fpng functions. fpng is a super fast C++ .PNG writer/reader.

Note that the images written / read are in RGB channel order, in contrast to OpenCV that reads / writes files in BGR format.

Installation

  1. Clone repository git clone --recurse-submodules [email protected]:qrmt/fpng-python.git
  2. Install python setup.py install

Usage

import pyfpng      # Calls fpng::init() on import

# Encode numpy array and write to file:
data = np.zeros((512, 512, 3), dtype=np.uint8)
success: bool = pyfpng.encode_image_to_file('sample.png', data)

# Encode numpy array to memory
success: bool
encoded: Optional[bytes]
success, encoded = pyfpng.encode_image_to_memory(data)

# Decode file to memory
success: int # see fpng.h
data: Optional[NDArray[np.uint8]]
success, data = pyfpng.decode_file_to_memory('sample.png')

# Decode bytes to memory
with open('sample.png', 'rb') as f:
    bytes_data = f.read()

success, data = pyfpng.decode_to_memory(bytes_data)

# Get info about file (e.g. if it can be decoded using pyfpng)
ret, height, width, channels = pyfpng.get_info(bytes_data)
if ret == 0:
    # can decode...
    ...
else:
    # see fpng.h for details on error code
    ...

Performance

In tests, beats cv2.imwrite / cv2.imencode by ~5x, with similar result file size. For cv2.imread/cv2.imdecode, ~10-20% faster.

License

Licensed under Unlicense

fpng-python's People

Contributors

labraoskar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fpng-python's Issues

Error during installation/compilation.

Hi!

I found your python implementation of fpng, and I really want to use it in a project where I have to encode a lot of png images for a video stream. I can only use png images, so i'm stuck with cv2.imencode.

Sadly, I have little to no experience in C++ and I have failed trying to install fpng-python. I installed Visual Studio Build Tools 2019, and clone this repo exactly as said in the Readme, but when running python setup.py install, I get the following error:

error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

[Errno 13] Permission denied: 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\\Lib\\site-packages\\test-easy-install-1792.write-test'

Then, I try running python setup.py install --user, and the installation starts, but I get another error (I have my computer in Spanish):

cl : Command-Line Warning D9002 : ignoring unknown option '-fno-strict-aliasing'
fpng.cpp
C:\Users\pcast\fpng-python\fpng\src\fpng.h(4): fatal error C1083: Cannot open filetype file: 'stdlib.h': No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.29.30133\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2

I would really appreciate any help,
Pablo

FPNG into Java via JNA: How to provide the Image Data Pointer

Greetings!

I try to wrap FPNG into Java via JNA.
Sorry for asking a maybe stupid question here. How is the Byte Array of the Image const void* pImage provided?
My current understanding is:

/*
num_chans must be 3 or 4. There must be w*3*h or w*4*h bytes pointed to by pImage.
The image row pitch is always w*3 or w*4 bytes.
There is no automatic determination if the image actually uses an alpha channel, so if you call it with 4 you will always get a 32bpp .PNG file.
*/

// read the Demo PNG into an Image
BufferedImage image = ImageIO.read(inputStream);

// Create the w*4*h byte array
final byte[] byteArr = new byte[image.getWidth()*4*image.getHeight()];
for (int y=0; y<image.getHeight(); y++) {
    for (int x=0; x<image.getWidth(); x++) {
        int argb = image.getRGB(x, y);

        final int alpha = 0xff & (argb >> 24);
        final int red = 0xff & (argb >> 16);
        final int green = 0xff & (argb >> 8);
        final int blue = 0xff & (argb >> 0);

        byteArr[x * 4 * y]= (byte) red;
        byteArr[x * 4 * y + 1]= (byte) green;
        byteArr[x * 4 * y + 2]= (byte) blue;
        byteArr[x * 4 * y + 3]= (byte) alpha;
    }
}

// Convert byte array to Pointer
Pointer pImage = new Memory(byteArr.length);
pImage.write(0, byteArr, 0, byteArr.length);

But it always crashes the Java VM when calling the Native Library.

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.