Coder Social home page Coder Social logo

ternaus / base64toimageconverters Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 0.0 25 KB

Library for converting from RGB / GrayScale image to base64 and back.

License: Apache License 2.0

Python 100.00%
base64-decoding base64-encoding base64-image base64image image-processing image2base64 jpeg python

base64toimageconverters's Introduction

Library for converting RGB / Grayscale numpy images from to base64 and back.

Installation

pip install -U image_to_base_64

Conversion

Takes numpy.array or PIL.Image as input

RGB to base 64

base64 = rgb2base64(rgb_image, image_format)

where image format is JPEG, PNG

Grayscale to base 64

base64 = grayscale2base64(grayscale_image)

Base64 to RGB image

output_type = "cv2" # or "PIL"
rgb_image = base64_to_rgb(base64, output_type)

Base64 to Grayscale image

output_type = "cv2" # or "PIL"
grayscale_image = base64_to_grayscale(base64, output_type)

Issues

For some reason I cannot convert RGB image to JPEG representation in base 64 and back without losses. => test only for PNG and not JPEG

base64toimageconverters's People

Contributors

ternaus avatar

Stargazers

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

Watchers

 avatar  avatar

base64toimageconverters's Issues

About the JPEG encoding, decoding

Referring to the issue in the README, saving the image as JPEG will inevitably apply some level of compression. So decoding the image to retrieve the exact pixel intensities of the original image may not be possible unless using lossless formats like PNG

However, I found a little parameter of PIL save subsampling while doing the JPEG compression which tries to maintain the pixel intensity, still incurring some loss but not as big as without this parameter.

Given codebase without subsampling

Original implementation: https://github.com/ternaus/base64ToImageConverters/blob/main/image2base64/converters.py
Line number: 12
format: 'JPEG', quality=100

im.save(buffered, format=image_format, quality=quality)

RGB Image:
download

Converted Image:
download

Mean difference of the assertion test: 125.74196689386562

With Subsampling parameter set to 0

im.save(buffered, format=image_format, quality=quality, subsampling=0)

RGB Image:
download

Converted Image:
download

Mean difference of the assertion test: 53.83414475819539

The visual as well as numerical difference in clear. JPEG will definitely lead to losses. But subsampling=0 can preserve more details.

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.