Git Product home page Git Product logo

randimage's Introduction

randimage - create random images in python

randimage example

This package comes out of frustration: I could not find a simple way to generate random profile pictures for users of my app riffandmatch.

Now it's as simple as

from randimage import get_random_image, show_array
img_size = (128,128)
img = get_random_image(img_size)  #returns numpy array
show_array(img) #shows the image

To save the image you can use matplotlib:

import matplotlib
matplotlib.image.imsave("randimage.png", img)

In examples.py you can find slightly more detailed code showing for example how to manually choose and combine the masks and path finding procedures.

Installation

pip install randimage

How it works

You can read this post on my blog for a full explanation of how the procedure works.

The basic idea is to generate a random region-filling path for the image, then pick a colormap from matplotlib and use it to progressively color the pixels in the image as ordered by the path.

To generate the path we start from a random gray-valued mask (of which you can find several examples in masks.py) of the same size of the final image and apply to it either the EPWT (Easy Path Wavelet Transform) [1] path-finding procedure or a novel probabilistic path finding.

In both cases the starting point is chosen randomly and for each point a neigborhood is considered, which does not include points that are already part of the path. For the EPWT, each subsequent point in the path is chosen in this neighborhood as the one whose gray value is closest in absolute value to the current point. For the probabilistic path instead, a random point in the neighborhood is chosen using the gray values as probability weights.

In the future I would like to try and use the path finding procedure of the RBEPWT (Region Based Easy Path Wavelet Transform) [2] and experiment with rule-based cellular automata to construct paths with greater self-similarity.

[1] Gerlind Plonka. The easy path wavelet transform: A new adaptive wavelet transform for sparse representation of two-dimensional data. Multiscale Modeling & Simulation, 7(3):1474โ€“1496, 2008.

[2] Budinich, R. (2017). A region-based easy-path wavelet transform for sparse image representation. International Journal of Wavelets, Multiresolution and Information Processing, 15(05), 1750045.

randimage's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

randimage's Issues

Error using example given

Hi there! Sometimes invoking your example, it gives the following error:

Traceback (most recent call last):
  File "/media//HackerDAO/5. Mainnet/random_tokens/make.py", line 6, in <module>
    img = get_random_image(img_size)  #returns numpy array
  File "/home/user/.local/lib/python3.9/site-packages/randimage/utils.py", line 37, in get_random_image
    path = random.choice(PATHS)(mask).get_path()
  File "/home/user/.local/lib/python3.9/site-packages/randimage/paths.py", line 78, in get_path
    next_point = random.choices(neighbors, weights=weights, k=1).pop()
  File "/usr/lib/python3.9/random.py", line 495, in choices
    raise ValueError('Total of weights must be greater than zero')
ValueError: Total of weights must be greater than zero

Here is the PoC:

#!/usr/bin/python3
import matplotlib
from randimage import get_random_image, show_array
img_size = (500,500)

img = get_random_image(img_size) 
matplotlib.image.imsave("randimage.png", img)

Try to call this PoC multiple times and the error will eventually occur

ValueError: numpy.ndarray size changed, may indicate binary incompatibility

After pip install, attempt to import, get this:

from randimage import get_random_image, show_img_list
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    from randimage import get_random_image, show_img_list
  File "C:\Python310\lib\site-packages\randimage\__init__.py", line 1, in <module>
    from .masks import GaussianBlobMask, SaltPepperMask, NormalMask
  File "C:\Python310\lib\site-packages\randimage\masks.py", line 3, in <module>
    from scipy.stats import multivariate_normal
  File "C:\Python310\lib\site-packages\scipy\stats\__init__.py", line 485, in <module>
    from ._stats_py import *
  File "C:\Python310\lib\site-packages\scipy\stats\_stats_py.py", line 39, in <module>
    from scipy.spatial.distance import cdist
  File "C:\Python310\lib\site-packages\scipy\spatial\__init__.py", line 105, in <module>
    from ._kdtree import *
  File "C:\Python310\lib\site-packages\scipy\spatial\_kdtree.py", line 4, in <module>
    from ._ckdtree import cKDTree, cKDTreeNode
  File "_ckdtree.pyx", line 1, in init scipy.spatial._ckdtree
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject

