def make(src_path, out_path): src = Image.open(src_path) src = src.copy() (srcr, srcg, srcb, srca) = src.split() white = ImageChops.constant(src, 255) outr = cast_gradation(srcr, 0, 90) outg = cast_gradation(srcg, 0, 90) outb = cast_gradation(srcb, 0, 90) outa = srca.copy() outr = ImageChops.composite(srcr, white, srca) outg = ImageChops.composite(srcg, white, srca) outb = ImageChops.composite(srcb, white, srca) (shadow_a, shadow) = make_inset_shadow(srca) outr = ImageChops.subtract(outr, shadow, 1, 0) outg = ImageChops.subtract(outg, shadow, 1, 0) outb = ImageChops.subtract(outb, shadow, 1, 0) outa = ImageChops.lighter(outa, shadow_a) (highlight_a, highlight) = make_highlight(srca) outa = ImageChops.lighter(outa, highlight) outa = ImageChops.subtract(outa, ImageChops.constant(outa, 25), 1, 0) out = Image.merge('RGBA', (outr, outg, outb, outa)) out.save(out_path)
def assertSameImage(self, baseImage, testImage): base_file = open(baseImage, 'rb') test_file = open(testImage, 'rb') base_image = Image.open(base_file) base = base_image.getdata() test_image = Image.open(test_file) test = test_image.getdata() has_diff = True for i in range(len(base)): # Stop at first difference if (base[i] - test[i]) != 0: break else: has_diff = False if has_diff and 'SHOW_IMAGE_DIFF' in os.environ: test_image.show() base_image.show() differ = ImageChops.subtract(test_image, base_image) differ.show() differ2 = ImageChops.subtract(base_image, test_image) differ2.show() # output the result as base64 for travis debugging if 'SHOW_DIFF_DEBUG' in os.environ: print(base64.b64encode(differ.tobytes())) print(base64.b64encode(differ2.tobytes())) if has_diff and 'SHOW_DIFF_DEBUG' in os.environ: print() print(os.system("gs --version")) base_file.seek(0) print(baseImage) print(base64.b64encode(base_file.read())) print(self._basePath) with open(self._basePath, 'rb') as base_pdf: print(base64.b64encode(base_pdf.read())) test_file.seek(0) print(testImage) print(base64.b64encode(test_file.read())) print(self._testPath) with open(self._testPath, 'rb') as test_pdf: print(base64.b64encode(test_pdf.read())) base_file.close() test_file.close() if has_diff: self.fail('Image is not the same: %s' % os.path.basename(baseImage))
def process(self): srcRun = self.paths[0][3] dstRun = self.paths[1][3] quantity = self.paths[0][-1].split(".")[0] textToDraw = ("BINARY " if useSimpleColorDiff else "") + "DIFFERENCE BETWEEN " + srcRun + " AND " + dstRun + " FOR " + quantity textWidthHeight = self.font.getsize(textToDraw) myDraw = ImageDraw.Draw(self.finalImage) myDraw.text( (self.currSize[0] * 1.5 - textWidthHeight[0] * 0.5, 100), textToDraw, fill = (0, 0, 0), font = self.font) del myDraw if useSimpleColorDiff: #ONE COLOR FOR ALL DIFFERENCES myDiff = ImageChops.subtract(self.regions[0], self.regions[1]) for i in range(myDiff.size[0]): for j in range(myDiff.size[1]): # print(i, j) px = myDiff.getpixel((i, j)) if px[0] != 0 or px[1] != 0 or px[2] != 0: myDiff.putpixel((i, j), binaryDifferenceColor) self.regions[1].putpixel((i, j), binaryDifferenceColor) # create difference-masked image myDiff = ImageChops.add(myDiff, self.regions[2]) self.finalImage.paste(self.regions[1], (self.currSize[0], self.currSize[1] + yShift)) textToDraw = "DIFFERENCE-MASKED TRACKER MAP FOR RUN " + dstRun else: ###MANY COLORS INDICATE MANY POSSIBLE DIFFERENCES myDiff = ImageChops.subtract(self.regions[0], self.regions[1]) myDiff = ImageChops.add(myDiff, self.regions[2]) #2 - refRegion blendedImage = Image.blend(self.regions[0], self.regions[1], op) self.finalImage.paste(blendedImage, (self.currSize[0], self.currSize[1] + yShift)) textToDraw = "SUPERIMPOSED TRACKER MAPS" textWidthHeight = self.font.getsize(textToDraw) myDraw = ImageDraw.Draw(self.finalImage) myDraw.text( (self.currSize[0] * 1.5 - textWidthHeight[0] * 0.5, self.currSize[1] + 100), textToDraw, fill = (0, 0, 0), font = self.font) del myDraw self.finalImage.paste(myDiff, (self.currSize[0], yShift)) outputFileName = self.savePath + "comparisonImage_" + srcRun + "vs" + dstRun + ".png" print(outputFileName) self.finalImage.save(outputFileName)
def run(self): ''' Limpia las imagenes empleando PIL ''' # notificar el inicio de operaciones self.notificador.inicio_operacion.emit() import Image, ImageChops, ImageOps, ImageEnhance for i in range(len(self.imagenesIn)): im = Image.open(self.imagenesIn[i]) if im: im1 = ImageChops.invert(im) im = ImageChops.subtract(im1, im) im = im.convert('L') im = ImageChops.invert(im) im = ImageEnhance.Brightness(im).enhance(2.0) im = ImageOps.colorize(im, (0, 0, 0), (255, 255, 255)) im.save(self.imagenesOut[i]) else: self.notificador.error.emit('Error al procesar la imagen: ' + self.imagenesIn[i]) return # notificar a la aplicacion el resultado de la operacion self.notificador.correcto.emit(self.plugin.nombre()) # notificar el fin de operaciones self.notificador.fin_operacion.emit() return
def loadallimgs(self, basename, scalefactor=1.0): """load images from disk, scale them immediately, subtract off the image called 'ambient' (if present) from all other images. Given basename like path/to/foo, loads images with names like path/to/foo.light1, path/to/foo.light2, etc. The filename after the . is considered to be the light name throughout the program. """ if self.loadedscale == scalefactor: # already loaded at this scale return self.im = {} ambientimg = None sizenotset = 1 for fullname in glob.glob(basename + ".*"): x = fullname[fullname.find(".") + 1 :] self.im[x] = Image.open(fullname) if scalefactor != 1.0: self.im[x] = self.im[x].resize([a * scalefactor for a in self.im[x].size]) if x == "ambient": ambientimg = self.im[x] if sizenotset: self.config(width=self.im[x].size[0], height=self.im[x].size[1]) self.itk = ImageTk.PhotoImage(Image.new("RGB", self.im[x].size)) self.config(image=self.itk) sizenotset = 0 # subtract off an image called 'ambient' from all the rest if ambientimg is not None: for k in self.im.keys(): if k != "ambient": self.im[k] = ImageChops.subtract(self.im[k], ambientimg) self.loadedscale = scalefactor self._remix() # update the image
def make_inset_shadow(alpha): mc = alpha.copy() for i in xrange(6): mc = mc.filter(ImageFilter.SMOOTH_MORE) mc = ImageChops.subtract(alpha, mc) mcb = ImageEnhance.Brightness(mc).enhance(0.35) m1 = alpha.copy() for i in xrange(6): m1 = m1.filter(ImageFilter.SMOOTH_MORE) m1 = m1.offset(0, OFFSET_S) m1 = ImageChops.subtract(alpha, m1) m1b = ImageEnhance.Brightness(m1).enhance(0.35) m = ImageChops.lighter(mc, m1) mb = ImageChops.lighter(mcb, m1b) return (m, mb)
def erode(self, image): paddedImage = self.createPaddedImage(image, 1) thresholdImg = paddedImage.point(lambda i, v=128: i > v and 255) filteredImg = thresholdImg.filter(ImageFilter.FIND_EDGES) thresholdImg = filteredImg.point(lambda i, v=128: i > v and 255) arithImg = ImageChops.subtract(paddedImage, thresholdImg) box = (1, 1, arithImg.size[0]-1, arithImg.size[1]-1) outImage = arithImg.crop(box) return outImage
def estimate(file, s=5): """Estimates the amount of focus of an image file. Returns a real number: lower values indicate better focus. """ im = Image.open(file).convert("L") w,h = im.size box = (w/2 - 50, h/2 - 50, w/2 + 50, h/2 + 50) im = im.crop(box) imf = im.filter(ImageFilter.MedianFilter(s)) d = ImageChops.subtract(im, imf, 1, 100) return ImageStat.Stat(d).stddev[0]
def do_subtract(self): """usage: subtract <image:pic1> <image:pic2> <int:offset> <float:scale> Pop the two top images, produce the scaled difference with offset. """ import ImageChops image1 = self.do_pop() image2 = self.do_pop() scale = float(self.do_pop()) offset = int(self.do_pop()) self.push(ImageChops.subtract(image1, image2, scale, offset))
def estimate(file, s=5): """Estimates the amount of focus of an image file. Returns a real number: lower values indicate better focus. """ im = Image.open(file).convert("L") w, h = im.size box = (w / 2 - 50, h / 2 - 50, w / 2 + 50, h / 2 + 50) im = im.crop(box) imf = im.filter(ImageFilter.MedianFilter(s)) d = ImageChops.subtract(im, imf, 1, 100) return ImageStat.Stat(d).stddev[0]
def estimate(file, s=5): """Estimates the amount of focus of an image file. Returns a real number: higher values indicate better focus. Bug: a high-contrast, blurry image can be considered with better focus than a low-contrast, perfectly focused image. """ im = Image.open(file).convert("L") w, h = im.size box = (w / 2 - 50, h / 2 - 50, w / 2 + 50, h / 2 + 50) im = im.crop(box) imf = im.filter(ImageFilter.MedianFilter(s)) d = ImageChops.subtract(im, imf, 1, 100) return ImageStat.Stat(d).stddev[0]
def estimate(file, s=5): """Estimates the amount of focus of an image file. Returns a real number: higher values indicate better focus. Bug: a high-contrast, blurry image can be considered with better focus than a low-contrast, perfectly focused image. """ im = Image.open(file).convert("L") w,h = im.size box = (w/2 - 50, h/2 - 50, w/2 + 50, h/2 + 50) im = im.crop(box) imf = im.filter(ImageFilter.MedianFilter(s)) d = ImageChops.subtract(im, imf, 1, 100) return ImageStat.Stat(d).stddev[0]
def create(self): mask = ImageChops.subtract(self.white, self.black, -1, 255) maskgray = mask.convert("L") #out = self.black.convert("RGBA") # this doesn't premultiply alpha correctly? #this better? def divide(ch): env = {"val": ch, "mask": maskgray} return ImageMath.eval("val*255/mask",env).convert("L") out = channelMap(divide, self.black).convert("RGBA") out.putalpha(maskgray) return out
def create(self): mask = ImageChops.subtract(self.white, self.black, -1, 255) maskgray = mask.convert("L") #out = self.black.convert("RGBA") # this doesn't premultiply alpha correctly? #this better? def divide(ch): env = {"val": ch, "mask": maskgray} return ImageMath.eval("val*255/mask", env).convert("L") out = channelMap(divide, self.black).convert("RGBA") out.putalpha(maskgray) return out
def image_difference(im1, im2, threshold): px1 = im1.load() px2 = im2.load() if im1.size[0] > im2.size[0] or im1.size[1] > im1.size[1]: print "error: first image too small" return False diff = 0 for x in range(im1.size[0]): for y in range(im2.size[1]): p1 = px1[x, y] p2 = px2[x, y] for i in range(3): diff += abs(p1[i] - p2[i]) diffImage = ImageChops.subtract(im1, im2, 0.5, 128) diffImage.save('geodots-diff.png') if diff >= threshold: print "error: image difference too large (", diff, ")" return False return True
def saturation(image, amount=50): """Adjust brightness from black to white - amount: -1(black) 0 (unchanged) 1(white) - repeat: how many times it should be repeated""" if amount == 0: return image amount /= 100.0 grey = image.convert("L").convert(image.mode) if amount < 0: #grayscale im = imtools.blend(image, grey, -amount) else: #overcolored = Image - (alpha * Grey) / (1 - alpha) alpha = 0.7 alpha_g = grey.point(lambda x: x * alpha) i_minus_alpha_g = ImageChops.subtract(image, alpha_g) overcolored = i_minus_alpha_g.point(lambda x: x / (1 - alpha)) im = imtools.blend(image, overcolored, amount) #fix image transparency mask if image.mode == 'RGBA': im.putalpha(imtools.get_alpha(image)) return im
if f.rpartition(".")[2] == "jpg": lista.append(f) lista.sort() print(lista) #create a new list for selected frames lista2 = [] #start substracting frames in order image1 = Image.open(lista[0]) for i in range(len(lista)-1): try: diferente = True image2= Image.open(lista[i+1]) dif = ImageChops.subtract(image1, image2) # number of pixels in the image (width x height) pixels = dif.size[0]*dif.size[1] #[0][256]&[512] are black values from R G and B channel, all 3 must be equal to the # number of pixels in the image for the image to be full black if dif.histogram()[0] == dif.histogram()[256] == dif.histogram()[512] == pixels: diferente = False if not diferente: print("same frame") else: print("diferent frame: ",image1.filename, image2.filename) lista2.append(image1) image1 = Image.open(lista[i+1]) except IOError:
def subtract_polygon_from_image(polygon, image): '''Sets all pixels inside the polygon to 0''' polygon = polygon2image(polygon, image.size, 'RGB') image = ImageChops.subtract(image, polygon) return image
import Image, ImageChops import numpy as np hr_flist = 'flist/hr.flist' lr_flist = 'flist/lrX2.flist' res_flist = 'flist/lrX2res.flist' with open(hr_flist) as f: hr_filename_list = f.read().splitlines() with open(lr_flist) as f: lr_filename_list = f.read().splitlines() with open(res_flist) as f: res_filename_list = f.read().splitlines() for hr_filename, lr_filename, res_filename in zip(hr_filename_list, lr_filename_list, res_filename_list): hr_image = Image.open(hr_filename) lr_image = Image.open(lr_filename) lr_image = lr_image.resize(hr_image.size, Image.ANTIALIAS) lr_image = ImageChops.subtract(hr_image, lr_image, 1, 127) lr_image.save(res_filename)