Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TurboJPEG Encode Uncorrect! But imencode is correct,but maybe encoder is not work #81

Closed
NakanoSanku opened this issue Jul 25, 2024 · 2 comments

Comments

@NakanoSanku
Copy link

use opencv imencode is correct

    def __buffer2bytes(self):
        # Directly use the pixel buffer and reshape only once
        pixel_array = np.frombuffer(self.pixels, dtype=np.uint8).reshape((self.height.value, self.width.value, 4))
        flipped_rgb_pixel_array = pixel_array[::-1, :, [2, 1, 0]]
        _, data = cv2.imencode(self.encode, flipped_rgb_pixel_array)
        return data.tobytes()

maybe use this code

        String filename = tempfile();
        code = encoder->setDestination(filename);
        CV_Assert( code );

        code = encoder->write(image, params);
        encoder->throwOnEror();
        CV_Assert( code );

        FILE* f = fopen( filename.c_str(), "rb" );
        CV_Assert(f != 0);
        fseek( f, 0, SEEK_END );
        long pos = ftell(f);
        buf.resize((size_t)pos);
        fseek( f, 0, SEEK_SET );
        buf.resize(fread( &buf[0], 1, buf.size(), f ));
        fclose(f);
        remove(filename.c_str());

image
but use pyturbojpeg is uncorrect

    def __buffer2bytes(self):
        # Directly use the pixel buffer and reshape only once
        pixel_array = np.frombuffer(self.pixels, dtype=np.uint8).reshape(
            (self.height.value, self.width.value, 4))
        flipped_rgb_pixel_array = pixel_array[::-1, :, [2, 1, 0]]
        return self.__turboJpegEncoder.encode(
            flipped_rgb_pixel_array, quality=self.quality)

image

@lilohuang
Copy link
Owner

Hi @NakanoSanku ,

The cv2.imencode function calls cv2_convert.cpp to ensure that the non-contiguous input can be converted properly. However, PyTurboJPEG assumes that the input is already a contiguous buffer. You can try calling np.ascontiguousarray(flipped_rgb_pixel_array) before passing it to the PyTurboJPEG encoder. This should resolve the issue you encountered, and this will be natively supported in a future release.

e.g.,

return self.__turboJpegEncoder.encode(
            np.ascontiguousarray(flipped_rgb_pixel_array), quality=self.quality)

I'm going to close this issue. Please let me know if it doesn't work for you.

@NakanoSanku
Copy link
Author

nice!thanks you !

lilohuang added a commit that referenced this issue Jul 28, 2024
…t to the encoder.

Ensuring that the pixel buffer is a contiguous array before sending it to the encoder to resolve the issue reported at #81.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants