opencv-python function

opencv-python reads, displays and stores images

 


1. Imshow function The function of

imshow function is to display the image in the window. The window automatically fits the image size. We can also adjust the size of the window displaying the image through the imutils module. The official definition of the function is as follows:

cv2.imshow(windows_name, image)

Function parameter 1: Window name (string)
Function parameter 2: Image object, the type is the ndarray type in numpy. Note: The image display size can be changed through the imutils module. The following example shows the

above example:

cv2.imshow('origin image', rgb_img)   #显示原图
cv2.imshow('origin image', imutils.resize(rgb_img, 800))  #利用imutils模块调整显示图像大小
cv2.imshow('gray image', imutils.resize(gray_img, 800))
if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()


2. The imwrite function

imwrite function checks the image and saves it locally, the official definition:

cv2.imwrite(image_filename, image)

Function parameter 1: Saved image name (string)
Function parameter 2: Image object, the type is the ndarray type in numpy

cv2.imwrite('rgb_img.jpg', rgb_img)   #将图像保存成jpg文件
cv2.imwrite('gray_img.png', gray_img) #将图像保存成png文件


3. Window destruction function

When we use the imshow function to display the image, we need to destroy the image display window in the program, otherwise the program will not be terminated normally. The commonly used functions for destroying the window are as follows:
(1), cv2. destroyWindow(windows_name) #Destroy a single specific window
Parameters: The name of the window to be destroyed

(2), cv2.destroyAllWindows() #Destroy all windows, no parameters.

Then we destroy the window properly. We certainly can't destroy the window as soon as the picture window appears, so that we can't watch the window. Imagine there are two ways:
(1) Let the window stay for a period of time and then automatically destroy;
(2) Receive a specified command, such as receiving a specified keyboard stroke and ending the window we want to end. In the
above two cases, the cv2.waitKey function will be used. First, look at the function definition:

cv2.waitKey(time_of_milliseconds)

The only parameter time_of_milliseconds is an integer, which can be positive or negative or zero. The meaning and operation are also different. They correspond to the two situations mentioned above

(1) time_of_milliseconds> 0: At this time, time_of_milliseconds represents the time, and the unit is milliseconds, meaning it means waiting for time_of_milliseconds milliseconds After the image will be automatically destroyed, see the following example

#表示等待10秒后,将销毁所有图像
if cv2.waitKey(10000):
    cv2.destroyAllWindows()

#表示等待10秒,将销毁窗口名称为'origin image'的图像窗口
if cv2.waitKey(10000):
    cv2.destroyWindow('origin image')


(2) time_of_milliseconds <= 0: At this time, the image window will wait for a keyboard stroke, and the window will be destroyed after receiving the specified keyboard stroke. We can customize the keyboard waiting to be typed, which is better explained by the following example

Set waitKey(0), it means that the program will wait for the user's key event indefinitely

#当指定waitKey(0) == 27时当敲击键盘 Esc 时便销毁所有窗口
if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()

#当接收到键盘敲击A时,便销毁名称为'origin image'的图像窗口
if cv2.waitKey(-1) == ord('A'):
    cv2.destroyWindow('origin image')


Here is an example of complete reading, displaying, saving and image destruction:

import cv2
import imutils
import numpy as np

rgb_img = cv2.imread('E:/peking_rw/ocr_project/base_prehandle/img/cartoon.jpg')
gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY)
cv2.imshow('origin image', imutils.resize(rgb_img, 800))
cv2.imshow('gray image', imutils.resize(gray_img, 800))
cv2.imwrite('rgb_img.jpg', rgb_img)
cv2.imwrite('gray_img.png', gray_img)

#等待一定时间自动销毁图像窗口
#if cv2.waitKey(10000):
#    cv2.destroyAllWindows()
#if cv2.waitKey(10000):
#    cv2.destroyWindow('origin image')

