分类目录归档:编码

python opencv 去水印实现

主要利用了 opencv 的 inpaint 方法,实现类似蒙版的效果
http://docs.opencv.org/master/df/d3d/tutorial_py_inpainting.html#gsc.tab=0

效果:注意右下角&左上角
原图

处理后:

'''
string to array
'''
def get_opencv_img_from_string(data):
    arr = np.array(bytearray(data), dtype=np.uint8)
    img = cv2.imdecode(arr, -1)
    return img

'''
array to string
'''
def get_buffer_from_bytes_array(data):
    arr = cv2.imencode(".jpg", data)[1].tostring()
    return arr


def watermark_clean(data, platform):
    img = get_opencv_img_from_string(data)
    height = img.shape[0]
    width = img.shape[1]
    rects = ((width - 169, height - 22, width, height),(1, 1, 164, 21)) #水印区域
    cv2.imshow('a', img)
    mask = np.zeros(img.shape[:2], np.uint8)
    for rect in rects:
        x1, y1, x2, y2 = rect
        cv2.rectangle(mask, (x1, y1), (x2, y2), (255, 255, 255), -1)
        img = cv2.inpaint(img, mask, 10.0, cv2.INPAINT_TELEA) #蒙版
    return get_buffer_from_bytes_array(img)


def show_img(img_arr):
    cv2.imshow('test', img_arr)
    cv.WaitKey(0)

Installing OpenCV 2.4.* with Python Support

Opencv的安装简直让想在图像处理方面试试水的新手抓耳挠腮,今天记录下心酸的安装过程,避免 fall in a same hole.

1.

cd ~/
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git

2.

cd ~/opencv
mkdir build
cd build

3.

cmake -D BUILD_SHARED_LIBS=OFF -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

(这部如果遇到文件 ippicv_linux_20151201.tgz 下载失败的情况,可以手动下载至 opencv/3rdpart/ippiv/downloads/liniux_*/ 下 再作cmake)

4.

make

5.

sudo make install

6.

sudo ln -s /usr/local/lib/python2.7/site-packages/cv2.so {your_python_path}/cv2.so
sudo ln -s /usr/local/lib/python2.7/site-packages/cv.py {your_python_path}/cv.py

zimuzu.tv自动签到脚本(Python)

python2.7,需要requests库

#coding:utf8
'''
@author modm
http://www.7mdm.com
'''
import requests
import urllib2
import string
import random
import uuid
import time
import re
def sign(account,password):
    print 'signing...'
    client_id=str(uuid.uuid1())
    headers={'Accept':' application/json, text/javascript, */*; q=0.01',
    'X-DevTools-Emulate-Network-Conditions-Client-Id':client_id,
    'X-Requested-With':'XMLHttpRequest',
    'X-FirePHP-Version':'0.0.6',
    'Host':'www.zimuzu.tv',
    'Origin':'http://www.zimuzu.tv',
    'Referer':'http://www.zimuzu.tv/user/login',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
    'Referer':' http://www.zimuzu.tv/user/sign',
    'Accept-Encoding':' gzip, deflate, sdch',
    'Accept-Language':' zh-CN,zh;q=0.8'
    }
    resHeaders = requests.get('http://www.zimuzu.tv/user/login',headers=headers).headers
    session = resHeaders['set-cookie'][10:36]
    headers['Cookie']='PHPSESSID='+session+'; CNZZDATA1254180690=111511151-1430530153-%7C1430530153'
    data={'account':account,
    'password':password,
    'remember':'1',
    'url_back':'http://www.zimuzu.tv/user/sign'}
    res = requests.post('http://www.zimuzu.tv/User/Login/ajaxLogin',data=data,headers=headers)
    cookie = res.headers['set-cookie']
    cookie=cookie.replace('GINFO=deleted;','').replace('GKEY=deleted;','')
    GINFO=re.search('GINFO=uid[^;]+',cookie).group(0)+";"
    GKEY=re.search('GKEY=[^;]+',cookie).group(0)+";"
    CPS = 'yhd%2F'+str(int(time.time()))+";"
    Cookie =' PHPSESSID='+session+'; '+CPS+(GINFO+GKEY)*3
    headers['Cookie']=Cookie
    requests.get("http://www.zimuzu.tv/user/sign",headers=headers).content
    print 'wait for 20 seconds...'
    time.sleep(20)
    content = requests.get("http://www.zimuzu.tv/user/sign/dosign",headers=headers).json()
    print "sign success! " if content['data']!=False else ("signed! " if content['data']==False else "sign failed! "+str(content)),content['status']
