question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Random Photo Colors and Sizes

See original GitHub issue

I see that I can create a photo with a certain color and size like this:

class MyFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.MyModel

    the_image = factory.django.ImageField(color='blue')

Is it possible to randomize that color and also the dimensions?

More generally, this means using one declaration as the input to another declaration in generation.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
kaushal235commented, Nov 2, 2018

Custom workaround for this issue is override ImageField & if any callable pass call it run time

class CustomImageField(factory.django.ImageField):

    def _make_data(self, params):
        color = params.pop('color', 'blue')
        if callable(color):
            color = color()
        params['color'] = color
        return super(CustomImageField, self)._make_data(params)

& use it as

class MediaItemFactory(factory.DjangoModelFactory):
    image = CustomImageField(color=faker.safe_color_name)
   
    class Meta:
         model = ModelName

here faker.safe_color_name is function that generate random color name

1reaction
x-yuricommented, Apr 11, 2019

I’ve found the same can be done by overriding generate method:

class RandomImageField(factory.django.ImageField):
    def generate(self, step, params):
        for n in ['width', 'height']:
            if not n in params:
                continue
            v = params[n]
            if callable(v):
                v = v()
            params[n] = v
        return super(RandomImageField, self).generate(step, params)

class PhotoFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Photo
    file = RandomImageField(
        width=faker().image_width,
        height=faker().image_height)

def faker():
    return factory.Faker._get_faker()

class MyProvider(BaseProvider):
    def image_width(self):
        return self.random_int(300, 400)

    def image_height(self):
        return self.random_int(400, 500)

factory.Faker.add_provider(MyProvider)

Seeing how generate doesn’t start with an underscore make it feel like a more legitimate/safe way. But if you want to persist the generated values in the model (it has corresponding fields), you can replace:

            params[n] = v

with:

            step.attributes[n] = params[n] = v

Or better using evaluate:

from collections import ChainMap

class RandomImageField(factory.django.ImageField):
    def evaluate(self, instance, step, extra):
        return super(RandomImageField, self).evaluate(instance, step,
            ChainMap(extra,
                {k: getattr(instance, v) for k, v in self.defaults.items()
                    if k in ['width', 'height']}))

class PhotoFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Photo
    width = factory.Faker('image_width')
    height = factory.Faker('image_height')
    file = RandomImageField(width='width', height='height')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Dummy Image Generator - DummyImage.com
Colors. background color / text color. Colors are represented as hex codes (#ffffff is white); Colors always ... Standard image sizes are also...
Read more >
Generate a Random Image
This tool generates random images. It randomly fills pixels with the colors you have specified in the Image Colors option section above. The...
Read more >
32059 Random Size Images, Stock Photos & Vectors
Random Size royalty-free images. 32,059 random size stock photos, vectors, and illustrations are available royalty-free. See random size stock video clips.
Read more >
1340 Random Colours Stock Photos, Images & Pictures
Download Random Colours stock photos. Free or royalty-free photos and images. Use them in commercial designs under lifetime, perpetual & worldwide rights.
Read more >
Image Picker Wheel - Pick a Random Image by Spinning
Image Picker Wheel is a special random image generator to pick a random picture from a list of ... Relationship Between Image Background...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found