chained
chained

Reputation: 23

How to load, deep dream and overwrite all images in a folder?

So far I have been able to ''manually'' process images by replacing 'picture' in

photo = ''directory/picture.jpg''

for every image I'm processing.

This is effective, sure, but it's very slow.

any ideas?

The code i'm using:

from deepdreamer import model, load_image, recursive_optimize
import numpy as np
import PIL.Image
layer_tensor = ...

photo = "directory/picture.jpg"
img_result = load_image(filename='{}'.format(photo))

img_result = recursive_optimize(...)
img_result = np.clip(img_result, 0.0, 255.0)
img_result = img_result.astype(np.uint8)
result = PIL.Image.fromarray(img_result, mode='RGB')
result.save(photo)

Upvotes: 0

Views: 140

Answers (1)

Matthew E. Miller
Matthew E. Miller

Reputation: 567

Assuming all image files are in the same folder/directory, you can:

  1. Encapsulate your image processing into a function.
  2. Find all the filenames using os.listdir().
  3. Loop over the filenames, passing each into the imageProcessing() function which takes action on each image.

Python Code:

from deepdreamer import model, load_image, recursive_optimize
import numpy as np
import PIL.Image
import os //for file management stuff

layer_tensor = ...

def imageProcess(aFile):
  photo = "directory/{aFile}"
  img_result = load_image(filename='{}'.format(photo))

  img_result = recursive_optimize(...)
  img_result = np.clip(img_result, 0.0, 255.0)
  img_result = img_result.astype(np.uint8)
  result = PIL.Image.fromarray(img_result, mode='RGB')
  result.save(photo)

for filename in os.listdir('dirname'):
     imageProcess(filename)

It'll be something like this. Let me know how that works, I didn't run the code.

Upvotes: 0

Related Questions