Skip to content

Instantly share code, notes, and snippets.

@initpwn
Last active February 17, 2020 12:32
Show Gist options
  • Save initpwn/a7c99953038454e96e88ed42b19d992d to your computer and use it in GitHub Desktop.
Save initpwn/a7c99953038454e96e88ed42b19d992d to your computer and use it in GitHub Desktop.

Using OpenCV

import cv2
import sys 

image = cv2.imread("pic.png")
#imshow() is the function that displays the image on the screen.
cv2.imshow("OpenCV Image Reading", image) #first arg is the title, second is the image that is already read
cv2.waitKey(0) #like getch() in Turbo C, wait for a key to get pressed by the user

another package, imageio

import imageio
im = imageio.imread('my_image.png')
print(im.shape)            
#op: (342, 548, 3)
#image have attributes like size, dtype, shape, 
#shape give you the demin of the image, returns a tuple of No. of (rows,col,channel);
#if RBG channels will be 3; if grayscale only rows and cols will be returned.

Loading image to numpy array

#here using for in, you can load multiple images.
#glob package is used for traversing the dir paths

from scipy import misc
import glob

for image_path in glob.glob("pic_folder/*.png"):
    image = misc.imread(image_path)
    print image.shape
    print image.dtype

Using pillow lib

from PIL import Image
import numpy as np

im_frame = Image.open('pic.png')
np_frame = np.array(im_frame.getdata())
print(str(np_frame))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment