Home > OS >  How do I crop a python array to maximum size with only non-zero values (largest non-zero rectangle)
How do I crop a python array to maximum size with only non-zero values (largest non-zero rectangle)

Time:12-03

I have a numpy array of pixel data, something like

0 0 0 0 0 0 0
0 1 3 4 6 1 0
0 2 3 5 2 1 0
0 1 0 0 1 0 0
0 0 0 0 0 0 0

I would like to get a new array which excludes any outer rows/columns with zeroes, so I just end up with only the non-zero values (that works for any given array) i.e.

1 3 4 6 1
2 3 5 2 1

So far all I've managed to get is

1 3 4 6 1
2 3 5 2 1
1 0 0 1 0

using np.argwhere to find the "min" and "max" non-zero values, but this still includes rows/columns with zero and non-zero values in.

My actual array:

0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   1872    1803    1731    1766    1816    1843    1706    1768    1815    1741    1846    1857    1731    1745    1842    1720    1769    1853    1764    1776    1816    1773    1793    1767    1830    1791    1835    1823    1762    1832    1763    1762    1779    1901    1872    1819    1862    1802    1726    1788    1847    1785    1796    1773    1800    1742    1873    1830    1869    1832    1809    1861    1702    1808    1709    1774    1765    0   0
0   0   1937    1746    1790    1750    1862    1898    1770    1727    1868    1895    1761    1800    1814    1826    1836    1774    1847    1868    1837    1746    1809    1869    1818    1760    1940    1844    1845    1833    1815    1872    1773    1816    1769    1860    1841    1856    1857    1779    1779    1822    1781    1778    1858    1727    1816    1835    1835    1864    1793    1781    1908    1820    1803    1838    1685    1814    1756    0   0
0   0   1754    1895    1806    1818    1829    1733    1865    1903    1764    1850    1847    1913    1856    1757    1782    1826    1818    1875    1843    1777    1716    1825    1761    1842    1843    1925    1791    1879    1887    1873    1789    1769    1805    1915    1825    1829    1817    1840    1882    1762    1840    1878    1830    1862    1789    1884    1798    1802    1847    1875    1825    1773    1803    1850    1817    1885    1792    0   0
0   0   1773    1830    1797    1878    1758    1897    1813    1836    1835    1960    1841    1807    1788    1799    1839    1834    1792    1855    1785    1912    1824    1845    1831    1902    1879    1869    1793    1901    1801    1881    1871    1786    1851    1879    1822    1829    1951    1873    1778    1769    1941    1805    1826    1892    1869    1783    1895    1799    1800    1973    1829    1869    1903    1858    1806    1837    1817    0   0
0   0   1828    1858    1793    1833    1894    1832    1763    1892    1786    1893    1883    1846    1828    1821    1875    1864    1778    1863    1832    1801    1798    1871    1753    1899    1892    1901    1907    1877    1756    1865    1899    1874    1841    1775    1838    1817    1864    1798    1843    1803    1853    1878    1831    1855    1803    1816    1885    1818    1882    1859    1790    1892    1826    1906    1842    1831    1754    0   0
0   0   1811    1831    1837    1828    1792    1768    1818    1797    1766    1924    1849    1921    1881    1795    1883    1954    1811    1804    2006    1849    1841    1808    1867    1918    1755    1765    1881    1852    1930    1848    1807    1876    1776    1790    1849    1855    1942    1871    1908    1822    1810    1794    1889    1780    1857    1879    1845    1858    1901    1839    1744    1743    1811    1853    1841    1854    1864    0   0
0   0   1880    1888    1874    1878    1888    1868    1852    1887    1875    1874    1892    1828    1842    1822    1789    1870    1829    1841    1864    1859    1846    1776    1799    1875    1875    1811    1873    1837    1921    1917    1777    1840    1872    1816    1878    1890    1821    1925    1810    1945    1884    1845    1859    1843    1806    1894    1886    1886    1885    1931    1761    1819    1889    1765    1891    1896    1824    0   0
0   0   1856    1827    1826    1882    1786    1852    1820    1880    1912    1795    1854    1868    1899    1855    1886    1894    1891    1907    1907    1713    1800    1922    1831    1814    1894    1851    1927    1879    1881    1884    1932    1904    1807    1839    1851    1885    1889    1913    1878    1754    1930    1905    1915    1825    1901    1870    1839    1867    1897    1862    1843    1836    1774    1764    1838    1829    1876    0   0
0   0   1858    1840    1897    1884    1861    1910    1860    1879    1882    1860    1831    1828    1846    1820    1889    1830    1852    1880    1842    1917    1872    1839    1820    1888    1871    1838    1817    1939    1905    1890    1832    1925    1780    1862    1793    1887    1836    1846    1852    1939    1922    1874    1865    1890    1864    1863    1918    1819    1861    1851    1854    1886    1898    1888    1796    1917    1754    0   0
0   0   1891    1852    1926    1803    1863    1814    1849    1857    1870    1882    1979    1786    1880    1820    1812    1863    1922    1916    1851    1879    1827    1859    1913    1843    1852    1823    1812    1891    1932    1887    1883    1975    1769    1831    1859    1954    1780    1829    1853    1754    1832    1733    1886    1800    1808    1879    1821    1934    1897    1822    1941    1863    1818    1826    1883    1894    1928    0   0
0   0   1829    1820    1899    1869    1864    1863    1895    1923    1839    1804    1884    1835    1859    1872    1825    1841    1817    1817    1832    1882    1878    1854    1867    1917    1843    1928    1949    1859    1929    1938    1826    1808    1823    1872    1865    1811    1908    1848    1861    1926    1799    1825    1799    1859    1957    1848    1863    1846    1806    1934    1845    1899    1827    1881    1836    1806    1798    0   0
0   0   1794    1914    1880    1892    1849    1862    1819    1927    1873    1886    1857    1907    1840    1897    1857    1867    1925    1972    1871    1975    1854    1843    1856    1872    1875    1927    1819    1905    1948    1881    1904    1832    1863    1854    1811    1869    1797    1946    1805    1779    1824    1919    1886    1817    1845    1844    1909    1885    1900    1826    1867    1817    1833    1870    1888    1879    1875    0   0
0   0   1930    1857    1851    1862    1907    1924    1838    1833    1858    1847    1892    1788    1902    1786    1880    1818    1896    1938    1953    1952    1903    1723    1867    1955    1859    1869    1890    1830    1864    1837    1806    1827    1872    1868    1907    1977    1878    1895    1786    1892    1897    1872    1927    1807    1854    1865    1911    1957    1816    1833    1904    1897    1764    1895    1854    1800    1825    0   0
0   0   1889    1837    1887    1885    1865    1863    1779    1883    1815    1807    1856    1788    1857    1842    1812    1838    1949    1887    1909    1843    1848    1901    1812    1890    1882    1873    1835    1870    1855    1846    1811    1899    1855    1826    1916    1781    1887    1882    1887    1826    1848    1855    1804    1859    1827    1802    1884    1920    1920    1876    1839    1835    1822    1868    1844    1796    1813    0   0
0   0   1845    1883    1857    1790    1738    1915    1963    1899    1878    1890    1813    1779    1836    1832    1895    1863    1874    1899    1946    1851    1967    1816    1860    1860    1793    1852    1917    1904    1879    1911    1747    1939    1938    1849    1917    1894    1845    1895    1877    1903    1870    1868    1878    1857    1921    1858    1843    1800    1930    1820    1752    1827    1885    1927    1902    1842    1857    0   0
0   0   1916    1898    1929    1884    1981    1866    1940    1978    1848    1903    1935    1843    1817    1944    1871    1862    1917    1876    1920    1921    1789    1881    1938    1793    1906    1912    1854    1904    1855    1901    1877    1814    1894    1907    1894    1828    1839    1980    1805    1878    1861    1808    1885    1854    1958    1863    1756    1922    1898    1808    1822    1864    1916    1855    1919    1896    1857    0   0
0   0   1961    1800    1897    1857    1791    1823    1925    1827    1894    1911    1836    1826    1888    1854    1753    1841    1900    1859    1807    1910    1902    1908    1902    1920    1901    1951    1944    1920    1897    1889    1880    1873    1836    1886    1930    1856    1984    1935    1834    1926    1868    1932    1876    1891    1796    1814    1807    1824    1852    1888    1870    1911    1834    1845    1854    1863    1818    0   0
0   0   1885    1947    1836    1886    1803    1982    1901    1939    1930    1876    1832    1888    1886    1855    1845    1910    1877    1836    1910    1888    1904    1905    1859    1899    1834    1879    1893    1861    1896    1931    1855    1890    1964    1939    1798    1894    1844    1913    1906    1920    1873    1807    1875    1837    1900    1904    1919    1845    1895    1844    1793    1855    1926    1786    1917    1834    1898    0   0
0   0   1863    1856    1776    1925    1943    1875    1903    1858    1878    1865    1877    1821    1892    1914    1907    1863    1779    1879    1939    1893    1867    1846    1940    1910    1927    1920    1920    1934    1788    1851    1937    1943    1906    1853    1954    1910    1892    1857    1878    1853    1887    1876    1915    1819    1820    1933    1813    1848    1867    1866    1949    1905    1832    1876    1786    1918    1822    0   0
0   0   1897    1880    1904    1942    1886    1894    1887    1946    1881    1855    1924    1866    1905    1846    1960    1854    1878    1979    1908    1933    1868    1920    1938    1805    1882    1879    1850    1862    1889    1872    1900    1903    1856    1862    1862    1959    1886    1856    1910    1912    1847    1939    1884    1885    1798    1885    1825    1903    1837    1900    1825    1837    1845    1807    1890    1843    1834    0   0
0   0   1879    1896    1898    1980    1844    1889    2013    1938    1950    1877    1849    1916    1879    1871    1946    1916    1890    1945    1942    1934    1914    1821    1902    1938    1878    1906    1823    1927    1912    1948    1932    1927    1859    1819    1933    1927    1915    1789    1970    1930    1931    1831    1856    1890    1831    1852    1863    1884    1821    1842    1861    1843    1751    1872    1790    1852    1819    0   0
0   0   1884    1974    1825    1888    1932    1843    1911    1899    1905    1845    1847    1920    1883    1934    1879    1869    1792    2024    1882    1944    1850    1913    1899    1799    1899    1927    1849    1935    1880    1874    1888    1881    1870    1829    1908    1841    1957    1892    2001    1999    1941    1959    1917    1913    1893    1849    1908    1853    1928    1868    1784    1881    1871    1844    1754    1849    1907    0   0
0   0   1890    1898    1845    1922    1950    1938    1868    1915    1907    1858    1825    1867    1933    1921    1933    1820    1865    1851    1947    1903    1869    1871    1837    1941    1892    1833    1817    1856    1863    1884    1909    1875    1904    1943    1916    2001    1887    1858    1837    1875    1846    1824    1913    1831    1891    1901    1818    1908    1921    1864    1898    1869    1829    1733    1815    1824    1861    0   0
0   0   1902    1934    1894    1839    1894    1869    1962    1809    1891    1865    1957    1950    1926    1861    1954    1876    1782    1883    1959    1852    1849    1891    1887    1756    1861    1905    1894    1913    1831    1828    1906    1875    1981    1887    1990    1922    1825    1995    1831    1852    1864    1922    1878    1895    1897    1819    1851    1873    1799    1901    1810    1880    1922    1875    1858    1841    1881    0   0
0   0   1852    1867    1940    1858    1867    1888    1863    1839    1851    1885    1875    1928    1903    1913    1858    1838    1819    1818    1744    1850    1856    1884    1861    1846    1896    1891    1894    1946    1911    1888    1865    1849    1777    1893    2010    1931    1832    1901    1817    1900    1869    1863    1825    1848    1885    1893    1875    1843    1884    1819    1950    1899    1926    1837    1819    1876    1873    0   0
0   0   1872    1871    1884    1844    1847    1935    1859    1858    1894    1866    1930    1741    1919    1854    1855    1866    1833    1860    1875    1852    1976    1835    1811    1994    1897    1833    1891    1904    1938    1906    1802    1875    1861    1835    1939    1870    1877    1972    1949    1880    1881    1795    1792    1764    1945    1978    1875    1887    1861    1890    1832    1794    1873    1919    1797    1876    1842    0   0
0   0   1897    1884    1845    1842    1878    1918    1835    1866    1868    1858    1908    1900    1868    1756    1841    1746    1842    1891    1852    1889    1869    1886    1802    1902    1859    1935    1978    1880    1918    1865    1779    1889    1824    1781    1902    1890    1836    1833    1908    1865    1916    1916    1902    1796    1878    1858    1825    1914    1921    1829    1848    1862    1863    1847    1847    1831    1888    0   0
0   0   1856    1933    1882    1948    1882    2003    1938    1901    1856    1755    1834    1868    1861    1768    1863    1841    1814    1896    1859    1871    1860    1908    1912    1893    1896    1968    1863    1938    1920    1828    1952    1854    1867    1913    1764    1893    1876    1892    1901    1813    1890    1916    1915    1887    1836    1812    1798    1846    1867    1846    1866    1787    1915    1898    1911    1717    1873    0   0
0   0   1877    1885    1868    1858    1932    1949    1835    1849    1898    1867    1911    1902    1926    1859    1818    1941    1836    1816    1940    1908    1886    1818    1899    1948    1870    1845    1887    1925    1891    1823    1885    1844    1795    1886    1879    1865    1841    1830    1902    1946    1803    1889    1893    1856    1816    1853    1813    1851    1897    1852    1827    1918    1834    1859    1738    1808    1796    0   0
0   0   1838    1839    1997    1844    1855    1867    1953    1898    1876    1865    1882    1808    1857    1856    1850    1832    1892    1802    1858    1882    1896    1925    1840    1905    1895    1838    1865    1922    1904    1843    1958    1890    1907    1796    1858    1871    1906    1815    1888    1870    1902    1717    1868    1823    1888    1905    1821    1812    1928    1867    1787    1826    1821    1905    1839    1747    1755    0   0
0   0   1870    1868    1899    1915    1873    1841    1938    1918    1897    1902    1846    1887    1750    1868    1841    1828    1928    1852    1876    1905    1859    1838    1931    1871    1920    1779    1836    1897    1863    1937    1895    1934    1940    1872    1890    1893    1852    1874    1860    1857    1874    1903    1826    1873    1877    1833    1922    1847    1832    1874    1914    1829    1846    1863    1829    1913    1816    0   0
0   0   1887    1888    1924    1880    1818    1878    1842    1908    1947    1914    1848    1867    1868    1891    1874    1872    1900    1828    1905    1865    1925    1965    1868    1893    1864    1869    1868    1867    1863    1946    1822    1883    1863    1817    1948    1846    1843    1826    1832    1793    1825    1802    2014    1967    1832    1895    1848    1833    1914    1817    1898    1798    1910    1865    1862    1856    1855    0   0
0   0   1914    1862    1828    1924    1897    1984    1931    1925    1896    1895    1908    1933    1889    1813    1836    1921    1855    1841    1935    1917    1897    1890    1880    1904    1851    1937    1936    1920    1856    1798    1810    1819    1871    1855    1905    1832    1941    1844    1827    1855    1901    1846    1826    1762    1870    1899    1873    1853    1902    1839    1884    1841    1838    1816    1846    1860    1787    0   0
0   0   1869    1874    1867    1894    1865    1951    1865    1887    1857    1900    1839    1874    1877    1876    1845    1897    1881    1952    1832    1855    1855    1949    1889    1942    1844    1881    1937    1892    1779    1841    1893    1902    1814    1791    1858    1870    1874    1856    1814    1744    1799    1831    1839    1717    1878    1815    1846    1864    1832    1927    1808    1859    1818    1848    1828    1803    1842    0   0
0   0   1871    1884    1842    1834    1873    1884    1950    1911    1992    1847    1847    1834    1849    1809    1822    1927    1925    1835    1857    1891    1848    1833    1843    1939    1858    1871    1975    1816    1874    1915    1835    1918    1906    1902    1849    1863    1909    1798    1842    1910    1791    1843    1781    1832    1898    1889    1884    1853    1883    1855    1975    1767    1826    1761    1879    1814    1738    0   0
0   0   1886    1909    1873    1850    1908    1894    1907    1872    1837    1773    1847    1926    1884    1882    1831    1832    1942    1897    1844    1950    1886    1978    1947    1815    1843    1785    1886    1914    1911    1883    1824    1873    1934    1943    1831    1906    1813    1820    1831    1870    1824    1875    1866    1913    1800    1818    1930    1860    1808    1884    1834    1921    1717    1812    1816    1947    1829    0   0
0   0   1860    1893    1883    1843    1923    1853    1834    1858    1922    1944    1942    1839    1813    1852    1889    1945    1902    1977    1929    1881    1850    1967    1844    1877    1970    1850    1941    1897    1814    1894    1841    1837    1821    1866    1777    1805    1851    1889    1838    1843    1853    1776    1907    1909    1846    1781    1775    1876    1941    1851    1849    1854    1813    1885    1912    1887    1776    0   0
0   0   1819    1896    1911    1936    1887    1847    1874    1894    1855    1869    1843    1864    1921    1883    1875    1926    1866    1923    1886    1889    1844    1896    2002    1944    1909    1858    1927    1870    1882    1886    1899    1894    1809    1904    1786    1920    1908    1888    1901    1859    1857    1793    1880    1828    1809    1839    1905    1893    1849    1920    1837    1868    1910    1850    1873    1900    1721    0   0
0   0   1861    1895    1819    1865    1741    1797    1832    1849    1901    1869    1870    1811    1786    1910    1936    1961    1907    1899    1949    1863    1845    1885    1881    1831    1884    1937    1860    1906    1873    1838    1859    1898    1924    1863    1902    1881    1851    1880    1945    1851    1929    1846    1843    1879    1774    1826    1788    1871    1918    1780    1825    1853    1782    1852    1861    1867    1844    0   0
0   0   1822    1867    1806    1745    1942    1836    1841    1861    1787    1867    1947    1906    1826    1822    1935    1787    1879    1920    1830    1928    1879    1837    1921    1923    1855    1932    1844    1841    1917    1928    1865    1915    1873    1839    1846    1910    1896    1903    1911    1838    1857    1905    1870    1811    1899    1874    1860    1822    1935    1757    1862    1807    1856    1868    1786    1919    1887    0   0
0   0   1850    1926    1855    1766    1858    1815    1894    1861    1911    1910    1846    1861    1857    1800    1837    1784    1912    1937    1916    1942    1929    1866    1905    1916    1923    1922    1899    1838    1910    1872    1778    1849    1863    1868    1870    1828    1880    1793    1889    1937    1857    1888    1882    1946    1841    1838    1800    1819    1874    1918    1879    1895    1874    1884    1861    1761    1800    0   0
0   0   0   1782    0   0   0   0   1879    0   0   0   0   1884    0   0   0   0   0   0   0   1893    0   1932    1909    1938    0   0   0   0   0   1928    0   0   1816    0   0   1921    1887    0   0   0   0   1876    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   0   0   1907    0   0   0   0   1944    0   0   0   0   1954    0   0   0   0   0   0   0   1930    0   1875    1882    1912    0   0   0   0   0   1890    0   0   1875    0   0   1873    1872    0   0   0   0   1897    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