#接收特定键盘销毁图像窗口
#if cv2.waitKey(-1) == ord('A'):
#    cv2.destroyWindow('origin image')
if cv2.waitKey(0) == 27:
    cv2.destroyAllWindows()

Add text to pictures 

ImageFont: Used to load the font library file downloaded in the preparation phase.
ImageDraw:  Based on the image object, create an object that can draw lines and paste text on the Image instance.
 

import cv2

from PIL import ImageFont, ImageDraw, Image

import numpy as np

 

bk_img = cv2.imread("background.jpg")

#设置需要显示的字体

fontpath = "font/simsun.ttc"

font = ImageFont.truetype(fontpath, 32) #32为字体大小

img_pil = Image.fromarray(bk_img)

draw = ImageDraw.Draw(img_pil)

#绘制文字信息

draw.text((100, 300),  "Hello World", font = font, fill = (255, 255, 255))

draw.text((100, 350),  "你好", font = font, fill = (255, 255, 255))

bk_img = np.array(img_pil)

 

cv2.imshow("add_text",bk_img)

cv2.waitKey()、
#保存图片

cv2.imwrite("add_text.jpg",bk_img)

 Simple use of pillow-Nanchang Institute of Technology practice project

import time
import random
import threading
import copy
import cv2  # 导入 opencv -python 库
from PIL import Image,ImageFont,ImageDraw
import numpy as np

img1 = cv2.imread('d:\\lib.jpg')
img2 = cv2.imread('d:\\lib1.png')
img2 = cv2.resize(img2, (100, 100))

img=Image.fromarray(img1)
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', 30, encoding="utf-8")
draw.text((500, 30), '....南理文字RPG0.2的世界开始了.....', textColor=(255,0,0), font=fontText)
draw.text((535, 100), 'Hi,欢迎加入南理文字RPG0.2 ', textColor=(255,0,0), font=fontText)
img1=np.array(img)





SCRIPT_NPC_SCHOOL_SISTER = ['你好!',
                                  '你好!',
                                  '你是新生嘛?',
                                  '是的',
                                  '想要我教你魔法吗?',
                                  '\n1、好的 \
                                 \n2、不用了吧,我不和学姐学魔法!\n'
                                  ]
MAGIC_BOOKS = [
    ['风巽千叶翔龙', 1],
    ['泽兑鬼尘珠', 2],
    ['凤翔九天', 3]
]




