def tint(self, tint='#ffffff'): self.open() src = self.img if src.mode not in ['RGB', 'RGBA']: raise TypeError('Unsupported source image mode: {}'.format(src.mode)) src.load() if type(tint) in [str, unicode]: tr, tg, tb = getrgb(tint) tl = getcolor(tint, "L") # tint color's overall luminosity else: tr, tg, tb = tint tl = sum([tr,tg,tb])/3 if not tl: tl = 1 # avoid division by zero tl = float(tl) # compute luminosity preserving tint factors sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb)) # per component adjustments # create look-up tables to map luminosity to adjusted tint # (using floating-point math only to compute table) luts = (map(lambda lr: int(lr*sr + 0.5), range(256)) + map(lambda lg: int(lg*sg + 0.5), range(256)) + map(lambda lb: int(lb*sb + 0.5), range(256))) l = grayscale(src) # 8-bit luminosity version of whole image if Image.getmodebands(src.mode) < 4: merge_args = (src.mode, (l, l, l)) # for RGB verion of grayscale else: # include copy of src image's alpha layer a = Image.new("L", src.size) a.putdata(src.getdata(3)) merge_args = (src.mode, (l, l, l, a)) # for RGBA verion of grayscale luts += range(256) # for 1:1 mapping of copied alpha values self.img = Image.merge(*merge_args).point(luts) self.save() self.close()
def image_tint(src, tint='#ffffff'): if Image.isStringType(src): # file path? src = Image.open(src) if src.mode not in ['RGB', 'RGBA']: raise TypeError('Unsupported source image mode: {}'.format(src.mode)) src.load() tr, tg, tb = getrgb(tint) tl = getcolor(tint, "L") # tint color's overall luminosity if not tl: tl = 1 # avoid division by zero tl = float(tl) # compute luminosity preserving tint factors sr, sg, sb = map(lambda tv: tv / tl, (tr, tg, tb)) # per component adjustments # create look-up tables to map luminosity to adjusted tint # (using floating-point math only to compute table) luts = (map(lambda lr: int(lr * sr + 0.5), range(256)) + map(lambda lg: int(lg * sg + 0.5), range(256)) + map(lambda lb: int(lb * sb + 0.5), range(256))) l = grayscale(src) # 8-bit luminosity version of whole image if Image.getmodebands(src.mode) < 4: merge_args = (src.mode, (l, l, l)) # for RGB verion of grayscale else: # include copy of src image's alpha layer a = Image.new("L", src.size) a.putdata(src.getdata(3)) merge_args = (src.mode, (l, l, l, a)) # for RGBA verion of grayscale luts += range(256) # for 1:1 mapping of copied alpha values return Image.merge(*merge_args).point(luts)
def get_status_images(): collection = {} for image in list_files("status"): im = Image.open(image, 'r') im = grayscale(im) a = array(im.getcolors()) a = a.sum() filename = return_filename(image).split(".")[0] collection[a] = filename return collection
def _go(): self.video = Video_device(self.videodevice.get()) try: self.highressize['size_x'], self.highressize['size_y'] = self.video.set_format( self.highressize['size_x'], self.highressize['size_y'], 0, 'MJPEG') try: self.video.set_auto_white_balance(True) except: pass try: self.video.set_exposure_auto(True) except: pass try: self.video.set_focus_auto(True) except: pass self.video.create_buffers(7) self.video.queue_all_buffers() self.video.start() stop_time = time() + 3.0 # wait for auto while stop_time >= time(): select((self.video, ), (), ()) self.update_idletasks() data = self.video.read_and_queue() image = frombytes('RGB', (self.highressize['size_x'], self.highressize['size_y'], ), data) if self.invert.get(): image = invert(image) if self.grayscale.get(): image = grayscale(image) if self.autocontrast.get(): image = autocontrast(image) if self.equalize.get(): self.image = equalize(self.image) if self.solarize.get(): self.image = solarize(self.image) if self.degree.get(): image = image.rotate(self.degree.get()) image.save(self.filename.get()) self.inc_picture() self.root.bell() self.video.stop() finally: self.video.close() self.video = None self.root.after(1, self.do_start_video)
def do_live_view(self, *args): ' show single pic live view and ask tk to call us again later ' if self.video is not None: select((self.video, ), (), ()) data = self.video.read_and_queue() self.image = frombytes('RGB', (self.previewsize['size_x'], self.previewsize['size_y']), data) if self.invert.get(): self.image = invert(self.image) if self.grayscale.get(): self.image = grayscale(self.image) if self.autocontrast.get(): self.image = autocontrast(self.image) if self.equalize.get(): self.image = equalize(self.image) if self.solarize.get(): self.image = solarize(self.image) if self.degree.get(): self.image = self.image.rotate(self.degree.get()) self.photo = PhotoImage(self.image) self.x_canvas.create_image(640/2, 640/2, image=self.photo) self.root.after(3, self.do_live_view)
def determineStar(oldWidget): c = gui.Table(width=500,height=100); c.tr(); c.td(gui.Label("Turn on your star and position the camera")); c.tr(); c.td(gui.Label("so the star is in view.")); c.tr(); c.td(gui.Label("Make sure no planets are in the way.")); c.tr(); c.td(gui.Label("Then press OK")); c.tr(); rButton = gui.Button(value = "OK"); c.td(rButton); c.tr(); rButton.connect(gui.CLICK, app.quit, None) app.quit(); app.run(c); app.remove(c); global cam; global getImage; if not cam: cam = NoFontVideoCapture.Device(); def getImage(): return cam.getImage(); for i in range(20): getImage(); camshot = grayscale(getImage()); lightCoords = []; level = camshot.getextrema()[1]-leniency; for p in camshot.getdata(): if p>=level: lightCoords.append(255); else: lightCoords.append(0); global maskIm; maskIm = new("L",res); maskIm.putdata(lightCoords); finishButton.disabled = False; app.run(oldWidget);
def position2image(positions,elem=None,wind=None): '''transforms vector of agent positions to display snapshot output format is HxW matrix of light intensity values (uint8) ''' if type(wind)==type(None): close=True; wind=Q.initDisplay() else: close=False if type(elem)==type(None): elem=visual.ElementArrayStim(wind,fieldShape='sqr', nElements=positions.shape[0], sizes=Q.agentSize, elementMask=RING,elementTex=None,colors='white') try: elem.setXYs(positions) elem.draw() wind.getMovieFrame(buffer='back') ret=wind.movieFrames[0] wind.movieFrames=[] pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT | pyglet.gl.GL_DEPTH_BUFFER_BIT) wind._defDepth=0.0 if close: wind.close() return grayscale(ret)# make grey, convert to npy except: if close: wind.close() raise
def run(self,mainframe,values=None): BLACK=0 from config import card_folder, a4_size, PRINT_FORMAT from os.path import join,isfile,splitext from math import sqrt, radians from ImageOps import grayscale dst=values['Destination file'] DEPTH=values['Depth(cm)'] LENGTH=values['Length(cm) - optional'] WIDTH=values['Width(cm) - optional'] if not dst: dlg = wx.FileDialog(mainframe, "Choose a destination file for Box Front", mainframe.deckdir, "box.png", "*.*", wx.SAVE| wx.OVERWRITE_PROMPT) try: if dlg.ShowModal() == wx.ID_OK: dst = dlg.GetPath() else: return finally: dlg.Destroy() name,ext=splitext(dst) dstfront='.'.join([name,'front'])+ext dstback='.'.join([name,'back'])+ext front=values['front'] if not isfile(front): front=None back=values['back'] if not isfile(back): back=None sidelr=values['side (left/right)'] if not isfile(sidelr): sidelr=None sidetb=values['side (top/bottom)'] if not isfile(sidetb): sidetb=None #Determine tuckbox size width,length=mainframe.fitting_size from config import left, right, bottom, top, a4_size, DEBUG, PRINT_FORMAT, card_folder, sheet_left, sheet_top, BLACK_FILLING width+=left+right+5 + 10 #10 added to make some place for the card around it in the tuckbox length+=top+bottom+5 length=float(length+10) width=float(width+10) deep=10+float((mainframe.deckgrid.GetDeckSize()))* 2.5 import config if DEPTH: deep=config.cm2p(DEPTH) if LENGTH: length=config.cm2p(LENGTH) if WIDTH: width=config.cm2p(WIDTH) #Adapt length & width to have back insert in to front lengthFront=length+10 widthFront=width+10 #Create Tuckboxes lines import Image, ImageDraw #w,l=a4_size w=int(3*deep+2*width+1) l=int(3*deep+length +1) if sorted((w,l))>sorted(a4_size): print "won't fit in a4 page" _W,_L=a4_size sheet=Image.new('RGB', (_W,_L),(255,255,255)) sheetFront=Image.new('RGB',(_W,_L),(255,255,255)) drawFront=ImageDraw.Draw(sheetFront) draw=ImageDraw.Draw(sheet) #Draw necessary lines rw=7*deep/8 rl=deep/4 for d,w,l in zip((draw,drawFront),(width,widthFront),(length,lengthFront)): d.rectangle((0,deep-rl,rw,deep),outline=BLACK) d.rectangle((0,l+deep,rw,l+deep+rl),outline=BLACK) d.rectangle((deep+w+deep-rw,deep-rl,deep+w+deep,deep),outline=BLACK) d.rectangle((deep+w+deep-rw,l+deep,deep+w+deep,l+deep+rl),outline=BLACK) d.line((rw,deep-rl, deep,deep),fill=BLACK) d.line((rw,deep-rl, deep,deep),fill=BLACK) #Now copy & resize images in the tuckboxes length=int(length) width=int(width) deep=int(deep) if front: front_image=Image.open(front) front_image=front_image.resize((widthFront,lengthFront)) sheetFront.paste(front_image,(deep,deep)) if back: back_image=Image.open(back) back_image=back_image.resize((width,length)) sheet.paste(back_image,(deep,deep)) if sidelr: side=Image.open(sidelr) side=grayscale(side.resize((length,deep))) lside=side.rotate(90,expand=True) rside=side.rotate(270,expand=True) sheet.paste(rside,(0,deep)) sheet.paste(lside,(deep+width,deep)) side=Image.open(sidelr) side=side.resize((lengthFront,deep)) lside=side.rotate(90,expand=True) rside=side.rotate(270,expand=True) sheetFront.paste(rside,(0,deep)) sheetFront.paste(lside,(deep+widthFront,deep)) if sidetb: side=Image.open(sidetb) side=grayscale(side.resize((width,deep))) bside=side.rotate(180,expand=True) sheet.paste(side,(deep,0)) sheet.paste(bside,(deep,length+deep)) side=Image.open(sidetb) side=side.resize((widthFront,deep)) bside=side.rotate(180,expand=True) sheetFront.paste(bside,(deep,0)) sheetFront.paste(side,(deep,lengthFront+deep)) #Save & display sheet.save(dstback) sheetFront.save(dstfront) import os os.startfile(dstfront) return
def loop(tstart): xmin = 0; xmax = period; global minValue, maxValue; if autoScale: minValue = False; maxValue = False; background = fig.canvas.copy_from_bbox(ax.bbox) myTime = tstart; while manager.window!=None: colorcamshot = getImage(); camshot = grayscale(colorcamshot); brightness = ImageStat.Stat(camshot,maskIm).sum[0]/divVal; # camshot = pygame.image.frombuffer(colorcamshot.tostring(), res, "RGB") # screen.blit(camshot, (0,0)) # pygame.display.flip() lastTime = myTime; myTime = time(); timeDif = myTime-lastTime; if timeDif<dur: sleep(dur-(myTime-lastTime)); # update the data t = myTime-tstart xdata.append(t) ydata.append(brightness) if not maxValue: minValue = maxValue = brightness; elif t>=xmax or (autoScale and (brightness<minValue or brightness>maxValue)): if myMode==2 and t>=xmax: break; else: fig.canvas.restore_region(background) if autoScale and t < xmax: minValue = min(minValue,brightness); maxValue = max(maxValue,brightness); add=(maxValue-minValue)/8; ax.set_ylim(max(minValue-add,0),min(maxValue+add,100)); else: xmin += halfPer; cutoff = binSearch(xdata,xmin); xmax += halfPer; del xdata[:cutoff] del ydata[:cutoff] ax.set_xlim(xmin,xmax); if autoScale: minValue = min(ydata); maxValue = max(ydata); add = (maxValue-minValue)/8; ax.set_ylim(max(minValue-add,0),min(maxValue+add,100)); if windowClosed: break; # try: fig.canvas.draw() # except Exception: # break; background = fig.canvas.copy_from_bbox(ax.bbox) line.set_data(xdata,ydata); else: line.set_data(xdata[-2:], ydata[-2:]) if windowClosed: break; # just draw the animated artist # try: ax.draw_artist(line) # except Exception: # break; if windowClosed: break; # just redraw the axes rectangle # try: fig.canvas.blit(ax.bbox) # except Exception: # break; # TODO Add Experiment mode termination here if windowClosed: plt.close(); else: global finished; finished = True;