def __copy_transparent(self, image): ''' Creates a semi-transparent copy of the given image ''' b = Bitmap( image.Width, image.Height ) g = Graphics.FromImage(b) cm = ColorMatrix() cm.Matrix33 = 0.3 ia = ImageAttributes() ia.SetColorMatrix(cm) g.DrawImage(image, Rectangle(0,0, image.Width, image.Height), 0,0,\ image.Width, image.Height, GraphicsUnit.Pixel, ia) return b
def take_screenshot(as_bytes=False): bounds = Screen.PrimaryScreen.Bounds screenshot = Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb) graphics = Graphics.FromImage(screenshot) graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size) if as_bytes: fp = MemoryStream() screenshot.Save(fp, ImageFormat.Png) return bytes(bytearray(fp.ToArray())) else: return screenshot
def pick(snip=False): snipper = PickSnipTool(PickSnipTool.take_screenshot(), snip=snip) while True: result = snipper.ShowDialog() if result in [DialogResult.OK, DialogResult.Cancel]: break if snipper.mouse_down_seconds == 1: Mouse.Instance.Click(snipper.mouse_down) time.sleep(0.5) snipper.BackgroundImage = PickSnipTool.take_screenshot() if result == DialogResult.OK and snip: if snipper.snip_rectangle.Width and snipper.snip_rectangle.Height: img = Bitmap(snipper.snip_rectangle.Width, snipper.snip_rectangle.Height) gr = Graphics.FromImage(img) gr.DrawImage( snipper.BackgroundImage, Rectangle(0, 0, img.Width, img.Height), snipper.snip_rectangle, GraphicsUnit.Pixel, ) fp = MemoryStream() img.Save(fp, ImageFormat.Png) return {"bytes": bytes(bytearray(fp.ToArray()))} return {} elif result == DialogResult.OK: if (snipper.mouse_down_seconds and snipper.mouse_down_seconds <= snipper.click_timeout): Mouse.Instance.Click(snipper.mouse_down) time.sleep(0.5) el = AutomationElement.FromPoint(snipper.mouse_up) result = { prop.ProgrammaticName.split(".", 1)[-1]: el.GetCurrentPropertyValue(prop) for prop in el.GetSupportedProperties() } result.update({ "NameProperty": el.GetCurrentPropertyValue(el.NameProperty), "ControlTypeProperty": el.GetCurrentPropertyValue( el.ControlTypeProperty).ProgrammaticName.split(".", 1)[-1], "AutomationIdProperty": el.GetCurrentPropertyValue(el.AutomationIdProperty), }) return result else: return {}
def ScreenCapture(x, y, width, height): hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()) hdcDest = GDI32.CreateCompatibleDC(hdcSrc) hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height) GDI32.SelectObject(hdcDest, hBitmap) # 0x00CC0020 is the magic number for a copy raster operation GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, 0x00CC0020) result = Bitmap(Image.FromHbitmap(hBitmap)) User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc) GDI32.DeleteDC(hdcDest) GDI32.DeleteObject(hBitmap) return result
def __perceptual_hash(image): ''' Returns a 'perceptual' image hash for the given Image. ''' if image is not None: SIZE = 8 # create ImageAttributes for converting image to greyscale # see: http://tech.pro/tutorial/660/ # csharp-tutorial-convert-a-color-image-to-grayscale attr = ImageAttributes() attr.SetColorMatrix( ColorMatrix(Array[Array[Single]](( \ (0.3, 0.3, 0.3, 0.0, 0.0), (.59, .59, .59, 0.0, 0.0), (.11, .11, .11, 0.0, 0.0), (0.0, 0.0, 0.0, 1.0, 0.0), (0.0, 0.0, 0.0, 0.0, 1.0) ))) ) with Bitmap(SIZE, SIZE, PixelFormat.Format64bppArgb) as small_image: with Graphics.FromImage(small_image) as g: # draw image in greyscale in a tiny square # see: https://www.memonic.com/user/aengus/folder/coding/id/1qVeq g.CompositingQuality = CompositingQuality.HighQuality g.SmoothingMode = SmoothingMode.HighQuality g.InterpolationMode = InterpolationMode.HighQualityBicubic g.DrawImage(image, Rectangle(0, 0, SIZE, SIZE), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr) # convert image pixels into bits, where 1 means pixel is greater # than image average, and 0 means pixel is less than average. # return bits as a single long value pixels = [ small_image.GetPixel(x, y).R for x in range(SIZE) for y in range(SIZE) ] average = reduce(lambda x, y: x + y, pixels) / float( len(pixels)) bits = map(lambda x: 1 if x > average else 0, pixels) return reduce(lambda x, (i, val): x | (val << i), enumerate(bits), 0) else: return long(0)
def DrawResizedImage(self, size): # load original image origImage = Bitmap.FromFile(self.filename, False) # calculate if self.__dims is None: self.__dims = (origImage.Width, origImage.Height) width, height = self.CalcResizedDims(size) # draw new image newImage = Bitmap(width, height) g = Graphics.FromImage(newImage) g.InterpolationMode = InterpolationMode.HighQualityBicubic g.DrawImage(origImage, 0, 0, width, height) imageBytes = BitmapToBytes(newImage) # cleanup g.Dispose() newImage.Dispose() origImage.Dispose() return imageBytes
def __drawFolderThumbnail(self, parent): size = ThumbnailSize # create new image newImage = Bitmap(size, size) g = Graphics.FromImage(newImage) g.InterpolationMode = InterpolationMode.HighQualityBicubic # draw background if parent: bc = ParentFolderColor else: bc = ChildFolderColor b = LinearGradientBrush(Point(0, 0), Point(size, size), bc, Color.GhostWhite) g.FillRectangle(b, 0, 0, size, size) b.Dispose() g.DrawRectangle(Pens.LightGray, 0, 0, size - 1, size - 1) # draw up to 4 subitems folderItems = self.GetFirstFolderItems(4) delta = 10 side = (size - 3 * delta) / 2 - 1 rects = (Rectangle(delta + 3, delta + 12, side, side), Rectangle(size / 2 + delta / 2 - 3, delta + 12, side, side), Rectangle(delta + 3, size / 2 + delta / 2 + 6, side, side), Rectangle(size / 2 + delta / 2 - 3, size / 2 + delta / 2 + 6, side, side)) for rect, item in zip(rects, folderItems): subImage = Bitmap.FromStream(MemoryStream(item.thumbnail()), False) g.DrawImage(subImage, rect) subImage.Dispose() for rect in rects: g.DrawRectangle(Pens.LightGray, rect) # draw folder name if parent: name = '[..]' else: name = Path.GetFileName(self.path) f = Font('Arial', 10) g.DrawString(name, f, Brushes.Black, RectangleF(2, 2, size - 2, size - 2)) f.Dispose() # get the bytes of the image imageBytes = BitmapToBytes(newImage) # cleanup g.Dispose() newImage.Dispose() return imageBytes
def __init__(self, moduleManager, contents): ListBox.__init__(self) self.moduleManager = moduleManager # behaviour self.LabelEdit = False self.MultiSelect = False # appearance self.Dock = DockStyle.Fill self.ScrollAlwaysVisible = True self.TabIndex = 0 self.HideSelection = False # Keep selected item grey when lost focus self.textBrush = SolidBrush(SystemColors.WindowText) self.icon = Bitmap(UIGlobal.ModuleIcon) self.DrawMode = DrawMode.OwnerDrawVariable self.DrawItem += self.OnDrawItem self.MeasureItem += self.OnMeasureItem self.UpdateAllItems(contents)
def __drawPictureThumbnail(self): # load original image origImage = Bitmap.FromFile(self.filename, False) # calculate size = ThumbnailSize if self.__dims is None: self.__dims = (origImage.Width, origImage.Height) width, height = self.CalcResizedDims(size) drawWidth, drawHeight = width, height width, height = max(width, size), max(height, size) drawXOffset, drawYOffset = (width-drawWidth)/2, (height-drawHeight)/2 # draw new image newImage = Bitmap(width, height) g = Graphics.FromImage(newImage) g.InterpolationMode = InterpolationMode.HighQualityBicubic g.FillRectangle(Brushes.GhostWhite, 0, 0, width, height) g.DrawRectangle(Pens.LightGray, 0, 0, width-1, height-1) g.DrawImage(origImage, drawXOffset, drawYOffset, drawWidth, drawHeight) imageBytes = BitmapToBytes(newImage) # cleanup g.Dispose() newImage.Dispose() origImage.Dispose() return imageBytes
for i in urlList: bitmaps = [] for j in i: request = WebRequest.Create(j) request.Accept = "text/html" request.UserAgent = "Mozilla/5.0" response = request.GetResponse() bitmaps.append(Image.FromStream(response.GetResponseStream())) bitmaps2.append(bitmaps) combinedBitmaps = [] for a,b,c in zip(bitmaps2,UniqueTileColumns,UniqueTileRows): TotalWidth = len(b)*PixelWidth TotalHeight = len(c)*PixelWidth img = Bitmap(TotalWidth,TotalHeight) g = Graphics.FromImage(img) LPx = [] n = 0 for l in j: LPx.append(n*PixelWidth) n=n+1 LPy = [] n = 0 for i in c: LPy.append(n*PixelWidth) n=n+1 LPx2=[] n=len(LPy)
def on_print(sender, event): # Get screenshot bmp = Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, event.Graphics) event.Graphics.CopyFromScreen(0, 0, 0, 0, bmp.Size)
def __init__(self, file): self._file = file self._bitmap = Bitmap(file)