CodePudding user response:

Welcome to StackOverflow!

Input:

[[   0    0    0 ...    0    0    0]
 [   0    0    0 ...    0    0    0]
 [   0    0 1872 ... 1765    0    0]
 ...
 [   0    0 1850 ... 1800    0    0]
 [   0    0    0 ...    0    0    0]
 [   0    0    0 ...    0    0    0]]

Input array.npy

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1872 1803 1731 1766 1816 1843 1706 1768 1815 1741 1846 1857 1731 1745 1842 1720 1769 1853 1764 1776 1816 1773 1793 1767 1830 1791 1835 1823 1762 1832 1763 1762 1779 1901 1872 1819 1862 1802 1726 1788 1847 1785 1796 1773 1800 1742 1873 1830 1869 1832 1809 1861 1702 1808 1709 1774 1765 0 0
0 0 1937 1746 1790 1750 1862 1898 1770 1727 1868 1895 1761 1800 1814 1826 1836 1774 1847 1868 1837 1746 1809 1869 1818 1760 1940 1844 1845 1833 1815 1872 1773 1816 1769 1860 1841 1856 1857 1779 1779 1822 1781 1778 1858 1727 1816 1835 1835 1864 1793 1781 1908 1820 1803 1838 1685 1814 1756 0 0
0 0 1754 1895 1806 1818 1829 1733 1865 1903 1764 1850 1847 1913 1856 1757 1782 1826 1818 1875 1843 1777 1716 1825 1761 1842 1843 1925 1791 1879 1887 1873 1789 1769 1805 1915 1825 1829 1817 1840 1882 1762 1840 1878 1830 1862 1789 1884 1798 1802 1847 1875 1825 1773 1803 1850 1817 1885 1792 0 0
0 0 1773 1830 1797 1878 1758 1897 1813 1836 1835 1960 1841 1807 1788 1799 1839 1834 1792 1855 1785 1912 1824 1845 1831 1902 1879 1869 1793 1901 1801 1881 1871 1786 1851 1879 1822 1829 1951 1873 1778 1769 1941 1805 1826 1892 1869 1783 1895 1799 1800 1973 1829 1869 1903 1858 1806 1837 1817 0 0
0 0 1828 1858 1793 1833 1894 1832 1763 1892 1786 1893 1883 1846 1828 1821 1875 1864 1778 1863 1832 1801 1798 1871 1753 1899 1892 1901 1907 1877 1756 1865 1899 1874 1841 1775 1838 1817 1864 1798 1843 1803 1853 1878 1831 1855 1803 1816 1885 1818 1882 1859 1790 1892 1826 1906 1842 1831 1754 0 0
0 0 1811 1831 1837 1828 1792 1768 1818 1797 1766 1924 1849 1921 1881 1795 1883 1954 1811 1804 2006 1849 1841 1808 1867 1918 1755 1765 1881 1852 1930 1848 1807 1876 1776 1790 1849 1855 1942 1871 1908 1822 1810 1794 1889 1780 1857 1879 1845 1858 1901 1839 1744 1743 1811 1853 1841 1854 1864 0 0
0 0 1880 1888 1874 1878 1888 1868 1852 1887 1875 1874 1892 1828 1842 1822 1789 1870 1829 1841 1864 1859 1846 1776 1799 1875 1875 1811 1873 1837 1921 1917 1777 1840 1872 1816 1878 1890 1821 1925 1810 1945 1884 1845 1859 1843 1806 1894 1886 1886 1885 1931 1761 1819 1889 1765 1891 1896 1824 0 0
0 0 1856 1827 1826 1882 1786 1852 1820 1880 1912 1795 1854 1868 1899 1855 1886 1894 1891 1907 1907 1713 1800 1922 1831 1814 1894 1851 1927 1879 1881 1884 1932 1904 1807 1839 1851 1885 1889 1913 1878 1754 1930 1905 1915 1825 1901 1870 1839 1867 1897 1862 1843 1836 1774 1764 1838 1829 1876 0 0
0 0 1858 1840 1897 1884 1861 1910 1860 1879 1882 1860 1831 1828 1846 1820 1889 1830 1852 1880 1842 1917 1872 1839 1820 1888 1871 1838 1817 1939 1905 1890 1832 1925 1780 1862 1793 1887 1836 1846 1852 1939 1922 1874 1865 1890 1864 1863 1918 1819 1861 1851 1854 1886 1898 1888 1796 1917 1754 0 0
0 0 1891 1852 1926 1803 1863 1814 1849 1857 1870 1882 1979 1786 1880 1820 1812 1863 1922 1916 1851 1879 1827 1859 1913 1843 1852 1823 1812 1891 1932 1887 1883 1975 1769 1831 1859 1954 1780 1829 1853 1754 1832 1733 1886 1800 1808 1879 1821 1934 1897 1822 1941 1863 1818 1826 1883 1894 1928 0 0
0 0 1829 1820 1899 1869 1864 1863 1895 1923 1839 1804 1884 1835 1859 1872 1825 1841 1817 1817 1832 1882 1878 1854 1867 1917 1843 1928 1949 1859 1929 1938 1826 1808 1823 1872 1865 1811 1908 1848 1861 1926 1799 1825 1799 1859 1957 1848 1863 1846 1806 1934 1845 1899 1827 1881 1836 1806 1798 0 0
0 0 1794 1914 1880 1892 1849 1862 1819 1927 1873 1886 1857 1907 1840 1897 1857 1867 1925 1972 1871 1975 1854 1843 1856 1872 1875 1927 1819 1905 1948 1881 1904 1832 1863 1854 1811 1869 1797 1946 1805 1779 1824 1919 1886 1817 1845 1844 1909 1885 1900 1826 1867 1817 1833 1870 1888 1879 1875 0 0
0 0 1930 1857 1851 1862 1907 1924 1838 1833 1858 1847 1892 1788 1902 1786 1880 1818 1896 1938 1953 1952 1903 1723 1867 1955 1859 1869 1890 1830 1864 1837 1806 1827 1872 1868 1907 1977 1878 1895 1786 1892 1897 1872 1927 1807 1854 1865 1911 1957 1816 1833 1904 1897 1764 1895 1854 1800 1825 0 0
0 0 1889 1837 1887 1885 1865 1863 1779 1883 1815 1807 1856 1788 1857 1842 1812 1838 1949 1887 1909 1843 1848 1901 1812 1890 1882 1873 1835 1870 1855 1846 1811 1899 1855 1826 1916 1781 1887 1882 1887 1826 1848 1855 1804 1859 1827 1802 1884 1920 1920 1876 1839 1835 1822 1868 1844 1796 1813 0 0
0 0 1845 1883 1857 1790 1738 1915 1963 1899 1878 1890 1813 1779 1836 1832 1895 1863 1874 1899 1946 1851 1967 1816 1860 1860 1793 1852 1917 1904 1879 1911 1747 1939 1938 1849 1917 1894 1845 1895 1877 1903 1870 1868 1878 1857 1921 1858 1843 1800 1930 1820 1752 1827 1885 1927 1902 1842 1857 0 0
0 0 1916 1898 1929 1884 1981 1866 1940 1978 1848 1903 1935 1843 1817 1944 1871 1862 1917 1876 1920 1921 1789 1881 1938 1793 1906 1912 1854 1904 1855 1901 1877 1814 1894 1907 1894 1828 1839 1980 1805 1878 1861 1808 1885 1854 1958 1863 1756 1922 1898 1808 1822 1864 1916 1855 1919 1896 1857 0 0
0 0 1961 1800 1897 1857 1791 1823 1925 1827 1894 1911 1836 1826 1888 1854 1753 1841 1900 1859 1807 1910 1902 1908 1902 1920 1901 1951 1944 1920 1897 1889 1880 1873 1836 1886 1930 1856 1984 1935 1834 1926 1868 1932 1876 1891 1796 1814 1807 1824 1852 1888 1870 1911 1834 1845 1854 1863 1818 0 0
0 0 1885 1947 1836 1886 1803 1982 1901 1939 1930 1876 1832 1888 1886 1855 1845 1910 1877 1836 1910 1888 1904 1905 1859 1899 1834 1879 1893 1861 1896 1931 1855 1890 1964 1939 1798 1894 1844 1913 1906 1920 1873 1807 1875 1837 1900 1904 1919 1845 1895 1844 1793 1855 1926 1786 1917 1834 1898 0 0
0 0 1863 1856 1776 1925 1943 1875 1903 1858 1878 1865 1877 1821 1892 1914 1907 1863 1779 1879 1939 1893 1867 1846 1940 1910 1927 1920 1920 1934 1788 1851 1937 1943 1906 1853 1954 1910 1892 1857 1878 1853 1887 1876 1915 1819 1820 1933 1813 1848 1867 1866 1949 1905 1832 1876 1786 1918 1822 0 0
0 0 1897 1880 1904 1942 1886 1894 1887 1946 1881 1855 1924 1866 1905 1846 1960 1854 1878 1979 1908 1933 1868 1920 1938 1805 1882 1879 1850 1862 1889 1872 1900 1903 1856 1862 1862 1959 1886 1856 1910 1912 1847 1939 1884 1885 1798 1885 1825 1903 1837 1900 1825 1837 1845 1807 1890 1843 1834 0 0
0 0 1879 1896 1898 1980 1844 1889 2013 1938 1950 1877 1849 1916 1879 1871 1946 1916 1890 1945 1942 1934 1914 1821 1902 1938 1878 1906 1823 1927 1912 1948 1932 1927 1859 1819 1933 1927 1915 1789 1970 1930 1931 1831 1856 1890 1831 1852 1863 1884 1821 1842 1861 1843 1751 1872 1790 1852 1819 0 0
0 0 1884 1974 1825 1888 1932 1843 1911 1899 1905 1845 1847 1920 1883 1934 1879 1869 1792 2024 1882 1944 1850 1913 1899 1799 1899 1927 1849 1935 1880 1874 1888 1881 1870 1829 1908 1841 1957 1892 2001 1999 1941 1959 1917 1913 1893 1849 1908 1853 1928 1868 1784 1881 1871 1844 1754 1849 1907 0 0
0 0 1890 1898 1845 1922 1950 1938 1868 1915 1907 1858 1825 1867 1933 1921 1933 1820 1865 1851 1947 1903 1869 1871 1837 1941 1892 1833 1817 1856 1863 1884 1909 1875 1904 1943 1916 2001 1887 1858 1837 1875 1846 1824 1913 1831 1891 1901 1818 1908 1921 1864 1898 1869 1829 1733 1815 1824 1861 0 0
0 0 1902 1934 1894 1839 1894 1869 1962 1809 1891 1865 1957 1950 1926 1861 1954 1876 1782 1883 1959 1852 1849 1891 1887 1756 1861 1905 1894 1913 1831 1828 1906 1875 1981 1887 1990 1922 1825 1995 1831 1852 1864 1922 1878 1895 1897 1819 1851 1873 1799 1901 1810 1880 1922 1875 1858 1841 1881 0 0
0 0 1852 1867 1940 1858 1867 1888 1863 1839 1851 1885 1875 1928 1903 1913 1858 1838 1819 1818 1744 1850 1856 1884 1861 1846 1896 1891 1894 1946 1911 1888 1865 1849 1777 1893 2010 1931 1832 1901 1817 1900 1869 1863 1825 1848 1885 1893 1875 1843 1884 1819 1950 1899 1926 1837 1819 1876 1873 0 0
0 0 1872 1871 1884 1844 1847 1935 1859 1858 1894 1866 1930 1741 1919 1854 1855 1866 1833 1860 1875 1852 1976 1835 1811 1994 1897 1833 1891 1904 1938 1906 1802 1875 1861 1835 1939 1870 1877 1972 1949 1880 1881 1795 1792 1764 1945 1978 1875 1887 1861 1890 1832 1794 1873 1919 1797 1876 1842 0 0
0 0 1897 1884 1845 1842 1878 1918 1835 1866 1868 1858 1908 1900 1868 1756 1841 1746 1842 1891 1852 1889 1869 1886 1802 1902 1859 1935 1978 1880 1918 1865 1779 1889 1824 1781 1902 1890 1836 1833 1908 1865 1916 1916 1902 1796 1878 1858 1825 1914 1921 1829 1848 1862 1863 1847 1847 1831 1888 0 0
0 0 1856 1933 1882 1948 1882 2003 1938 1901 1856 1755 1834 1868 1861 1768 1863 1841 1814 1896 1859 1871 1860 1908 1912 1893 1896 1968 1863 1938 1920 1828 1952 1854 1867 1913 1764 1893 1876 1892 1901 1813 1890 1916 1915 1887 1836 1812 1798 1846 1867 1846 1866 1787 1915 1898 1911 1717 1873 0 0
0 0 1877 1885 1868 1858 1932 1949 1835 1849 1898 1867 1911 1902 1926 1859 1818 1941 1836 1816 1940 1908 1886 1818 1899 1948 1870 1845 1887 1925 1891 1823 1885 1844 1795 1886 1879 1865 1841 1830 1902 1946 1803 1889 1893 1856 1816 1853 1813 1851 1897 1852 1827 1918 1834 1859 1738 1808 1796 0 0
0 0 1838 1839 1997 1844 1855 1867 1953 1898 1876 1865 1882 1808 1857 1856 1850 1832 1892 1802 1858 1882 1896 1925 1840 1905 1895 1838 1865 1922 1904 1843 1958 1890 1907 1796 1858 1871 1906 1815 1888 1870 1902 1717 1868 1823 1888 1905 1821 1812 1928 1867 1787 1826 1821 1905 1839 1747 1755 0 0
0 0 1870 1868 1899 1915 1873 1841 1938 1918 1897 1902 1846 1887 1750 1868 1841 1828 1928 1852 1876 1905 1859 1838 1931 1871 1920 1779 1836 1897 1863 1937 1895 1934 1940 1872 1890 1893 1852 1874 1860 1857 1874 1903 1826 1873 1877 1833 1922 1847 1832 1874 1914 1829 1846 1863 1829 1913 1816 0 0
0 0 1887 1888 1924 1880 1818 1878 1842 1908 1947 1914 1848 1867 1868 1891 1874 1872 1900 1828 1905 1865 1925 1965 1868 1893 1864 1869 1868 1867 1863 1946 1822 1883 1863 1817 1948 1846 1843 1826 1832 1793 1825 1802 2014 1967 1832 1895 1848 1833 1914 1817 1898 1798 1910 1865 1862 1856 1855 0 0
0 0 1914 1862 1828 1924 1897 1984 1931 1925 1896 1895 1908 1933 1889 1813 1836 1921 1855 1841 1935 1917 1897 1890 1880 1904 1851 1937 1936 1920 1856 1798 1810 1819 1871 1855 1905 1832 1941 1844 1827 1855 1901 1846 1826 1762 1870 1899 1873 1853 1902 1839 1884 1841 1838 1816 1846 1860 1787 0 0
0 0 1869 1874 1867 1894 1865 1951 1865 1887 1857 1900 1839 1874 1877 1876 1845 1897 1881 1952 1832 1855 1855 1949 1889 1942 1844 1881 1937 1892 1779 1841 1893 1902 1814 1791 1858 1870 1874 1856 1814 1744 1799 1831 1839 1717 1878 1815 1846 1864 1832 1927 1808 1859 1818 1848 1828 1803 1842 0 0
0 0 1871 1884 1842 1834 1873 1884 1950 1911 1992 1847 1847 1834 1849 1809 1822 1927 1925 1835 1857 1891 1848 1833 1843 1939 1858 1871 1975 1816 1874 1915 1835 1918 1906 1902 1849 1863 1909 1798 1842 1910 1791 1843 1781 1832 1898 1889 1884 1853 1883 1855 1975 1767 1826 1761 1879 1814 1738 0 0
0 0 1886 1909 1873 1850 1908 1894 1907 1872 1837 1773 1847 1926 1884 1882 1831 1832 1942 1897 1844 1950 1886 1978 1947 1815 1843 1785 1886 1914 1911 1883 1824 1873 1934 1943 1831 1906 1813 1820 1831 1870 1824 1875 1866 1913 1800 1818 1930 1860 1808 1884 1834 1921 1717 1812 1816 1947 1829 0 0
0 0 1860 1893 1883 1843 1923 1853 1834 1858 1922 1944 1942 1839 1813 1852 1889 1945 1902 1977 1929 1881 1850 1967 1844 1877 1970 1850 1941 1897 1814 1894 1841 1837 1821 1866 1777 1805 1851 1889 1838 1843 1853 1776 1907 1909 1846 1781 1775 1876 1941 1851 1849 1854 1813 1885 1912 1887 1776 0 0
0 0 1819 1896 1911 1936 1887 1847 1874 1894 1855 1869 1843 1864 1921 1883 1875 1926 1866 1923 1886 1889 1844 1896 2002 1944 1909 1858 1927 1870 1882 1886 1899 1894 1809 1904 1786 1920 1908 1888 1901 1859 1857 1793 1880 1828 1809 1839 1905 1893 1849 1920 1837 1868 1910 1850 1873 1900 1721 0 0
0 0 1861 1895 1819 1865 1741 1797 1832 1849 1901 1869 1870 1811 1786 1910 1936 1961 1907 1899 1949 1863 1845 1885 1881 1831 1884 1937 1860 1906 1873 1838 1859 1898 1924 1863 1902 1881 1851 1880 1945 1851 1929 1846 1843 1879 1774 1826 1788 1871 1918 1780 1825 1853 1782 1852 1861 1867 1844 0 0
0 0 1822 1867 1806 1745 1942 1836 1841 1861 1787 1867 1947 1906 1826 1822 1935 1787 1879 1920 1830 1928 1879 1837 1921 1923 1855 1932 1844 1841 1917 1928 1865 1915 1873 1839 1846 1910 1896 1903 1911 1838 1857 1905 1870 1811 1899 1874 1860 1822 1935 1757 1862 1807 1856 1868 1786 1919 1887 0 0
0 0 1850 1926 1855 1766 1858 1815 1894 1861 1911 1910 1846 1861 1857 1800 1837 1784 1912 1937 1916 1942 1929 1866 1905 1916 1923 1922 1899 1838 1910 1872 1778 1849 1863 1868 1870 1828 1880 1793 1889 1937 1857 1888 1882 1946 1841 1838 1800 1819 1874 1918 1879 1895 1874 1884 1861 1761 1800 0 0
0 0 0 1782 0 0 0 0 1879 0 0 0 0 1884 0 0 0 0 0 0 0 1893 0 1932 1909 1938 0 0 0 0 0 1928 0 0 1816 0 0 1921 1887 0 0 0 0 1876 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1907 0 0 0 0 1944 0 0 0 0 1954 0 0 0 0 0 0 0 1930 0 1875 1882 1912 0 0 0 0 0 1890 0 0 1875 0 0 1873 1872 0 0 0 0 1897 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Solution 1:

