How convert image to an array in python - python

I want to convert image (.jpg) to binary array. Because I have to use this array to my scrambler operating on it saved in file. Which library and and functions should I use?

You should take a look at the openCV library.
import cv2
img = cv2.imread('image.jpg', flags=cv2.IMREAD_COLOR)

You can use the python library: PIL & numpy. Click here to learn more about image handling in python.
import numpy
import PIL
img = PIL.Image.open("foo.jpg").convert("L")
imgarr = numpy.array(img)

You can use this code to convert your image to array
# Import the necessary libraries
from PIL import Image
from numpy import asarray
# load the image and convert into
# numpy array
img = Image.open('test.jpg')
arraydata = asarray(img)
# data
print(arraydata)

Related

how do i create an image using a matrix with pycharm and python?

I'm trying to create some pixelart using a matrix in pycharm. The problem is that I have never used this program. It's supposed to work just by simply selecting if you're working with the RGB model, but it doesn't.
import cv2
import numpy as np
from matplotlib import pyplot as plt
pixels = ([0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0])
([0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0])
([0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0])
([0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0])
print (pixels[2][4])
cv2.waitKey()
You need to save pixels as a numpy array with type uint8 and then let cv2 display it. If you pass 0 to waitKey the window will stay open until you close it manually.
import cv2
import numpy as np
pixels = np.array([[0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0],[0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0],[0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0]], np.uint8)
cv2.imshow("My Image", pixels)
cv2.waitKey(0)
You can use the Pillow library.
from PIL import Image
import numpy as np
pixels = np.array(pixels).astype(np.uint8) # converts pixels to a numpy array
image = Image.fromarray(pixels)
# now you can save your image using
image.save('newimage.png')

Load svg image in opencv

I'm trying to download an svg image and open it in opencv for further processing. What I am doing is to convert from svg to png format with cairosvg, open it using Pillow and finally convert to opencv. The problem here is that the image converted in opencv does not look exactly the same as the one in PIL format.
from io import BytesIO
import numpy as np
import requests
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
# Get svg data
svg_data = requests.get(svg_url).content
# Convert from svg to png
png = svg2png(bytestring=svg_data)
# Open png in PIL
pil_img = Image.open(BytesIO(png))
pil_img.show() # This looks good
# Convert to opencv
cv_img = np.array(pil_img.convert('RGB'))[:, :, ::-1].copy() # Taken from https://stackoverflow.com/questions/14134892/convert-image-from-pil-to-opencv-format
cv2.imshow('cv_img', cv_img) # This does not look right
cv2.waitKey(0)
These are the resulting images from PIL and opencv format respectively:
The opencv image does not look right, e.g. the text does not have space between characters.
Why is that and how can I fix it?
As suggested, you have to preserve alpha channel.
import numpy as np
import requests
from io import BytesIO
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
svg_data = requests.get(svg_url).content
png = svg2png(bytestring=svg_data)
pil_img = Image.open(BytesIO(png)).convert('RGBA')
pil_img.save('output/pil.png')
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGBA2BGRA)
cv2.imwrite('cv.png', cv_img)

PIL Image can not open transformed image with skimage.io

I have performed a shear and horizona flip transformations on an float array saved in a PFM file, then I have saved them as tif files through this code:
import os
from scipy import ndarray
# image processing library
import skimage as sk
from skimage.transform import warp
from skimage import util
from skimage import io
import numpy as np
def shear(image_array: ndarray, shear=10):
tf_augment = sk.transform.AffineTransform(shear=np.deg2rad(shear))
return warp(image_array, tf_augment, order=1, preserve_range=True,
mode='edge')
def horizontal_flip(image_array: ndarray):
return image_array[:, ::-1]
image_path='./train/0006.pfm'
image_to_transform, scale=readPFM(image_path)
flipped_image1=horizontal_flip(image_to_transform)
io.imsave("flipped_image1.tif", flipped_image1)
sheared_image1 =shear(image_to_transform,5)
io.imsave("sheared_image1.tif", sheared_image1)
However, when I try to open these images with PIL framework, it works with the flipped image but not with sheared one:
from PIL import Image
Image.open("sheared_image1.tif")
I got the error
OSError: cannot identify image file 'sheared_image1.tif'
Could anyone explain to me the reason, or how to save my transformed images through skimage framework to be able to read them with PIL (I have to read them with PIL for latter purposes)

