0

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)
chained
  • 23
  • 2

1 Answers1

0

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.

Matthew E. Miller
  • 557
  • 1
  • 5
  • 13
  • This is the answer for anyone wondering. You do need to fix the format of the pictures you're reading by removing the ''.jpg'' in ''photo = f'directory/{aFile}' '' because that will cause the program to look for an image called '' x.jpg.jpg '' which does not exist. – chained Aug 19 '19 at 20:13
  • @chained I'll adjust that in the answer. Glad it worked for you. – Matthew E. Miller Aug 19 '19 at 20:49