installed:

C:\Users\Jarad>pip install randimage
Requirement already satisfied: randimage in c:\python310\lib\site-packages (1.2)
Requirement already satisfied: scipy in c:\python310\lib\site-packages (from randimage) (1.10.0)
Requirement already satisfied: numpy in c:\python310\lib\site-packages (from randimage) (1.21.4)
Requirement already satisfied: matplotlib in c:\python310\lib\site-packages (from randimage) (3.6.3)
Requirement already satisfied: pillow>=6.2.0 in c:\python310\lib\site-packages (from matplotlib->randimage) (9.2.0)
Requirement already satisfied: contourpy>=1.0.1 in c:\python310\lib\site-packages (from matplotlib->randimage) (1.0.7)
Requirement already satisfied: cycler>=0.10 in c:\python310\lib\site-packages (from matplotlib->randimage) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in c:\python310\lib\site-packages (from matplotlib->randimage) (4.38.0)
Requirement already satisfied: python-dateutil>=2.7 in c:\python310\lib\site-packages (from matplotlib->randimage) (2.8.2)
Requirement already satisfied: packaging>=20.0 in c:\python310\lib\site-packages (from matplotlib->randimage) (23.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\python310\lib\site-packages (from matplotlib->randimage) (1.4.4)
Requirement already satisfied: pyparsing>=2.2.1 in c:\python310\lib\site-packages (from matplotlib->randimage) (3.0.9)
Requirement already satisfied: six>=1.5 in c:\python310\lib\site-packages (from python-dateutil>=2.7->matplotlib->randimage) (1.16.0)

matplotlib.cm.get_cmap is deprecated

Hi, thanks for this package - loving the aesthetic of the generated images.

When running my own tests the following warning is being thrown:

/code/.tox/py311/lib/python3.11/site-packages/randimage/coloredpath.py:13: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.

Whilst not critical to fix it may cause problems in the future as matplotlib does not have any version restrictions so when the function is removed this library will break. Two possible fixes:

  1. Add a maximum version restriction to matplotlib so that it won't install with the version in which the function is removed.
    • e.g. matplotlib<3.9
  2. Update randimage to use the new function. I'll note that doing this probably means adding in a minimum version restriction.

Update (2024-05-18) - this is now causing errors as matplotlib v3.9 has been released and thus the deprecated function removed.

randimage 1.2 is not pyinstaller-friendly

Thanks for sharing a really interesting random image generator, it's proven ideal for the placeholder images that show in my application while full rendered images are pending.

Unfortunately, version 1.2 doesn't seem to be compatible with PyInstaller. If randimage is imported, my compiled Windows application won't run- instead throwing an error, the key part of which is pasted below.

It's non-critical as I can just use different placeholder images, and I'm not in a position to fix it myself right now. But I thought I'd flag it as an issue in case anybody else hit the same problem.

  File "func\func_images.py", line 21, in <module>
    from randimage import get_random_image
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "randimage\__init__.py", line 1, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "randimage\masks.py", line 3, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\stats\__init__.py", line 608, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\stats\_stats_py.py", line 39, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\spatial\__init__.py", line 110, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\spatial\_kdtree.py", line 4, in <module>
  File "_ckdtree.pyx", line 12, in init scipy.spatial._ckdtree
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\sparse\__init__.py", line 287, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\sparse\csgraph\__init__.py", line 185, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\sparse\csgraph\_laplacian.py", line 7, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\sparse\linalg\__init__.py", line 120, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\sparse\linalg\_isolve\__init__.py", line 4, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "scipy\sparse\linalg\_isolve\iterative.py", line 9, in <module>
ImportError: DLL load failed while importing _iterative: The specified module could not be found.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.