Python Project: pillow, tesseract, and opencv_1

Python Project: pillow, tesseract, and opencv

一、Pillow

1. What is pillow?

Pillow is an imagining library of python------Python image Library (PIL)
we can use

help(PIL)
help(PIL.Image)
help(PIL.Image.open)

To get the Manual of them, even to other module.
we can use

dir(PIL)

To get the classes that are interact with PIL, and we can use this method to other module or functions.

If we are using jupyter on browser programming python, we can use a module called Ipython to display the imagine on browser, such as

import PIL
from PIL import Image
import Ipython
filename="........"
a=Image.open(filename)
Image.show()   #programming on your own computer to show the imagine, and if someone use this method, the iamge'll be closed, and it can not be invoke below
ipython.display(a) #programming by jupyter to show the imagine

so, through these code, we can find that the function display in ipython can interact your code to the imagine on browser.

2. The common function in image

2.1 copy

copy------Imagine.copy()
We can use help(image.copy) to find all apis of it

imagine_2=imagine1.copy()

2.2 save()
image.save(filename=para_1,interesting=type_1)

image.save("nsici.gif")

2.3 filter
The function filter has prepared a lot of filters that can be used directly for us to choose, for example:

image_2=image_1.filter(PIL.ImageFilter.BLUR)

2.4 crop
The function crop can segment the image and extract a specified part

image_3=image.crop((2,3,4,5)) 

2.5 ImageDraw
After declare which image we’d like to draw in, we can use function ImageDraw like using module turtle.
such as:

iamge_draw=ImageDraw.Draw(image)
iamge_draw.rectangle((500,0,190,150),fill=None,outline="blue")

2.6 ImageEnhance
There are a lot of function that can deal with image in it, such as

enhanceImage=ImageEnhance.Brightness(image)
images=[]
for i in range(10):
    images.append(enhanceImage.enhance(i/10))
print(images)

2.7 New
Create a new image without anything in it:

from PIL import Image   #The ways to create a new image with a currrnt mode gotten from orignal image
currt_image=PIL.Image.new(images[9].mode,(10*images[0].width,10*images[0].height) )

2.8 paste
Paste is a function that can replace a declared area of a image with a selected image.

cl=0
for i in images:
    currt_image.paste(i,(0,cl))
    cl=cl+500
currt_image.show() 

2.9 Image.merge
Merge n pieces of image into one piece.

new = Image.merge('RGB',(red,green,blue))

2.10 ImageFont
The method that can be used to choose font, such as:

font = ImageFont.truetype('readonly/fanwood-webfont.ttf', 50)

3. Homework

Assignment 1: Building a Better Contact Sheet
In the lectures for this week you were shown how to make a contact sheet for digital photographers, and how you can take one image and create nine different variants based on the brightness of that image. In this assignment you are going to change the colors of the image, creating variations based on a single photo. There are many complex ways to change a photograph using variations, such as changing a black and white image to either “cool” variants, which have light purple and blues in them, or “warm” variants, which have touches of yellow and may look sepia toned. In this assignment, you’ll be just changing the image one color channel at a time

Your assignment is to learn how to take the stub code provided in the lecture (cleaned up below), and generate the following output image:

From the image you can see there are two parameters which are being varied for each sub-image. First, the rows are changed by color channel, where the top is the red channel, the middle is the green channel, and the bottom is the blue channel. Wait, why don’t the colors look more red, green, and blue, in that order? Because the change you to be making is the ratio, or intensity, or that channel, in relationship to the other channels. We’re going to use three different intensities, 0.1 (reduce the channel a lot), 0.5 (reduce the channel in half), and 0.9 (reduce the channel only a little bit).

For instance, a pixel represented as (200, 100, 50) is a sort of burnt orange color. So the top row of changes would create three alternative pixels, varying the first channel (red). one at (20, 100, 50), one at (100, 100, 50), and one at (180, 100, 50). The next row would vary the second channel (blue), and would create pixels of color values (200, 10, 50), (200, 50, 50) and (200, 90, 50).

Note: A font is included for your usage if you would like! It’s located in the file readonly/fanwood-webfont.ttf

Need some hints? Use them sparingly, see how much you can get done on your own first! The sample code given in the class has been cleaned up below, you might want to start from that.
在这里插入图片描述

import PIL
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageDraw
from PIL import ImageFont   #font 
font = ImageFont.truetype('readonly/fanwood-webfont.ttf', 50)
image=Image.open("readonly/msi_recruitment.gif")
image=image.convert('RGB')
intensity = [0.1, 0.5, 0.9]
def eachline(channel_num,inten_index):
    o=[0,1,2]
    o.pop(channel_num)
    block=Image.new("RGB",(image.width,50),color="black")
    txt=ImageDraw.Draw(block)
    txt.text((0,0),"channel {},intensity {}".format(channel_num,intensity[inten_index]),font=font,color="white")
    b_t=Image.new("RGB",(image.width,image.height+50))
    b_t.paste(image,(0,0))
    b_t.paste(block,(0,image.height))
    a=b_t.split()
    if channel_num==0:
        tu=(a[channel_num].point(lambda x:x*intensity[inten_index]),a[o[0]],a[o[1]])
    elif channel_num==1:
        tu=(a[o[0]],a[channel_num].point(lambda x:x*intensity[inten_index]),a[o[1]])
    else:
        tu=(a[o[0]],a[o[1]],a[channel_num].point(lambda x:x*intensity[inten_index]))
    return Image.merge("RGB",tu)
first_image=images[0]
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3))
images=[eachline(i,j) for i in [0,1,2] for j in [0,1,2]]
x=0
y=0

for img in images:
    contact_sheet.paste(img, (x, y) )
    if x+first_image.width == contact_sheet.width:
        x=0
        y=y+first_image.height
    else:
        x=x+first_image.width
        

contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) ))
display(contact_sheet)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值