Yaohong

为了真相不惜被羞辱

How to Read a Image in Python

How to Read a Image in Python

There are three ways to read a image, the codes is showing below.


image_path = "fasterRCNN/faster-rcnn-keras-master/img/street.jpg";

from PIL import Image
import numpy as np
image = Image.open(image_path) # RGB
tmp_image = np.array(image)
print("PIL open shape:",tmp_image.shape, tmp_image)


# load with cv2
import cv2
image = cv2.imread(image_path)  # mode: BGR
print("cv2 imread shape:", image.shape) # (width,height,channel)


# load with matplotlib
import matplotlib.image as mpimg
image = mpimg.imread(image_path) #  mode: RGB
print("matplotlib imread shape:",image.shape, image) # ( height, width,channel(RGB) )

If you use cv2 to load a image , then show it with matplotlib, you should convert the image to RGB mode, this is because cv2 read a image in BGR mode, while matplotlib presents the image in RGB mode;


What do `*args` and `**kwargs` mean in python function?

What do *args and **kwargs mean in python function?

*args means we can pass an arbitrary number of arguments to the function;

Similarly, **kwargs allow we pass many key=value argument to the function;

*args

*args iterable:

def my_sum(*args):
    result = 0
    # Iterating over the Python args tuple
    for x in args:
        result += x
    return result

print(my_sum(1, 2, 3))
# output: 6

The * is a unpacking operator;

def print_three_things(a, b, c):
    print( 'a = {0}, b = {1}, c = {2}'.format(a,b,c))

mylist = ['aardvark', 'baboon', 'cat']
print_three_things(*mylist)
# output: a = aardvark, b = baboon, c = cat

**kwargs example

**kwargs iterable:


SSLCertVerificationError报错

双击执行Applications > Python3.6 下的Install Certificates.command

SSLCertVerificationError报错

Error:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)

Solution: 环境:Mac

Macintosh HD > Applications > Python3.6 (或者其它安装python目录)
然后:双击 Install Certificates.command

Double click Install Certificates.command log:

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
/Applications/Python\ 3.7/Install\ Certificates.command ; exit;
macdeMacBook-Air-5:~ Rhys$ /Applications/Python\ 3.7/Install\ Certificates.command ; exit;
 -- pip install --upgrade certifi
Requirement already up-to-date: certifi in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2020.6.20)
WARNING: You are using pip version 20.1.1; however, version 20.2.4 is available.
You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 -m pip install --upgrade pip' command.
 -- removing any existing file or link
 -- creating symlink to certifi certificate bundle
 -- setting permissions
 -- update complete
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
Deleting expired sessions...68 completed.

[Process completed]

Double click Update Shell Profile.command log:


简单的图像加宽和截取类

支持图像加宽高,或截掉宽高

简单的图像加宽和截取类

源码如下:

class ImageUtils:

	def __init__(self):
		import numpy;
		self.np = numpy;
		pass

	## 
	def resizePadding(self, np_2d_image, target_width,target_height):
		single_img 		= np_2d_image;
		tmp_img_width 	= single_img.shape[1]
		tmp_img_height 	= single_img.shape[0]
		np 				= self.np
		print("resizeFile origin shape :",single_img.shape)
		# 宽度pading添加
		if tmp_img_width < target_width:
			for x in range(tmp_img_width, target_width):
				# tmp_arr    = np.arange(255,255,(len(single_img),1));
				if x%2 == 0:
					single_img = np.insert(single_img, 0, 255,axis=1);
				else:
					single_img = np.insert(single_img, single_img.shape[1], 255,axis=1);
				# print(file_name_prefix, "origin shape :",single_img.shape)
		# 高度pading添加
		if tmp_img_height < target_height:
			for x in range(tmp_img_height, target_height):
				# tmp_arr    = np.arange(255,255,(len(single_img),1));
				if x%2 == 0:
					single_img = np.insert(single_img, 0, 255,axis=0);
				else:
					single_img = np.insert(single_img, single_img.shape[0], 255,axis=0);

		# 宽度截掉
		if tmp_img_width > target_width:
			for x in range(target_width, tmp_img_width):
				if x%2 == 0:
					single_img = np.delete(single_img, 0, axis=1);
				else:
					single_img = np.delete(single_img, single_img.shape[1]-1, axis=1);

		# 高度截掉
		if tmp_img_height > target_height:
			for x in range(target_height, tmp_img_height):
				if x%2 == 0:
					single_img = np.delete(single_img, 0, axis=0);
				else:
					single_img = np.delete(single_img, single_img.shape[0]-1, axis=0);


		print("resizeFile after shape :",single_img.shape)
		return single_img;

调用代码