贴些图片处理的代码
ghostwwl
|
1#
ghostwwl 发表于 2008-05-27 00:48
贴些图片处理的代码
好久没来CU贴代码了 贴一段 买了相机后 一直写给自己用的
呵呵 用的最多的 还是批量加水印
[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/env python
#-*- coding:UTF-8 -*- #**************************** # Author: ghostwwl # Email: [email]ghostwwl@gmail.com[/email] # License: GPL #**************************** import os import sys import Image import ImageDraw import ImageFont from opencv.cv import * from opencv.highgui import * import win32ui, win32con logo = os.path.join(os.path.dirname(sys.argv[0]), 'logo.jpg') def mImageROI(infile, outfile, zoom, point, width, height): ''' zoom:缩放倍数 infile:原始文件 outfile:处理生成的文件 point 起始点坐标 width, height: 截取区域的宽高 ''' try: if not os.path.exists(infile): return "No infile!" im = cvLoadImage(infile, 1) cw, ch = int(im.width*zoom), int(im.height*zoom) fm = cvCreateImage(cvSize(cw, ch), im.depth, im.nChannels) cvSetZero(fm) del cw,ch cvResize(im, fm) cv.cvRelease(im) del im om = cvCreateImage(cvSize(int(point[0] + width), int(point[1] + height)), fm.depth, fm.nChannels) cvSetZero(om) rcenter = cvPoint2D32f(px1 + width/2.0, py1 + height/2.0) cvGetRectSubPix(fm, om, rcenter) del rcenter cvSaveImage(outfile, om) cv.cvReleaseImage(fm) del fm cv.cvReleaseImage(om) del om print "ok" except Exception, e: return "ROI Error: %s" % str(e) def fupian(infile, outfile): def base_1(data1,data2): x1, y1 = data1 x2, y2 = data2 x1, x2, y1, y2 = float(x1), float(x2), float(y1), float(y2) a = (y2 - y1)/(x2 * x2 - x1 * x1) b = y1 - a * x1 * x1 return a, b def base_2(i, points): points_length = len(points) for j in range(points_length): now_point = points[j] next_point = points[j+1] x1, x2 = now_point[0],next_point[0] if x1 <= i <= x2: a, b = base_1(now_point, next_point) return a * i * i + b def smoonth(i): points=[(0, 0), (73, 47), (179, 213), (255, 255)] return base_2(i, points) def smoonth_r(i): points=[(0, 0), (91, 22), (192, 149), (255, 232)] return base_2(i, points) def smoonth_g(i): points=[(0, 21), (70, 90), (189, 151), (255, 212)] return base_2(i,points) def smoonth_b(i): points=[(0, 2), (45, 68), (205, 147), (255, 255)] return base_2(i,points) try: im = Image.open(infile) R, G, B = im.split() R = R.point(smoonth_r) G = G.point(smoonth_g) B = B.point(smoonth_b) new = Image.merge('RGB', (R, G, B)) new = new.point(smoonth) new.save(outfile) except Exception, e: print str(e) def WaterMark(fil, dh='y'): ''' 第一个参数是 图片的完整路径 第二个参数表示是否对logo图像进行透明处理 PIL简单 就PIL OpenCV虽然速度快 写起来也多要好多行啊 ''' img1 = Image.open(fil).convert("RGB") logoimg = Image.open(logo).convert("RGBA") #这里是针对 w2 = (img1.size[0] - 10)/5 h2 = w2*logoimg.size[1]/logoimg.size[0] logoimg = logoimg.resize((w2, h2), Image.ANTIALIAS) #在属主图像中生成嵌入图像大小的box box = (img1.size[0] - w2 - 20, img1.size[1] - h2 - 20) if 'y' in dh: Mask = logoimg.convert("L").point(lambda x: min(x, 100)) logoimg.putalpha(Mask) img1.paste(logoimg, box, logoimg) img1.save(fil.split(".")[0]+"_W.jpg") #-------------------------------------------------------------- def WriteText(im, text = None, align = 0): ''' align 表示对齐方式 1 为右下脚 0 为底部居中 text 表示要写到图片上的文字 如果需要在linux下用把请修改字体路径和字体, 然后确保安装了python和pil模块 这里没有用OpenCV是因为 官方库 暂时不支持中文 如果要用中文 就要C++ 用国内爱好这开发的 第三方扩展了 所以 直接PIL了 ''' if not text: text = 'Email:ghostwwl@gmail.com' img = Image.open(im).convert("RGB") Draw = ImageDraw.ImageDraw(img, "RGBA") #自动调整字体大小 size = 0 while True: size += 1 #如果在linux下 请去/usr/share/fonts下找个truetype吧 支持中文的字体 font = ImageFont.truetype("C:\\Windows\\Fonts\\STXINWEI.TTF", size) tw, th = font.getsize(text) if tw > img.size[0]/5: break if align == 1: w1 = (img.size[0] - tw)/2 else: w1 = img.size[0]*0.78 h1 = img.size[1] - th - 10 Draw.setfont(font) Draw.text((w1, h1), unicode(text, "utf-8")) img.save(im.split(".")[0]+"_Text.jpg") #-------------------------------------------------------------- def GetFiles(): openflag = "JPEG File (*.jpg)|*.jpg|ALL File (*.*)|*.*|" filedialog = win32ui.CreateFileDialog(1, None, None, win32con.OFN_ALLOWMULTISELECT|win32con.OFN_HIDEREADONLY, openflag) filedialog.SetOFNTitle("Select files to Work") filedialog.DoModal() return filedialog.GetPathNames() if __name__ == '__main__': #这里需要那些函数 自己改写 还有的东西没加进来 #会继续完善 想加入 人脸库 从海量照片中快速定位 包含自己的照片 和包含人脸的照片 #有时间 就写一点 一点点的爬啊 # f = GetFiles() import glob import time pw = raw_input("Enter The JPEG File Path:") fs = glob.glob(os.path.join(pw, '*.jpg')) for i in fs: # WriteText(i) if not i.endswith('W.jpg') or not i.endswith('W.JPG'): WaterMark(i) print "%s Water Img: %s OK!" % (time.strftime("[%Y-%m-%d %H:%M:%S]"), i) |