2

enter image description here

I got some problem with merging 100 images, so that they build a 10 x 10 huge picture like above: I tried like this:

from PIL import Image
image1 = Image.open("4XP6edit.png")
image2 = Image.open('4MBSedit.png')
(width, height) = image1.size
result_width = 10*width
result_height = 10*height

result = Image.new('RGB', (result_width, result_height))
result.paste(im=image1, box=(0, 0))
result.paste(im=image2, box=(width, 0))
result.paste(im=image2, box=(2*width, 0))
# and so on until 10*width, 0 and than:
result.paste(im=image10, box=(0, height))
result.paste(im=image10, box=(0, 2*height))

This works fine, but I want it to be created automatically in a for loop or sth. But my suggestion doesnt work.. Can someone find my mistake?

length = len(protein_list)
k=0; m=0; j=0

for i in range(length):
    image= Image.open(str(protein_list[i] + 'edit.png')

    (width, height) = image.size

    result_width = 10*width 
    result_height = 10*height

    result = Image.new('RGB', (result_width, result_height))
    if (k == 10): k = 0
    if (j >= 0 and j <= 9):  m = 0
    if (j >= 10 and j <= 19):  m = 1
    if (j >= 20 and j <= 29): m = 2
    if (j >= 30 and j <= 39): m = 3
    if (j >= 40 and j <= 49): m = 4    
    if (j >= 50 and j <= 59): m = 5
    if (j >= 60 and j <= 69): m = 6    
    if (j >= 70 and j <= 79): m = 7
    if (j >= 80 and j <= 89): m = 8
    if (j >= 90 and j <= 99): m = 9    

    result.paste(im=image, box=(k*width, m*height))
    k= k+1
    j=j+1


result

I know this if part is really ugly but I am new in programming so please excuse this.. Can someone help me with this? It shows a huge 10x10 black picture in which a single picture of the proteinedit.png is displayed.. it looks like this enter image description here

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
Anja
  • 345
  • 5
  • 21
  • 1
    Can you elaborate on what you mean by your solution "doesn't work"? Is it throwing an error? Are the results wrong? If so, how are they wrong? Please [edit] your question to include those details. – Mr. Llama Oct 23 '18 at 13:55
  • This question has already been answers here: [https://stackoverflow.com/questions/30227466/combine-several-images-horizontally-with-python](https://stackoverflow.com/questions/30227466/combine-several-images-horizontally-with-python) – Y.Wang Oct 23 '18 at 13:56
  • 1
    @Y.Wang I looked at this example but it didn't help me because its only for mering images horizontally.. i need it 10x10 – Anja Oct 23 '18 at 14:11
  • @ Anja, I was serious about there being a lot more problems with this code in terms of readability and quality. I suggest you post this code on [code review](https://codereview.stackexchange.com/) for some tips. This code can be made a lot simpler – Maarten Fabré Oct 24 '18 at 07:23
  • Try this: https://github.com/hugovk/pixel-tools/blob/master/contact_sheet.py – Hugo Oct 25 '18 at 06:39

1 Answers1

3

it looks like that because you define your result image every iteration of the loop. If all images are the same size, you can do something like this:

length = len(protein_list)
k=0; m=0; j=0
result = None

    for i in range(length):
        image= Image.open(str(protein_list[i] + 'edit.png')
        if result is None:
            (width, height) = image.size

            result_width = 10*width 
            result_height = 10*height

            result = Image.new('RGB', (result_width, result_height))

    if (k == 10): k = 0
    ...

result

The rest of the code can use a lot of improvements, so if this works, I would take that code to Code Review SO.

Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36