def post(self): # check pic exgister if self.request.files == {} or 'pic' not in self.request.files: self.write('<script>m_ksb_msg.show("请选择图片")</script>') return # check pic format image_type_list = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/bmp', 'image/png', 'image/x-png'] send_file = self.request.files['pic'][0] if send_file['content_type'] not in image_type_list: self.write('<script>m_ksb_msg.show("仅支持jpg,jpeg,bmp,gif,png格式的图片!")</script>') return # check pic size 4M if len(send_file['body']) > 4 * 1024 * 1024: self.write('<script>m_ksb_msg.show("请上传4M以下的图片");</script>') return # create temp file tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) # illegal pic can't open with Image try: image_one = Image(tmp_file.name) except IOEturerror: logging.info(error) logging.info('+'*30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write('<script>m_ksb_msg.show("图片不合法!")</script>') return # check pixel if image_one.columns() < 250 or image_one.rows() < 250 or \ image_one.columns() > 2000 or image_one.rows() > 2000: tmp_file.close() self.write('<script>m_ksb_msg.show("图片长宽在250px~2000px之间!")</script>') return # saving image_path = "./static/pic/goods/" image_format = send_file['filename'].split('.').pop().lower() tmp_name = image_path + str(int(time.time())) + image_format image_one.write(tmp_name) # close temp tmp_file.close() self.write('<script>m_ksb_msg.show("文件上传成功,路径为:" + image_path[1:])</script>') return
def resize8(srcFile="", destFile="", w=200, h=200): img = Image(srcFile) #.def("extent", (void (Magick::Image::*)(const Magick::Geometry&, const Magick::Color&, const Magick::GravityType))&Magick::Image::extent) #白色背景图 backImg = None #sw源图宽度 sw = img.columns() #sh源图高度 sh = img.rows() #若目标图的宽或高都比源图大则不处理 if (sw <= w and sh <= h): backImg = Image(Geometry(w, h), 'white') backImg.composite(img, GravityType.CenterGravity, co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True" #目标的宽或高都比源图的小则进行裁剪 elif (sw > w and sh > h): #源图的宽高比 sratio = float(sw) / float(sh) rratio = float(w) / float(h) #若源图宽高比大于目标图的宽高比的话,则就高缩放,从0,0位置裁前源图宽 #print sratio,rratio if (sratio > rratio): hscale = float(h) / float(sh) rw = int(sw * hscale) rh = int(sh * hscale) else: wscale = float(w) / float(sw) rw = int(sw * wscale) rh = int(sh * wscale) linePos = int((rw - w) / 2) colPos = int((rh - h) / 2) img.scale("%dx%d" % (rw, rh)) img.crop(Geometry(w, h, linePos, colPos)) img.profile("*", Blob()) img.write(destFile) return "True" elif (sw > w): backImg = Image(Geometry(w, h), 'white') img.crop(Geometry(w, sh, int((sw - w) / 2))) backImg.composite(img, GravityType.CenterGravity, co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True" elif (sh > h): backImg = Image(Geometry(w, h), 'white') img.crop(Geometry(sw, h, 0, int((sh - h) / 2))) backImg.composite(img, GravityType.CenterGravity, co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True" return "True"
def emotion_to_avatar(emotion, font_path, color=Color("black")): """使用颜文字(文本)来生成一张方形图片 :type emotion: string :param emotion: 需要绘制在图像上的文本 :type font_path: string :param font_path: 字体文件路径 :type color: Color :param color: 绘制的文本颜色 返回结果是一个 Image 对象,并叫将会被规整到 AVATAR_SIZE 设定的大小 """ font_size = 128 max_size = len(emotion.decode('utf-8')) * font_size img = Image(Geometry(max_size, max_size), Color("white")) img.font(font_path) img.fontPointsize(font_size) img.annotate(emotion, GravityType.CenterGravity) img.trim() img.write('tmp.png') height = img.rows() width = img.columns() origin_pimg = PImage.open('tmp.png') new_pimg = PImage.new("RGB", (max(height, width), max(height, width)), "white") if height > width: new_pimg.paste(origin_pimg, ((height - width) / 2, 0)) else: new_pimg.paste(origin_pimg, (0, (width - height) / 2)) return new_pimg.resize(AVATAR_SIZE, PImage.ANTIALIAS)
def resize8( srcFile="", destFile="", w=200,h=200 ): img = Image(srcFile) #.def("extent", (void (Magick::Image::*)(const Magick::Geometry&, const Magick::Color&, const Magick::GravityType))&Magick::Image::extent) #白色背景图 backImg = None #sw源图宽度 sw = img.columns() #sh源图高度 sh = img.rows() #若目标图的宽或高都比源图大则不处理 if ( sw <= w and sh <= h ): backImg = Image(Geometry(w,h), 'white' ) backImg.composite(img, GravityType.CenterGravity, co.OverCompositeOp) backImg.profile("*",Blob()) backImg.write(destFile) return "True" #目标的宽或高都比源图的小则进行裁剪 elif ( sw > w and sh > h ): #源图的宽高比 sratio = float(sw)/float(sh) rratio = float(w)/float(h) #若源图宽高比大于目标图的宽高比的话,则就高缩放,从0,0位置裁前源图宽 #print sratio,rratio if ( sratio > rratio ): hscale = float(h)/float(sh) rw = int(sw*hscale) rh = int(sh*hscale) else: wscale = float(w)/float(sw) rw = int(sw*wscale) rh = int(sh*wscale) linePos = int( (rw-w)/2) colPos = int( (rh-h)/2) img.scale("%dx%d"%(rw,rh)) img.crop(Geometry(w,h,linePos,colPos)) img.profile("*",Blob()) img.write(destFile) return "True" elif ( sw > w ): backImg = Image(Geometry(w,h), 'white' ) img.crop(Geometry(w,sh,int((sw-w)/2))) backImg.composite(img,GravityType.CenterGravity,co.OverCompositeOp ) backImg.profile("*",Blob()) backImg.write(destFile) return "True" elif ( sh > h ): backImg = Image(Geometry(w,h), 'white' ) img.crop( Geometry(sw,h,0,int((sh-h)/2) ) ) backImg.composite(img, GravityType.CenterGravity,co.OverCompositeOp ) backImg.profile("*",Blob()) backImg.write(destFile) return "True" return "True"
def resize0( srcFile="", destFile="", w=200 ): img = Image(srcFile) sw = img.columns() sh = img.rows() if ( sw > w ): tw = w th = sh*(float(w)/float(sw)) img.scale("%dx%d"%(tw,th)) img.profile("*", Blob()) img.write(destFile) return "True"
def resize0(srcFile="", destFile="", w=200): img = Image(srcFile) sw = img.columns() sh = img.rows() if (sw > w): tw = w th = sh * (float(w) / float(sw)) img.scale("%dx%d" % (tw, th)) img.profile("*", Blob()) img.write(destFile) return "True"
def resize7(srcFile="", destFile="", w=200, h=200): img = Image(srcFile) #白色背景图 backImg = None #sw源图宽度 sw = img.columns() #sh源图高度 sh = img.rows() #若目标图的宽或高都比源图大则不处理 if (sw <= w and sh <= h): backImg = Image(Geometry(w, h), 'white') backImg.composite(img, Geometry(sw, sh, 0, 0), co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True" #目标的宽或高都比源图的小则进行裁剪 elif (sw > w and sh > h): #源图的宽高比 sratio = float(sw) / float(sh) rratio = float(w) / float(h) #若源图宽高比大于目标图的宽高比的话,则就高缩放,从0,0位置裁前源图宽 #print sratio,rratio if (sratio > rratio): hscale = float(h) / float(sh) rw = int(sw * hscale) rh = int(sh * hscale) else: wscale = float(w) / float(sw) rw = int(sw * wscale) rh = int(sh * wscale) img.scale("%dx%d" % (rw, rh)) img.crop(Geometry(w, h, 0, 0)) img.profile("*", Blob()) img.write(destFile) return "True" elif (sw > w): backImg = Image(Geometry(w, h), 'white') img.crop(Geometry(w, sh)) backImg.composite(img, Geometry(w, h, 0, 0), co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True" elif (sh > h): backImg = Image(Geometry(w, h), 'white') img.crop(Geometry(sw, h)) backImg.composite(img, Geometry(w, h, 0, 0), co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True" return "True"
def resize7( srcFile="", destFile="", w=200,h=200 ): img = Image(srcFile) #白色背景图 backImg = None #sw源图宽度 sw = img.columns() #sh源图高度 sh = img.rows() #若目标图的宽或高都比源图大则不处理 if ( sw <= w and sh <= h ): backImg = Image(Geometry(w,h), 'white' ) backImg.composite(img, Geometry( sw, sh, 0, 0 ), co.OverCompositeOp) backImg.profile("*",Blob()) backImg.write(destFile) return "True" #目标的宽或高都比源图的小则进行裁剪 elif ( sw > w and sh > h ): #源图的宽高比 sratio = float(sw)/float(sh) rratio = float(w)/float(h) #若源图宽高比大于目标图的宽高比的话,则就高缩放,从0,0位置裁前源图宽 #print sratio,rratio if ( sratio > rratio ): hscale = float(h)/float(sh) rw = int(sw*hscale) rh = int(sh*hscale) else: wscale = float(w)/float(sw) rw = int(sw*wscale) rh = int(sh*wscale) img.scale("%dx%d"%(rw,rh)) img.crop(Geometry(w,h,0,0)) img.profile("*",Blob()) img.write(destFile) return "True" elif ( sw > w ): backImg = Image(Geometry(w,h), 'white' ) img.crop(Geometry(w,sh)) backImg.composite(img,Geometry(w,h,0,0),co.OverCompositeOp ) backImg.profile("*",Blob()) backImg.write(destFile) return "True" elif ( sh > h ): backImg = Image(Geometry(w,h), 'white' ) img.crop( Geometry(sw,h) ) backImg.composite(img, Geometry(w,h,0,0),co.OverCompositeOp ) backImg.profile("*",Blob()) backImg.write(destFile) return "True" return "True"
def resize10(srcFile="", destFile="", w=200): img = Image(srcFile) sw = img.columns() sh = img.rows() scale = sw * sh if (scale > w): tw = int(sw * ((float(w) / float(scale))**0.5)) th = int(w / tw) img.scale("%dx%d" % (tw, th)) img.profile("*", Blob()) img.write(destFile) return "True"
def resize10( srcFile="", destFile="", w=200 ): img = Image( srcFile ) sw = img.columns() sh = img.rows() scale = sw*sh if ( scale > w ): tw = int(sw*((float(w)/float(scale))**0.5)) th = int(w/tw) img.scale("%dx%d"%(tw,th)) img.profile("*",Blob()) img.write(destFile) return "True"
def resize9(srcFile="", destFile="", w=200, h=200, color="", crop=False, align="center"): img = Image(srcFile) #白色背景图 backImg = None #sw源图宽度 sw = img.columns() #sh源图高度 sh = img.rows() #目标图与源图的宽比例 wScale = float(w) / float(sw) #目标图与源图的高比例 hScale = float(h) / float(sh) if (w > sw or h > sh): if (wScale == hScale): tw = w th = h elif (wScale < hScale): th = h tw = sw * wScale else: tw = w th = sh * hScale elif (w < sw or h < sh): if (wScale == hScale): tw = w th = h elif (wScale < hScale): th = h tw = sw * wScale else: tw = w th = sh * hScale else: tw = sw th = sh img.scale("%dx%d" % (tw, th)) backImg = Image(Geometry(w, h), 'white') backImg.composite(img, GravityType.CenterGravity, co.OverCompositeOp) backImg.profile("*", Blob()) backImg.write(destFile) return "True"
def execute(self, context): downloaded_thumbnail = context["task_instance"].xcom_pull( self.get_inputs_from) if downloaded_thumbnail is None: log.info("Nothing to process.") return log.info("downloaded_thumbnail: {}".format(downloaded_thumbnail)) img = Image(downloaded_thumbnail) least_dim = min(int(img.columns()), int(img.rows())) img.crop("{dim}x{dim}".format(dim=least_dim)) img.scale(self.thumb_size_x+'x'+self.thumb_size_y) img.scale("{}x{}".format(self.thumb_size_x, self.thumb_size_y)) output_path = os.path.join( os.path.dirname(downloaded_thumbnail), "thumbnail.jpeg") img.write(output_path) return output_path
def convertGMtoPIL(gmimage): """ Convert GraphicsMagick image to PIL work with grayscale and colour """ img = Image(gmimage) # make copy gmimage.depth(8) img.magick("RGB") w, h = img.columns(), img.rows() blob = Blob() img.write(blob) data = blob.data # convert string array to an RGB PIL image pilimage = PilImage.fromstring("RGB", (w, h), data) return pilimage
def convertGMtoPIL(gmimage): """ Convert GraphicsMagick image to PIL work with grayscale and colour """ img = Image(gmimage) # make copy gmimage.depth(8) img.magick("RGB") w, h = img.columns(), img.rows() blob = Blob() img.write(blob) data = blob.data # convert string array to an RGB PIL image pilimage = PilImage.fromstring('RGB', (w, h), data) return pilimage
def resize9( srcFile="", destFile="", w=200,h=200, color="", crop=False, align="center" ): img = Image(srcFile) #白色背景图 backImg = None #sw源图宽度 sw = img.columns() #sh源图高度 sh = img.rows() #目标图与源图的宽比例 wScale = float(w)/float(sw) #目标图与源图的高比例 hScale = float(h)/float(sh) if ( w > sw or h > sh ): if (wScale == hScale ): tw = w th = h elif ( wScale < hScale ): th = h tw = sw*wScale else: tw = w th = sh*hScale elif( w<sw or h < sh ): if (wScale == hScale ): tw = w th = h elif ( wScale < hScale ): th = h tw = sw*wScale else: tw = w th = sh*hScale else: tw = sw th = sh img.scale("%dx%d"%(tw,th)) backImg = Image(Geometry(w,h), 'white' ) backImg.composite(img,GravityType.CenterGravity,co.OverCompositeOp ) backImg.profile("*",Blob()) backImg.write(destFile) return "True"
def resize1( srcFile="", destFile="", w=200,h=200 ): img = Image( srcFile ) #sw源图宽度 sw = img.columns() sh = img.rows() #要缩略的宽度 rw = w #要缩略的高度 rh = h #源图的宽高比 sratio = float(sw)/float(sh) #目标图的宽高比 rratio = float(rw)/float(rh) #若源图的宽高比大于目标图的宽高比时,则按照高进行缩放后再裁剪宽度 if ( sratio > rratio ): hscale = float(rh)/float(sh) w = sw*hscale h = sh*hscale #print (sw,sh,w,h,rw,rh,hscale) #就高缩放 img.scale("%dx%d"%(w,h)) #计算裁剪宽的部分的横坐标,超出的宽的部分进行裁剪 tmpRowsPos = int((sw*hscale - rw)/2) img.crop(Geometry( rw,rh,tmpRowsPos,0 ) ) #若源图的宽高比小于目标图的宽高比时,则按照宽进行缩放后再裁剪高度 else: wscale = float(rw)/float(sw) w = sw*wscale h = sh*wscale #print (sw,sh,w,h,rw,rh,wscale) #就宽缩放 img.scale("%dx%d"%(w,h)) tmpColsPos = int((sh*wscale-rh)/2 ) img.crop( Geometry( rw,rh,0,tmpColsPos ) ) #只有宽大于目标宽度的时候才进行缩略 #elif ( sw > w ): # pass #unicodestring.encode("utf-8") img.profile("*", Blob()) img.write(destFile) return "True"
def resize1(srcFile="", destFile="", w=200, h=200): img = Image(srcFile) #sw源图宽度 sw = img.columns() sh = img.rows() #要缩略的宽度 rw = w #要缩略的高度 rh = h #源图的宽高比 sratio = float(sw) / float(sh) #目标图的宽高比 rratio = float(rw) / float(rh) #若源图的宽高比大于目标图的宽高比时,则按照高进行缩放后再裁剪宽度 if (sratio > rratio): hscale = float(rh) / float(sh) w = sw * hscale h = sh * hscale #print (sw,sh,w,h,rw,rh,hscale) #就高缩放 img.scale("%dx%d" % (w, h)) #计算裁剪宽的部分的横坐标,超出的宽的部分进行裁剪 tmpRowsPos = int((sw * hscale - rw) / 2) img.crop(Geometry(rw, rh, tmpRowsPos, 0)) #若源图的宽高比小于目标图的宽高比时,则按照宽进行缩放后再裁剪高度 else: wscale = float(rw) / float(sw) w = sw * wscale h = sh * wscale #print (sw,sh,w,h,rw,rh,wscale) #就宽缩放 img.scale("%dx%d" % (w, h)) tmpColsPos = int((sh * wscale - rh) / 2) img.crop(Geometry(rw, rh, 0, tmpColsPos)) #只有宽大于目标宽度的时候才进行缩略 #elif ( sw > w ): # pass #unicodestring.encode("utf-8") img.profile("*", Blob()) img.write(destFile) return "True"
def resize5( srcFile="", destFile="", w=200,h=200 ): #CONVERT_RESIZE_CROP = "%s -resize %d" + "x" + " -crop %d" + "x" + "%d" + "+0+0 +repage %s" img = Image( srcFile ) sw = img.columns() sh = img.rows() #源图宽高比 sratio = float(sw)/float(sh) #目标图宽高比 tratio = float(w)/float(h) #若源图的宽高比大于目标图的宽高比,则 if( sratio == tratio and (w==sw) and (h==sh )): img.profile("*",Blob()) img.write(destFile) return "True" elif ( sratio > tratio ): hscale = float(w)/float(sw) tw = sw*hscale th = sh*hscale img.scale("%dx"%(tw)) if ( th > h ): img.crop(Geometry(w,h)) img.profile("*",Blob()) img.write(destFile) return "True" elif( sratio < tratio ): wscale = float(w)/float(sw) tw = int(sw*wscale) th = int(sh*wscale) #260 132 670 502 0.388059701493 260 194 img.scale("%dx%d"%(tw,th)) if ( th > h ): img.crop(Geometry(w,h)) img.profile("*",Blob()) img.write(destFile) return "True" return "True"
def resize5(srcFile="", destFile="", w=200, h=200): #CONVERT_RESIZE_CROP = "%s -resize %d" + "x" + " -crop %d" + "x" + "%d" + "+0+0 +repage %s" img = Image(srcFile) sw = img.columns() sh = img.rows() #源图宽高比 sratio = float(sw) / float(sh) #目标图宽高比 tratio = float(w) / float(h) #若源图的宽高比大于目标图的宽高比,则 if (sratio == tratio and (w == sw) and (h == sh)): imb.profile("*", Blob()) img.write(destFile) return "True" elif (sratio > tratio): hscale = float(w) / float(sw) tw = sw * hscale th = sh * hscale img.scale("%dx" % (tw)) if (th > h): img.crop(Geometry(w, h)) img.profile("*", Blob()) img.write(destFile) return "True" elif (sratio < tratio): wscale = float(w) / float(sw) tw = int(sw * wscale) th = int(sh * wscale) #260 132 670 502 0.388059701493 260 194 img.scale("%dx%d" % (tw, th)) if (th > h): img.crop(Geometry(w, h)) img.profile("*", Blob()) img.write(destFile) return "True" return "True"
parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('infiles', nargs='*', type=argparse.FileType('r'), default=sys.stdin) parser.add_argument('-o', default="split%d.pnm") args = parser.parse_args() number = 0 outpattern = args.o for file in args.infiles: blob = Blob(file.read()) file.close() # upper half im = Image(blob) width = im.columns() height = im.rows() // 2 geo = Geometry( '%dx%d' % (width,height) ) im.crop(geo) im.write(outpattern % number) # lower half im = Image(blob) width = im.columns() height = im.rows() // 2 geo = Geometry( '%dx%d+0+%d' % (width,height,height) ) im.crop(geo) im.rotate(180) im.write(outpattern % (number + 1)) number += 2
def post(self, gid=None): good_keys = ['catalog', 'name', 'subname', 'price', 'discount'] good = dict() if gid: good = self.db.goods.find_one({'_id':gid}) for key in good_keys: good[key] = self.get_argument(key, None) if gid: self.db.goods.save(good) else: #========= new id generate gid = 'G'+(str(uuid.uuid4()).split('-'))[4].upper() #========= image handler # check pic exgister if self.request.files == {} or 'pic' not in self.request.files: self.write('<script>alert("请选择图片")</script>') send_file = self.request.files['pic'][0] # check pic format image_type = ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/bmp', 'image/png', 'image/x-png'] if send_file['content_type'] not in image_type: self.write('<script>alert("仅支持jpg,jpeg,bmp,gif,png格式的图片")</script>') # check pic size 4M if len(send_file['body']) > 4 * 1024 * 1024: self.write('<script>alert("请上传4M以下的图片");</script>') # create temp file tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) # illegal pic can't open with Image try: image_one = Image(tmp_file.name) except IOError: logging.info(error) logging.info('+'*30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write('<script>alert("图片不合法!")</script>') # check pixel if image_one.columns() < 250 or image_one.rows() < 250 or \ image_one.columns() > 2000 or image_one.rows() > 2000: tmp_file.close() self.write('<script>alert("图片长宽在250px~2000px之间!")</script>') # saving two type image_path = "./static/images/goods/" image_format = send_file['filename'].split('.').pop().lower() thumbnail_174 = image_path + gid + '_174.' + image_format image_one.quality(100) image_one.scale('174x174') image_one.write(str(thumbnail_174)) thumbnail_94 = image_path + gid + '_94.' + image_format image_one.quality(100) image_one.scale('94x94') image_one.write(str(thumbnail_94)) # close temp tmp_file.close() good['_id'] = gid good['image1'] = thumbnail_174 good['image2'] = thumbnail_94 self.db.goods.insert(good) self.redirect('/admin', permanent=True)