sign('用户名','密码')

python post上传文件

首先我们先来看看一个上传图片的网页form结构

<form action=”images_upload.php”  method=”post” name=”form_1″ id=”form_1″ enctype=”multipart/form-data” >
<input name=”file” class=”div3″ id=”file” type=”file” onchange=”load_from()” title=”点击更改”>
<button type=”submit” value=”上传”/>
</form>

可以看到action提交表单的地址为images_upload.php,method为post,enctype=”multipart/form-data

由于不知道如何把图片上传,就google了下

发现了如下几个库及方法

Python中使用POST方式上传文件(poster)

http://oldj.net/article/python-upload-file-via-form-post/

using requests post a multipart encoded file(requests)

http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

Python模拟HTTP Post上传文件(urllib2)

http://www.cnblogs.com/chy710/p/3791317.html

打开chrome 查看network 请求信息,发现request payload的一栏信息如下:

——WebKitFormBoundary6W5jJP9OUllB3OgE

Content-Disposition: form-data; name=”file”; filename=”1.jpg”

Content-Type: image/jpeg

 

——WebKitFormBoundary6W5jJP9OUllB3OgE

Content-Disposition: form-data; name=”type”

 

 

img1

——WebKitFormBoundary6W5jJP9OUllB3OgE–

由于对于我要处理的网页1,2两种方法无效,遂尝试第三种方法

把图片的二进制文件放到content-type之后,配好data及headers,请求成功得到响应数据。

实现代码如下:
boundary = '----%s' % hex(int(time.time() * 1000))
data = []
fr=open(your imgDir,'rb')
print fr
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="file"; filename="1.jpg"')
data.append('Content-Type: %s\r\n' % 'image/jpeg')
data.append(fr.read())
fr.close()
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="type"')
data.append('')
data.append('--%s--' % boundary)
http_url='images_upload.php'
http_body='\r\n'.join(data)
try:
#buld http request
req=urllib2.Request(http_url, data=http_body)
req.unverifiable = True
#header
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
req.add_header('Referer','somewhere')
req.add_header('Cookie',add your cookie)
#post data to server
resp = urllib2.urlopen(req, timeout=5)
#get response
qrcont=resp.read()
print qrcont

except Exception,e:
print ‘http error’

python去/替标点总结

首先是英文标点:
import re, string, timeit

s = “string. With. Punctuation”
exclude = set(string.punctuation)
table = string.maketrans(“”,””)
regex = re.compile(‘[%s]’ % re.escape(string.punctuation))

def test_set(s):
return ”.join(ch for ch in s if ch not in exclude)

def test_re(s): # From Vinko’s solution, with fix.
return regex.sub(”, s)

def test_trans(s):
return s.translate(table, string.punctuation)

def test_repl(s): # From S.Lott’s solution
for c in string.punctuation:
s=s.replace(c,””)
return s

print “sets :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_set as f’).timeit(1000000)
print “regex :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_re as f’).timeit(1000000)
print “translate :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_trans as f’).timeit(1000000)
print “replace :”,timeit.Timer(‘f(s)’, ‘from __main__ import s,test_repl as f’).timeit(1000000)
速度对比:
sets : 19.8566138744
regex : 6.86155414581
translate : 2.12455511093
replace : 28.4436721802

 

然后是中文标点:

re.sub(u”[\uFF00-\uFFEF]+” ,’ ‘,str.decode(‘utf8’))

 

参考地址:

http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python

http://www.unicode.org/charts/PDF/UFF00.pdf