Create a random jpg set

Here's the scenario, I want to create a set of random, small jpg - somewhere between 50 bytes and 8k in size - the actual visual content of jpeg doesn't matter if they are valid. I need to create a thousand or so, and they all have to be unique - even if they differ from each other by only one pixel. Can I just write jpeg header / footer and some random bytes? I cannot use existing photos or photo sets from the Internet.

The second problem is that the set of images must be different for each program run.

I would prefer to do this in python since the wrapper scripts are in Python.

I was looking for python code to create jpg from scratch and didn't find anything, so library pointers are just as good.

+5
source share
3 answers

If the images can only be random noise, you can generate an array with numpy.randomand save them using PIL Image.save.

This example can be extended to include ways to avoid (very unlikely) pattern repetition:

import numpy, Image

for n in xrange(10):
    a = numpy.random.rand(30,30,3) * 255
    im_out = Image.fromarray(a.astype('uint8')).convert('RGBA')
    im_out.save('out%000d.jpg' % n)

These conditions must be met in order to receive jpeg images:

  • An array must be formed (m, n, 3) - three colors, RG and B;
  • Each element (each color of each pixel) must be a byte integer (uint or unsigned integer with 8 bits) in the range from 0 to 255.

Additionally, another method, in addition to pure chance, can be used to create images in case you do not want pure noise.

+13
source

If you are looking for a way to do this without numpy, this worked for me

(python 3.6 , )

import random as r
from PIL import Image

dat = bytes([r.randint(1,3) for x in range(4500000)])
i = Image.frombytes('1', (200,200), dat)
0

, JPEG Pillow (PIL.Image.new [0]) :

from PIL import Image

width = height = 128
valid_solid_color_jpeg = Image.new(mode='RGB', size=(width, height), color='red')
valid_solid_color_jpeg.save('red_image.jpg')

[0] https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.new

// EDIT: I thought the OP wanted to generate reliable images and didn't care about its content (why did I suggest solid color images). Here is a function that generates the correct image with random pixels and, as a bonus, writes a random line to the generated image. The only dependency is Pillow, everything else is pure Python.

import random
import uuid

from PIL import Image, ImageDraw    


def generate_random_image(width=128, height=128):
    rand_pixels = [random.randint(0, 255) for _ in range(width * height * 3)]
    rand_pixels_as_bytes = bytes(rand_pixels)
    text_and_filename = str(uuid.uuid4())

    random_image = Image.frombytes('RGB', (width, height), rand_pixels_as_bytes)

    draw_image = ImageDraw.Draw(random_image)
    draw_image.text(xy=(0, 0), text=text_and_filename, fill=(255, 255, 255))
    random_image.save("{file_name}.jpg".format(file_name=text_and_filename))

# Generate 42 random images: 
for _ in range(42):
    generate_random_image()
0
source

All Articles