np_input = np.load('array.npy')

# Remove all zeros from column
np_input = np_input[:, (np_input != 0).any(axis=0)]

# Remove all zeros from row
np_input = np_input[(np_input != 0).any(axis=1)]

# converting to list of lists
np_input = np_input.tolist()

# Remove sub list that contains a zero
np_input = [x for x in np_input if 0 not in x]

# Convert pixles_input to numpy array
final_np = np.array(np_input)

print(final_np)

Solution 2:

np_input = np.load('array.npy')
final_np = np.array([x for x in np_input[:, (np_input != 0).any(axis=0)][(np_input != 0).any(axis=1)].tolist() if 0 not in x])
print(final_np)

Output:

[[1872 1803 1731 ... 1709 1774 1765]
 [1937 1746 1790 ... 1685 1814 1756]
 [1754 1895 1806 ... 1817 1885 1792]
 ...
 [1861 1895 1819 ... 1861 1867 1844]
 [1822 1867 1806 ... 1786 1919 1887]
 [1850 1926 1855 ... 1861 1761 1800]]

Output array.npy

1872 1803 1731 1766 1816 1843 1706 1768 1815 1741 1846 1857 1731 1745 1842 1720 1769 1853 1764 1776 1816 1773 1793 1767 1830 1791 1835 1823 1762 1832 1763 1762 1779 1901 1872 1819 1862 1802 1726 1788 1847 1785 1796 1773 1800 1742 1873 1830 1869 1832 1809 1861 1702 1808 1709 1774 1765
1937 1746 1790 1750 1862 1898 1770 1727 1868 1895 1761 1800 1814 1826 1836 1774 1847 1868 1837 1746 1809 1869 1818 1760 1940 1844 1845 1833 1815 1872 1773 1816 1769 1860 1841 1856 1857 1779 1779 1822 1781 1778 1858 1727 1816 1835 1835 1864 1793 1781 1908 1820 1803 1838 1685 1814 1756
1754 1895 1806 1818 1829 1733 1865 1903 1764 1850 1847 1913 1856 1757 1782 1826 1818 1875 1843 1777 1716 1825 1761 1842 1843 1925 1791 1879 1887 1873 1789 1769 1805 1915 1825 1829 1817 1840 1882 1762 1840 1878 1830 1862 1789 1884 1798 1802 1847 1875 1825 1773 1803 1850 1817 1885 1792
1773 1830 1797 1878 1758 1897 1813 1836 1835 1960 1841 1807 1788 1799 1839 1834 1792 1855 1785 1912 1824 1845 1831 1902 1879 1869 1793 1901 1801 1881 1871 1786 1851 1879 1822 1829 1951 1873 1778 1769 1941 1805 1826 1892 1869 1783 1895 1799 1800 1973 1829 1869 1903 1858 1806 1837 1817
1828 1858 1793 1833 1894 1832 1763 1892 1786 1893 1883 1846 1828 1821 1875 1864 1778 1863 1832 1801 1798 1871 1753 1899 1892 1901 1907 1877 1756 1865 1899 1874 1841 1775 1838 1817 1864 1798 1843 1803 1853 1878 1831 1855 1803 1816 1885 1818 1882 1859 1790 1892 1826 1906 1842 1831 1754
1811 1831 1837 1828 1792 1768 1818 1797 1766 1924 1849 1921 1881 1795 1883 1954 1811 1804 2006 1849 1841 1808 1867 1918 1755 1765 1881 1852 1930 1848 1807 1876 1776 1790 1849 1855 1942 1871 1908 1822 1810 1794 1889 1780 1857 1879 1845 1858 1901 1839 1744 1743 1811 1853 1841 1854 1864
1880 1888 1874 1878 1888 1868 1852 1887 1875 1874 1892 1828 1842 1822 1789 1870 1829 1841 1864 1859 1846 1776 1799 1875 1875 1811 1873 1837 1921 1917 1777 1840 1872 1816 1878 1890 1821 1925 1810 1945 1884 1845 1859 1843 1806 1894 1886 1886 1885 1931 1761 1819 1889 1765 1891 1896 1824
1856 1827 1826 1882 1786 1852 1820 1880 1912 1795 1854 1868 1899 1855 1886 1894 1891 1907 1907 1713 1800 1922 1831 1814 1894 1851 1927 1879 1881 1884 1932 1904 1807 1839 1851 1885 1889 1913 1878 1754 1930 1905 1915 1825 1901 1870 1839 1867 1897 1862 1843 1836 1774 1764 1838 1829 1876
1858 1840 1897 1884 1861 1910 1860 1879 1882 1860 1831 1828 1846 1820 1889 1830 1852 1880 1842 1917 1872 1839 1820 1888 1871 1838 1817 1939 1905 1890 1832 1925 1780 1862 1793 1887 1836 1846 1852 1939 1922 1874 1865 1890 1864 1863 1918 1819 1861 1851 1854 1886 1898 1888 1796 1917 1754
1891 1852 1926 1803 1863 1814 1849 1857 1870 1882 1979 1786 1880 1820 1812 1863 1922 1916 1851 1879 1827 1859 1913 1843 1852 1823 1812 1891 1932 1887 1883 1975 1769 1831 1859 1954 1780 1829 1853 1754 1832 1733 1886 1800 1808 1879 1821 1934 1897 1822 1941 1863 1818 1826 1883 1894 1928
1829 1820 1899 1869 1864 1863 1895 1923 1839 1804 1884 1835 1859 1872 1825 1841 1817 1817 1832 1882 1878 1854 1867 1917 1843 1928 1949 1859 1929 1938 1826 1808 1823 1872 1865 1811 1908 1848 1861 1926 1799 1825 1799 1859 1957 1848 1863 1846 1806 1934 1845 1899 1827 1881 1836 1806 1798
1794 1914 1880 1892 1849 1862 1819 1927 1873 1886 1857 1907 1840 1897 1857 1867 1925 1972 1871 1975 1854 1843 1856 1872 1875 1927 1819 1905 1948 1881 1904 1832 1863 1854 1811 1869 1797 1946 1805 1779 1824 1919 1886 1817 1845 1844 1909 1885 1900 1826 1867 1817 1833 1870 1888 1879 1875
1930 1857 1851 1862 1907 1924 1838 1833 1858 1847 1892 1788 1902 1786 1880 1818 1896 1938 1953 1952 1903 1723 1867 1955 1859 1869 1890 1830 1864 1837 1806 1827 1872 1868 1907 1977 1878 1895 1786 1892 1897 1872 1927 1807 1854 1865 1911 1957 1816 1833 1904 1897 1764 1895 1854 1800 1825
1889 1837 1887 1885 1865 1863 1779 1883 1815 1807 1856 1788 1857 1842 1812 1838 1949 1887 1909 1843 1848 1901 1812 1890 1882 1873 1835 1870 1855 1846 1811 1899 1855 1826 1916 1781 1887 1882 1887 1826 1848 1855 1804 1859 1827 1802 1884 1920 1920 1876 1839 1835 1822 1868 1844 1796 1813
1845 1883 1857 1790 1738 1915 1963 1899 1878 1890 1813 1779 1836 1832 1895 1863 1874 1899 1946 1851 1967 1816 1860 1860 1793 1852 1917 1904 1879 1911 1747 1939 1938 1849 1917 1894 1845 1895 1877 1903 1870 1868 1878 1857 1921 1858 1843 1800 1930 1820 1752 1827 1885 1927 1902 1842 1857
1916 1898 1929 1884 1981 1866 1940 1978 1848 1903 1935 1843 1817 1944 1871 1862 1917 1876 1920 1921 1789 1881 1938 1793 1906 1912 1854 1904 1855 1901 1877 1814 1894 1907 1894 1828 1839 1980 1805 1878 1861 1808 1885 1854 1958 1863 1756 1922 1898 1808 1822 1864 1916 1855 1919 1896 1857
1961 1800 1897 1857 1791 1823 1925 1827 1894 1911 1836 1826 1888 1854 1753 1841 1900 1859 1807 1910 1902 1908 1902 1920 1901 1951 1944 1920 1897 1889 1880 1873 1836 1886 1930 1856 1984 1935 1834 1926 1868 1932 1876 1891 1796 1814 1807 1824 1852 1888 1870 1911 1834 1845 1854 1863 1818
1885 1947 1836 1886 1803 1982 1901 1939 1930 1876 1832 1888 1886 1855 1845 1910 1877 1836 1910 1888 1904 1905 1859 1899 1834 1879 1893 1861 1896 1931 1855 1890 1964 1939 1798 1894 1844 1913 1906 1920 1873 1807 1875 1837 1900 1904 1919 1845 1895 1844 1793 1855 1926 1786 1917 1834 1898
1863 1856 1776 1925 1943 1875 1903 1858 1878 1865 1877 1821 1892 1914 1907 1863 1779 1879 1939 1893 1867 1846 1940 1910 1927 1920 1920 1934 1788 1851 1937 1943 1906 1853 1954 1910 1892 1857 1878 1853 1887 1876 1915 1819 1820 1933 1813 1848 1867 1866 1949 1905 1832 1876 1786 1918 1822
1897 1880 1904 1942 1886 1894 1887 1946 1881 1855 1924 1866 1905 1846 1960 1854 1878 1979 1908 1933 1868 1920 1938 1805 1882 1879 1850 1862 1889 1872 1900 1903 1856 1862 1862 1959 1886 1856 1910 1912 1847 1939 1884 1885 1798 1885 1825 1903 1837 1900 1825 1837 1845 1807 1890 1843 1834
1879 1896 1898 1980 1844 1889 2013 1938 1950 1877 1849 1916 1879 1871 1946 1916 1890 1945 1942 1934 1914 1821 1902 1938 1878 1906 1823 1927 1912 1948 1932 1927 1859 1819 1933 1927 1915 1789 1970 1930 1931 1831 1856 1890 1831 1852 1863 1884 1821 1842 1861 1843 1751 1872 1790 1852 1819
1884 1974 1825 1888 1932 1843 1911 1899 1905 1845 1847 1920 1883 1934 1879 1869 1792 2024 1882 1944 1850 1913 1899 1799 1899 1927 1849 1935 1880 1874 1888 1881 1870 1829 1908 1841 1957 1892 2001 1999 1941 1959 1917 1913 1893 1849 1908 1853 1928 1868 1784 1881 1871 1844 1754 1849 1907
1890 1898 1845 1922 1950 1938 1868 1915 1907 1858 1825 1867 1933 1921 1933 1820 1865 1851 1947 1903 1869 1871 1837 1941 1892 1833 1817 1856 1863 1884 1909 1875 1904 1943 1916 2001 1887 1858 1837 1875 1846 1824 1913 1831 1891 1901 1818 1908 1921 1864 1898 1869 1829 1733 1815 1824 1861
1902 1934 1894 1839 1894 1869 1962 1809 1891 1865 1957 1950 1926 1861 1954 1876 1782 1883 1959 1852 1849 1891 1887 1756 1861 1905 1894 1913 1831 1828 1906 1875 1981 1887 1990 1922 1825 1995 1831 1852 1864 1922 1878 1895 1897 1819 1851 1873 1799 1901 1810 1880 1922 1875 1858 1841 1881
1852 1867 1940 1858 1867 1888 1863 1839 1851 1885 1875 1928 1903 1913 1858 1838 1819 1818 1744 1850 1856 1884 1861 1846 1896 1891 1894 1946 1911 1888 1865 1849 1777 1893 2010 1931 1832 1901 1817 1900 1869 1863 1825 1848 1885 1893 1875 1843 1884 1819 1950 1899 1926 1837 1819 1876 1873
1872 1871 1884 1844 1847 1935 1859 1858 1894 1866 1930 1741 1919 1854 1855 1866 1833 1860 1875 1852 1976 1835 1811 1994 1897 1833 1891 1904 1938 1906 1802 1875 1861 1835 1939 1870 1877 1972 1949 1880 1881 1795 1792 1764 1945 1978 1875 1887 1861 1890 1832 1794 1873 1919 1797 1876 1842
1897 1884 1845 1842 1878 1918 1835 1866 1868 1858 1908 1900 1868 1756 1841 1746 1842 1891 1852 1889 1869 1886 1802 1902 1859 1935 1978 1880 1918 1865 1779 1889 1824 1781 1902 1890 1836 1833 1908 1865 1916 1916 1902 1796 1878 1858 1825 1914 1921 1829 1848 1862 1863 1847 1847 1831 1888
1856 1933 1882 1948 1882 2003 1938 1901 1856 1755 1834 1868 1861 1768 1863 1841 1814 1896 1859 1871 1860 1908 1912 1893 1896 1968 1863 1938 1920 1828 1952 1854 1867 1913 1764 1893 1876 1892 1901 1813 1890 1916 1915 1887 1836 1812 1798 1846 1867 1846 1866 1787 1915 1898 1911 1717 1873
1877 1885 1868 1858 1932 1949 1835 1849 1898 1867 1911 1902 1926 1859 1818 1941 1836 1816 1940 1908 1886 1818 1899 1948 1870 1845 1887 1925 1891 1823 1885 1844 1795 1886 1879 1865 1841 1830 1902 1946 1803 1889 1893 1856 1816 1853 1813 1851 1897 1852 1827 1918 1834 1859 1738 1808 1796
1838 1839 1997 1844 1855 1867 1953 1898 1876 1865 1882 1808 1857 1856 1850 1832 1892 1802 1858 1882 1896 1925 1840 1905 1895 1838 1865 1922 1904 1843 1958 1890 1907 1796 1858 1871 1906 1815 1888 1870 1902 1717 1868 1823 1888 1905 1821 1812 1928 1867 1787 1826 1821 1905 1839 1747 1755
1870 1868 1899 1915 1873 1841 1938 1918 1897 1902 1846 1887 1750 1868 1841 1828 1928 1852 1876 1905 1859 1838 1931 1871 1920 1779 1836 1897 1863 1937 1895 1934 1940 1872 1890 1893 1852 1874 1860 1857 1874 1903 1826 1873 1877 1833 1922 1847 1832 1874 1914 1829 1846 1863 1829 1913 1816
1887 1888 1924 1880 1818 1878 1842 1908 1947 1914 1848 1867 1868 1891 1874 1872 1900 1828 1905 1865 1925 1965 1868 1893 1864 1869 1868 1867 1863 1946 1822 1883 1863 1817 1948 1846 1843 1826 1832 1793 1825 1802 2014 1967 1832 1895 1848 1833 1914 1817 1898 1798 1910 1865 1862 1856 1855
1914 1862 1828 1924 1897 1984 1931 1925 1896 1895 1908 1933 1889 1813 1836 1921 1855 1841 1935 1917 1897 1890 1880 1904 1851 1937 1936 1920 1856 1798 1810 1819 1871 1855 1905 1832 1941 1844 1827 1855 1901 1846 1826 1762 1870 1899 1873 1853 1902 1839 1884 1841 1838 1816 1846 1860 1787
1869 1874 1867 1894 1865 1951 1865 1887 1857 1900 1839 1874 1877 1876 1845 1897 1881 1952 1832 1855 1855 1949 1889 1942 1844 1881 1937 1892 1779 1841 1893 1902 1814 1791 1858 1870 1874 1856 1814 1744 1799 1831 1839 1717 1878 1815 1846 1864 1832 1927 1808 1859 1818 1848 1828 1803 1842
1871 1884 1842 1834 1873 1884 1950 1911 1992 1847 1847 1834 1849 1809 1822 1927 1925 1835 1857 1891 1848 1833 1843 1939 1858 1871 1975 1816 1874 1915 1835 1918 1906 1902 1849 1863 1909 1798 1842 1910 1791 1843 1781 1832 1898 1889 1884 1853 1883 1855 1975 1767 1826 1761 1879 1814 1738
1886 1909 1873 1850 1908 1894 1907 1872 1837 1773 1847 1926 1884 1882 1831 1832 1942 1897 1844 1950 1886 1978 1947 1815 1843 1785 1886 1914 1911 1883 1824 1873 1934 1943 1831 1906 1813 1820 1831 1870 1824 1875 1866 1913 1800 1818 1930 1860 1808 1884 1834 1921 1717 1812 1816 1947 1829
1860 1893 1883 1843 1923 1853 1834 1858 1922 1944 1942 1839 1813 1852 1889 1945 1902 1977 1929 1881 1850 1967 1844 1877 1970 1850 1941 1897 1814 1894 1841 1837 1821 1866 1777 1805 1851 1889 1838 1843 1853 1776 1907 1909 1846 1781 1775 1876 1941 1851 1849 1854 1813 1885 1912 1887 1776
1819 1896 1911 1936 1887 1847 1874 1894 1855 1869 1843 1864 1921 1883 1875 1926 1866 1923 1886 1889 1844 1896 2002 1944 1909 1858 1927 1870 1882 1886 1899 1894 1809 1904 1786 1920 1908 1888 1901 1859 1857 1793 1880 1828 1809 1839 1905 1893 1849 1920 1837 1868 1910 1850 1873 1900 1721
1861 1895 1819 1865 1741 1797 1832 1849 1901 1869 1870 1811 1786 1910 1936 1961 1907 1899 1949 1863 1845 1885 1881 1831 1884 1937 1860 1906 1873 1838 1859 1898 1924 1863 1902 1881 1851 1880 1945 1851 1929 1846 1843 1879 1774 1826 1788 1871 1918 1780 1825 1853 1782 1852 1861 1867 1844
1822 1867 1806 1745 1942 1836 1841 1861 1787 1867 1947 1906 1826 1822 1935 1787 1879 1920 1830 1928 1879 1837 1921 1923 1855 1932 1844 1841 1917 1928 1865 1915 1873 1839 1846 1910 1896 1903 1911 1838 1857 1905 1870 1811 1899 1874 1860 1822 1935 1757 1862 1807 1856 1868 1786 1919 1887
1850 1926 1855 1766 1858 1815 1894 1861 1911 1910 1846 1861 1857 1800 1837 1784 1912 1937 1916 1942 1929 1866 1905 1916 1923 1922 1899 1838 1910 1872 1778 1849 1863 1868 1870 1828 1880 1793 1889 1937 1857 1888 1882 1946 1841 1838 1800 1819 1874 1918 1879 1895 1874 1884 1861 1761 1800