class Body:  # 人物类
    LEVEL=1  #等级
    EXP=0    #经验
    NAME = ''  # 名字
    HP = 0  # 血量
    MAX_HP = 0  # 最大血量
    MP = 0  # 魔法值
    MAX_MP = 0  # 最大魔法值

    ATTACK = 1
    MAGIC_ATTACK = 1.3
    DEFENSE=5
    HAS_MAGIC = []  # 会的魔法

    EMOTION=0.5 #范围0-1,0为绝望,1为神采奕奕,(0.5为平和,此时为最佳状态)

    POS_NAME = '南昌理工学院南大门'  # 地点

    def __init__(self, IS_HUMAN_= False,NAME_='Ice NPC',LEVEL_=1,EXP_=0, HP_=3, MP_=3, MAX_HP_=3, MAX_MP_=3, ATTACK_=1, MAGIC_ATTACK_=1.3,EMOTION_=0.5, HAS_MAGIC_=[],
                 POS_NAME_='南昌理工学院南大门', DEFENSE_=5,bounce_=0):
        # 初始化 默认值
        self.IS_HUMAN = IS_HUMAN_  # 判断是否位人类
        self.LEVEL=LEVEL_
        self.EXP=EXP_
        self.NAME = NAME_
        self.HP = HP_
        self.MP = MP_
        self.MAX_HP = MAX_HP_
        self.MAX_MP = MAX_MP_
        self.ATTACK = ATTACK_
        self.MAGIC_ATTACK = MAGIC_ATTACK_
        self.DEFENSE=DEFENSE_
        self.EMOTION=EMOTION_
        self.POS_NAME = POS_NAME_
        self.HAS_MAGIC = HAS_MAGIC_
        self.bounce=bounce_





    def display_status(self):  # 显示人物状态
        print('\n\r', '    状态:' + self.NAME, end='')
        print('\n血量:' + '❤' * int(self.HP), '\n  魔法值:' + '⚓' * int(self.MP))
        print('地点:' + self.POS_NAME)

        if self.HAS_MAGIC == []:
            print('无魔法')
        else:
            self.display_magic()


    def display_status_fighting(self):

        print('\n\r', '    状态:' + self.NAME, end='')
        print('\n血量:' + '❤' * int(self.HP), '  魔法值:' + '⚓' * int(self.MP))

    def display_magic(self):  # 显示魔法
        print(self.NAME + '会如下几种魔法:')

        print('魔法名 ,消耗点数')

        i = 0

        for iMagic in self.HAS_MAGIC:
            print('第', i, '个魔法:', MAGIC_BOOKS[iMagic])
            i = i + 1

    def talk_to(self, NPC_, NPC_SCRIPT_, delay=0.3):
        # 按剧本和NPC聊天
        print('在' + self.POS_NAME + '遇到 ' + NPC_.NAME)

        for i in range(len(NPC_SCRIPT_)):
            if i % 2 == 1:  # 判断当前说话的人
                TURN = self.NAME
            else:
                TURN = NPC_.NAME

            print(TURN + ':' + NPC_SCRIPT_[i])
            time.sleep(delay)

    def learn_magic_from(self, NPC_):
        # 向NPC学习魔法

        NPC_.display_magic()
        CHOOSE = input('是否向' + NPC_.NAME + '学魔法?(输入:1/2/3/no)')

        if CHOOSE == '1':

            print('恭喜' + self.NAME + '学会' + MAGIC_BOOKS[NPC_.HAS_MAGIC[0]][0] +
                  ',每次使用消耗' + str(MAGIC_BOOKS[NPC_.HAS_MAGIC[0]][1]) + 'MP.')
            self.HAS_MAGIC = self.HAS_MAGIC + [NPC_.HAS_MAGIC[0]]
            self.display_magic()
        elif CHOOSE == '2':
            print('恭喜' + self.NAME + '学会' + MAGIC_BOOKS[NPC_.HAS_MAGIC[1]][0] +
                  ',每次使用消耗' + str(MAGIC_BOOKS[NPC_.HAS_MAGIC[1]][1]) + 'MP.')
            self.HAS_MAGIC = self.HAS_MAGIC + [NPC_.HAS_MAGIC[1]]
            self.display_magic()
        elif CHOOSE == '3':
            print('恭喜' + self.NAME + '学会' + MAGIC_BOOKS[NPC_.HAS_MAGIC[2]][0] +
                  ',每次使用消耗' + str(MAGIC_BOOKS[NPC_.HAS_MAGIC[2]][1]) + 'MP.')
            self.HAS_MAGIC = self.HAS_MAGIC + [NPC_.HAS_MAGIC[2]]
            self.display_magic()
        elif CHOOSE == 'no':
            print('由于你的固执,没有学习新的魔法.')
        else:
            print('不明白你说什么.')

    def DEFENSE_(self, NPC):
        hit = self.ATTACK * (100 - self.DEFENSE) % 100
        return hit

    def attack_one_time(self, NPC_, iChoose_magic_):  # 攻击一下NPC
        if iChoose_magic_ == -2:  # 休息,且调出自己的全面状态
            self.display_status()
        elif iChoose_magic_ == -1:  # 普通攻击

            NPC_.HP = NPC_.HP - self.ATTACK
            self.HP = NPC_.HP + self.DEFENSE

            self.EXP = self.EXP + MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]
            print('【', self.NAME + '击剑攻击' + NPC_.NAME, '造成了' + str(self.ATTACK), '点攻击】')
            print('***' + self.NAME + '增加了' + str(MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]) + '点经验值***')
            print('【', self.NAME + '消耗了' + str(MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]) + '点魔法】')
            print('***' + self.NAME + '的经验值为' + str(self.EXP) + '***')
        elif iChoose_magic_ in self.HAS_MAGIC:
            # print(iChoose_magic_)
            hit = self.MAGIC_ATTACK * MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]

            # print('hit:',hit,',',self.MAGIC_ATTACK,',',self.HAS_MAGIC[iChoose_magic_])
            NPC_.HP = NPC_.HP - hit  # 魔法伤害=魔法消耗*魔法攻击力
            NPC_.MP = NPC_.MP - self.HAS_MAGIC[iChoose_magic_]
            NPC_.MP = NPC_.MP + self.HAS_MAGIC[iChoose_magic_]  # 实现魔法无消耗
            self.MP = self.MP - MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]
            self.EXP = self.EXP + MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]
            print('【', self.NAME + '用魔法' + MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][0] + ',攻击了' + NPC_.NAME + ',',
                  '造成了' + str(hit) + '点攻击!】')
            print('***' + self.NAME + '增加了' + str(MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]) + '点经验值***')
            print('【', self.NAME + '消耗了' + str(MAGIC_BOOKS[self.HAS_MAGIC[iChoose_magic_]][1]) + '点魔法】')
            print('***' + self.NAME + '的经验值为' + str(self.EXP) + '***')
        if self.EXP >= 2:  # 如果经验值大于等于2  玩家即升一级
            print('***' + self.NAME + '已升级')  # 显示玩家已升级
            self.LEVEL = self.LEVEL + 1
            self.EXP = 0  # 升级后 经验清零
            print('***' + self.NAME + '的等级为' + str(self.LEVEL) + '***')
            # 玩家各属性提升

            self.HP = self.HP + 1
            self.MP = self.MP + 1
            self.MAX_HP = self.MAX_HP + 1
            self.MAX_MP = self.MAX_MP + 1
            self.DEFENSE = self.DEFENSE + 5
            self.ATTACK = self.ATTACK + 1
            self.MAGIC_ATTACK = self.MAGIC_ATTACK + 0.5
            # 输出玩家升级后属性值
            print('***' + self.NAME + '的HP为' + str(self.HP) + '***')
            print('***' + self.NAME + '的MP为' + str(self.MP) + '***')
            print('***' + self.NAME + '的最大生命为' + str(self.MAX_HP) + '***')
            print('***' + self.NAME + '的最大魔法为' + str(self.MAX_MP) + '***')
            print('***' + self.NAME + '的普通攻击力为' + str(self.ATTACK) + '***')
            print('***' + self.NAME + '的魔法攻击为' + str(self.MAGIC_ATTACK) + '***')
            print('***' + self.NAME + '的防御为' + str(self.DEFENSE) + '***')

        if NPC_.HP <= 0:
            return 1
        else:
            return 0




    def fight_with(self, NPC_):
        # 挑战NPC

        print('【', self.NAME + '开始和' + NPC_.NAME + '战斗! !', '】')

        MAX_ROUND = 100
        IS_DEAD = 0

        for ROUND in range(MAX_ROUND):

            if (ROUND % 2 == 0):
                self.display_status_fighting()
                self.display_magic()
                NPC_.display_status_fighting()

                choose_magic = int(input('-----使用魔法/普通攻击(-1普通攻击,0/1/2..魔法攻击,查看状态-2):'))
                IS_DEAD = self.attack_one_time(NPC_, choose_magic)
                IS_DEAD = IS_DEAD * 2  # 用乘法,让我们不改变0,非0状态的情况下,标记是谁赢了
            else:
                choose_magic = random.choice(NPC_.HAS_MAGIC + [-1])

                IS_DEAD = NPC_.attack_one_time(self, choose_magic)
                IS_DEAD = IS_DEAD * 4  # 用乘法,让我们不改变0,非0状态的情况下,标记是谁赢了

            if IS_DEAD >= 1:
                print('【战斗结束】')
                if IS_DEAD == 2:
                    print('【', self.NAME + '战胜了' + NPC_.NAME, '】')
                elif IS_DEAD == 4:
                    print('【', NPC_.NAME + '战胜了' + self.NAME, '】')
                return IS_DEAD

            else:
                if ROUND % 2 == 1:
                    print('【第' + str(1 + int(ROUND / 2)) + '回合结束!】\n===================================')


