-3

I'm setting up a wiki for a game, and I need to put GIFs for make the site easier to understand. But I have a problem. To make the GIFs I need to merge some images, I did it image by image manually, which is annoying. So what language can I use to automate this?

I've tried some codes with python, and I haven't had any success. The only way I can make it work was using Photoshop to combine these images.

I tried this code:

import numpy as np
from PIL import Image
images_list = []
for i in range(1,4): #insert last number of photo
images_list.append(str(i)+'.PNG')
count = 1;

directory = "C:/Users/Windows/Desktop/BloodStoneSprites/sprites1"  
#change to directory where your photos are
ext = ".PNG"
new_file_name = "vimage-"
new_directory = "C:/Users/Windows/Desktop/BloodStoneSprites/Uniao" # 
change to path of new directory where you want your photos to be saved


for j in range(0,len(images_list),2):
name = new_file_name + str(j) + ext
two_images_list = [images_list[j],images_list[j+1]]
imgs = [ Image.open(i) for i in two_images_list ]
min_img_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack( (np.asarray( i.resize(min_img_shape) ) for i in 
imgs ) )
imgs_comb = Image.fromarray( imgs_comb)
imgs_comb.save(new_directory+'/'+name )
count +=1

And here are some images I need to combine: https://i.stack.imgur.com/nvd8N.jpg

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • your code isn't valid python. What happened when you ran your code? What is the expected output? Please show a [mcve] – Alan Birtles Apr 16 '19 at 15:26
  • Sorry for my bad english, im in cellphone and corrector is horrible. But, when i start the code appears this: Traceback (most recent call last): File "C:\Users\Windows\Desktop\BloodStoneSprites\mergin.py", line 2, in from PIL import Image ModuleNotFoundError: No module named 'PIL'. and the output dont show nothing. – Gabriel Lima Barbosa Apr 16 '19 at 16:56

1 Answers1

0

The PIL library hasn't been maintained. "Pillow" also identifies as PIL, check that it's properly installed.

As mentioned in the comments, your question isn't completely clear. That said, it looks like you're trying to write something like this:

import numpy as np
from PIL import Image

images_names = [
    "C:/Users/Windows/Desktop/BloodStoneSprites/sprites1/{0!s}.PNG".format(i)
    for i in range(1,4)
  ]
images = [Image.open(file_name) for file_name in images_names]

new_name_scheme = "C:/Users/Windows/Desktop/BloodStoneSprites/Uniao/vimage-{0!s}.PNG"

for j in range(0,len(images),2):
  two_images_list = [images[j],images[j+1]]
  min_img_shape = sorted( [(np.sum(i.size), i.size ) for i in two_images_list] )[0][1]
  imgs_comb = Image.fromarray(
    np.hstack((
      np.asarray(i.resize(min_img_shape))
      for i in two_images_list
    )))
  imgs_comb.save(new_name_scheme.format(j))

I make no promises that the above will actually run; you'll need to work on it.
The most important changes are removing or flattening a lot of the variables, and adding the indentations needed for your for loops to work.
You'll note that I'm using 'str'.format(arg) syntax. 'str' % (arg) would also work just fine.

ShapeOfMatter
  • 991
  • 6
  • 25