CodePudding user response:

If we go by your assumption that there likely won't be any zeros in the middle of the array, we can figure out if a row contains any zeros using any(axis=1) (or axis=0 for columns), and if a row contains all zeros using all

data = np.array([[0, 0, 0, 0, 0, 0, 0],
                 [0, 1, 3, 4, 6, 1, 0],
                 [0, 2, 3, 5, 2, 1, 0],
                 [0, 1, 0, 0, 1, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0]])

To start, we want to delete those rows and columns that are all zeros.

delete_rows = (data == 0).all(axis=1)
delete_cols = (data == 0).all(axis=0)

For now, let's set those rows to -999 (since your data is pixel data, -999 is an invalid value that you never expect to see) so that data == 0 for the future steps isn't confused by these "border" rows/cols

data[delete_rows, :] = -999
data[:, delete_cols] = -999

Next, let's find any rows that contain any zeros and are next to a row that's going to be deleted (previous or next row is in delete_rows):

zero_rows = (data == 0).any(axis=1)

d_r = np.zeros(zero_rows.shape, dtype=bool)
d_r[1:] = d_r[1:] | delete_rows[:-1]
d_r[:-1] = d_r[:-1] | delete_rows[1:]

delete_rows = delete_rows | (zero_rows & d_r)
data[delete_rows, :] = -999

