Skip to content

Instantly share code, notes, and snippets.

@delta2golf
delta2golf / svg2png.py
Created January 16, 2022 02:18 — forked from thomir/svg2png.py
Various ways to convert SVG -> PNG
#!/usr/bin/env python
"""Convert an SVG file to a PNG file."""
from argparse import ArgumentParser
import subprocess
import os.path
def main():
@delta2golf
delta2golf / polygon_mask.py
Created January 16, 2022 01:49
create a bitmap mask from a shapely polygon
import numpy
import cairo
def polygon_mask(polygon):
assert polygon.geom_type == 'Polygon'
minx, miny, maxx, maxy = polygon.bounds
minx, miny = numpy.floor([minx, miny]).astype(numpy.int32)
maxx, maxy = numpy.ceil([maxx, maxy]).astype(numpy.int32)
@delta2golf
delta2golf / threshold_to_polygon.py
Created January 12, 2022 19:35 — forked from rocreguant/threshold_to_polygon.py
Function to transform cv2 thesholdiag (black and white / binary image) to a group of polygons
import rasterio
from rasterio import features
import shapely
from shapely.geometry import Point, Polygon
def mask_to_polygons_layer(mask):
all_polygons = []
for shape, value in features.shapes(mask.astype(np.int16), mask=(mask >0), transform=rasterio.Affine(1.0, 0, 0, 0, 1.0, 0)):
return shapely.geometry.shape(shape)
all_polygons.append(shapely.geometry.shape(shape))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import numpy as np
def outline_to_mask(line, x, y):
"""Create mask from outline contour
Parameters
----------
line: array-like (N, 2)
x, y: 1-D grid coordinates (input for meshgrid)
@delta2golf
delta2golf / png_to_polygon_to_geojson.py
Created January 11, 2022 16:54 — forked from urschrei/png_to_polygon_to_geojson.py
Read a PNG into a numpy array, convert it to a Shapely Polygon, and dump it as GeoJSON
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created by Stephan Hügel on 2017-03-02
The MIT License (MIT)
Copyright (c) 2017 Stephan Hügel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@delta2golf
delta2golf / combine.py
Created November 25, 2017 15:34 — forked from glombard/combine.py
Merging 4 images into one with Python and PIL/Pillow
# Combine multiple images into one.
#
# To install the Pillow module on Mac OS X:
#
# $ xcode-select --install
# $ brew install libtiff libjpeg webp little-cms2
# $ pip install Pillow
#
from __future__ import print_function
#!/usr/bin/python
#
# Delete the label image from an Aperio SVS file.
#
# Copyright (c) 2012-2013 Carnegie Mellon University
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, version 2.1.
@delta2golf
delta2golf / full-page-screenshots-selenium-chrome.rb
Created September 10, 2016 00:35 — forked from elcamino/full-page-screenshots-selenium-chrome.rb
How to take full-page screenshots with Selenium and Google Chrome in Ruby
#!/usr/bin/env ruby
require 'selenium-webdriver'
wd = Selenium::WebDriver.for :remote, url: 'http://10.3.1.7:4444/wd/hub', desired_capabilities: :chrome
wd.navigate.to 'https://snipt.net/restrada/python-selenium-workaround-for-full-page-screenshot-using-chromedriver-2x/'
# Get the actual page dimensions using javascript
#
width = wd.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")