How do i convert an image between PIL and cv2 without needless saving/loading (Python 3.6.3) [duplicate]

I want to convert an image loaded
TestPicture = cv2.imread("flowers.jpg")
I would like to run a PIL filter like on the example with the variable
TestPicture
but I'm unable to convert it back and forth between these types.
Is there a way to do these conversions?
Can OpenCV do all of the image filters that are in the PIL package?
Example:
Result:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold_img = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
im_pil = cv2_to_pil(threshold_img)
pytesseract.image_to_string(im_pil)
Out[5]: 'TUM'
Yes OpenCV is more robust and flexible and can perform most of the image processing routines which are available out there, So probably this filter can be done with OpenCV> However, there may not be a straightforward API for that.
Anyways, as far as the conversion of image format from OpenCV to PIL is concerned you may use Image.fromarray as:
import cv2
import numpy as np
from PIL import Image
img = cv2.imread("path/to/img.png")
# You may need to convert the color.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
# For reversing the operation:
im_np = np.asarray(im_pil)
But you must keep in mind that, OpenCV follows BGR convention and PIL follows RGB color convention, so to keep the things consistent you may need to do use cv2.cvtColor() before conversion.
Pillow and OpenCV use different formats of images. So you can't just read an image in Pillow and manipulate it into an OpenCV image.
Pillow uses the RGB format as #ZdaR highlighted, and OpenCV uses the BGR format. So, you need a converter to convert from one format to another.
To convert from PIL image to OpenCV use:
import cv2
import numpy as np
from PIL import Image
pil_image=Image.open("demo2.jpg") # open image using PIL
# use numpy to convert the pil_image into a numpy array
numpy_image=numpy.array(pil_img)
# convert to a openCV2 image, notice the COLOR_RGB2BGR which means that
# the color is converted from RGB to BGR format
opencv_image=cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
To convert from OpenCV image to PIL image use:
import cv2
import numpy as np
from PIL import Image
opencv_image=cv2.imread("demo2.jpg") # open image using openCV2
# convert from openCV2 to PIL. Notice the COLOR_BGR2RGB which means that
# the color is converted from BGR to RGB
color_converted = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
pil_image=Image.fromarray(color_converted)
Here are two functions to convert image between PIL and OpenCV:
def toImgOpenCV(imgPIL): # Conver imgPIL to imgOpenCV
i = np.array(imgPIL) # After mapping from PIL to numpy : [R,G,B,A]
# numpy Image Channel system: [B,G,R,A]
red = i[:,:,0].copy(); i[:,:,0] = i[:,:,2].copy(); i[:,:,2] = red;
return i;
def toImgPIL(imgOpenCV): return Image.fromarray(cv2.cvtColor(imgOpenCV, cv2.COLOR_BGR2RGB));
Convert from OpenCV img to PIL img will lost transparent channel. While convert PIL img to OpenCV img will able to keep transparent channel, although cv2.imshow not display it but save as png will gave result normally.

How do I create an OpenCV image from the contents of a file?

Usually I would create an image in OpenCV as:
from cv2 import imread
img = imread("/home/nick/myfile.jpg")
But already have the contents of the file in another variable, so how to I create an OpenCV from this directly? e.g.
fc = open("/home/nick/myfile.jpg", "rb").read()
img = something(fc)
What is something? Is there an OpenCV or numpy function to do this?
cv2.imdecode() can do that in memory. and yes, it wants a numpy array as input

Categories