We can repeat this until there are no more changes to delete_rows. I.e.:

del_count = sum(delete_rows)
prev_del_count = del_count   1

while del_count != prev_del_count:
    zero_rows = (data == 0).any(axis=1)

    d_r = np.zeros(zero_rows.shape, dtype=bool)
    d_r[1:] = d_r[1:] | delete_rows[:-1]
    d_r[:-1] = d_r[:-1] | delete_rows[1:]

    delete_rows = delete_rows | (zero_rows & d_r)
    prev_del_count, del_count = del_count, sum(delete_rows)
    data[delete_rows, :] = -999

Then, we can do the same for columns:

del_count = sum(delete_cols)
prev_del_count = del_count   1

while del_count != prev_del_count:
    zero_cols = (data == 0).any(axis=0)

    d_c = np.zeros(zero_cols.shape, dtype=bool)
    d_c[1:] = d_c[1:] | delete_cols[:-1]
    d_c[:-1] = d_c[:-1] | delete_cols[1:]

    delete_cols = delete_cols | (zero_cols & d_c)
    prev_del_count, del_count = del_count, sum(delete_cols)
    data[:, delete_cols] = -999

Now, we have:

delete_rows = np.array([ True, False, False,  True,  True])
delete_cols = np.array([ True, False, False, False, False,  False,  True])

And we can filter out the required rows and cols:

filtered_data = data[~delete_rows, :][:, ~delete_cols]

which gives:

array([[1, 3, 4, 6, 1],
       [2, 3, 5, 2, 1]])
  • Related