Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Thursday, July 14, 2022

Image Processing: How to read image from string in python ?

 

Reading image from string base64


🚩 In this tutorial we are going to investigate together how to read image string base64 in python language.

To be able to do processing on image in python some of modules should be used. We are going to use PIL to demonstrate image at this point.

Let's code it !

To get an image as string we need to convert base64 format firstly. Python base64 module is also used for this process.

Let's start with importing the required modules.

👉

   import base64
   import io
   from PIL import Image

That's it !
So, the first we are going to convert image to base64 using python. Let's do it !

👉

   def read_string():
      with open("tux.jpg", "rb") as image:
          image_string = base64.b64encode(image.read())

      return image_string

I have used a local image "tux.jpg" so, you can use anything that has true image format. Let me clarify the codes above.

Step 1: I have defined function read_string() and opened the image in rb mode. The variable image_string that inside of the function holds base64 string.

👉

   if __name__ == "__main__":
       base64_string = read_string()
       print(base64_string)

When I call the function in the main method we probably are going to get an output like this:

   b'/9j/4AAQSkZJRgABAQAAAQABAAD/7QBsUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAE8cAVoAAxslRxwCAAACAAAcAnQAO8KpIFNvZmlhWW91c2hpIC0gaHR0cDovL3d3dy5yZWRidWJibGUuY29tL3Blb3BsZS9zb2ZpYXlvdXNoAP/bAEMAAwICAwICAwMDAwQDAwQFCAUFBAQFCgcHBggMCgwMCwoLCw0OEhANDhEOCwsQ

....

/5W09f+V+gfFfqHxX6h8V+ofFfqHxX6h8V+ofFfvHxX7x8V+8fFfvHxU6Y9FblW4A0P8Aj2//2Q=='

It's too long as you see but there is no problem. Not too big data to process on programming language !

Let's decode the image base64 string vice versa.

👉

   def decode_base64():
       base64_string = read_string()
       decoded_string = io.BytesIO(base64.b64decode(base64_string))
       img = Image.open(decoded_string)
       return img.show()

I have used the other function inside this function to get image string and the other function returns image string as you know. Anyways so base64_string variable holds image string in base64 format and decoded. The last I have used Image function from PIL to show image.

👉

   if __name__ == "__main__":
       decode_base64()

When I call the function decode_base64() the image will be open.

Output:
tux.jpg

The entire code is shared below:
👉

import base64
import io
from PIL import Image


def read_string():
    with open("tux.jpg", "rb") as image:
        image_string = base64.b64encode(image.read())

    return image_string

def decode_base64():

    base64_string = read_string()
    decoded_string = io.BytesIO(base64.b64decode(base64_string))
    img = Image.open(decoded_string)
    return img.show()



if __name__ == "__main__":
    base64_string = read_string()
    print(base64_string)

    decode_base64()

REFERENCE: https://dev.to/bl4ckst0n3/image-processing-how-to-read-image-from-string-in-python-pf8

Wednesday, June 1, 2022

Ways to convert Python 2 code to Python 3

 There. They are few packages developed over time, one of them is official and extremely powerful.

Three Library that is used to convert your Python 2 code to Python 3 code.

  1. 2to3 (Offical, recommended)
  2. Python Future + Futurize
  3. Six + Modernize

Now at this point, you definitely thinking well, what’s the difference between, 2to3, Python Future + Futurize, and Six + Modernize.

Here’s your answer!


What’s the difference between


Automated tools for PY2 to PY3 conversion/compatibility?

2to3, Python Future + Futurize, and Six + Modernize


How to convert Python 2 to Python 3


This can be very easy if you use any of the above libraries (do not forget to make sure the compatibility)
There are two approaches for converting Python 2 to python 3.


Method #1: Convert Python 2 to python 3 using web-tool.


There is a simple tool for converting python 2 to python 3.

you can use a web application to convert python 2 to 3.

This tool is:

  1. 100% FREE
  2. Compatible with All versions of Python
  3. Actively maintained


Method #2: Convert Python 2 to python 3 using a python library.


We can convert Python2 scripts to Python3 scripts by using the 2to3 module. It changes Python2 syntax to Python3 syntax. We can change all the files in a particular folder from python2 to python3.

Step 1: Open your terminal and navigate to your File


Open your terminal or CMD depend on whatever you are using Windows or Linux.

Navigate to the directory where your python file(s) is/are located. (of course, using the “cd” command)


Step 2: Install the library

As usable, use pip to install 2to3, use can use any other library as well, I like 2to3

Command: pip install 2to3

Step 3: Convert the files

Excute: 2to3 [file or folder] -w