class World:  # 世界类


    # 剧本:遇到学姐

    # 玩家初始化
    PLAYER = Body()


    # NPC初始化(先创造三个NPC,学姐,冰怪,火怪

    NPCs = {
        'school elder sister': Body(NAME_='school elder sister', HAS_MAGIC_=[0, 1, 2]),
        'ice monster': Body(NAME_='ice monster', HAS_MAGIC_=[2]),
        'fire monster': Body(NAME_='fire monster', HAS_MAGIC_=[0])
    }

        # 初始化世界:

    def init(self):
        player_name = input('请输入玩家名:')

        self.PLAYER.NAME = player_name

        print('....南理文字RPG0.2的世界开始了.....')

        welcome_script = 'Hi,' + self.PLAYER.NAME + ',欢迎加入南理文字RPG0.2 '
        # self.display_a_line(script=welcome_script)
        print(welcome_script)

        self.PLAYER.display_status()

        self.PLAYER.talk_to(self.NPCs['school elder sister'],
                            SCRIPT_NPC_SCHOOL_SISTER)  # 通过NPCs集合,调取'school elder sister'NPC

        self.PLAYER.learn_magic_from(self.NPCs['school elder sister'])

    def run(self):
        self.PLAYER.fight_with(self.NPCs['fire monster'])


    def image_on_image(img1, img2, x=100, y=100):
        # 代表图像像素有多少行多少列多少通道数
        rows, cols, channels = img2.shape  # 获取图像2的shape信息 存入rows,cols,channels
        roi = img1[0:rows, 0:cols]  # 把背景图片img1的左上角按图像2的形状赋值给roi

        img1[0 + y:rows + y, 0 + x:cols + x] = img2

        cv2.imshow('res', img1)
        return img1

        # 当前状态、之前状态

    cur_flag = -1
    pre_flag = -1

    # 无限循环

    x, y = 100, 100  # 主角的坐标位置
    delta = 15  # 主角每次走一步,走的步伐

    while True:
        # 每一帧

        background = img1.copy()  # 背景图像值拷贝
        actor = img2.copy()  # 主角图像值拷贝
        # 获取键盘事件
        flag = cv2.waitKey(0)  # 程序等待用户敲击键盘,然后返回键盘值给flag
        # Esc,退出
        if flag == 27:
            break
        # 判断是否按下其他键
        if flag > -1 and flag != pre_flag:
            cur_flag = flag
        pre_flag = flag

        # 响应 actor移动事件
        if cur_flag == ord('w'):
            y = y - delta

        elif cur_flag == ord('s'):
            y = y + delta
        elif cur_flag == ord('a'):
            x = x - delta
        elif cur_flag == ord('d'):
            x = x + delta
        else:
            1 + 1

        if x < 10:
            x = 0
        if y < 10:
            y = 0
        if x > 1435:
            x = 1435
        if y > 745:
            y = 745
        # 显示
        print('Actor(x,y)=(' + str(x) + ',' + str(y) + ')')
        background = image_on_image(background, actor, x, y)  # 把背景图像,主角图像,x,y坐标传入,返回获取图像叠加的一个图像

    cv2.destroyAllWindows()
    # 显示一句话:两种方式,直接显示0,以及逐步显示1
    def display_a_line(way, script='hello'):
        for i in range(len(script)):
            time.sleep(0.3)
            print('\r', script[0:i], end='')


magic_world = World()

magic_world.init()

magic_world.run()

 

Guess you like

Origin blog.csdn.net/q1246192888/article/details/106125019