0

Whilst trying to reverse the process of this question by joining smaller images together to assemble the original one. I've put this code together.

# imgarr is a list of image filenames.
# w & h are the number of tiles used for width & height
# folder the destination folder


def tiles_to_image(imgarr, w, h, folder):
  # concatenates images
  im_h = cv2.hconcat(imgarr)
  
  nuimg = chunks(imgarr, w)
  img_tile = concat_vh(nuimg)

  # write the output image
  imgFile = os.path.join(folder, "image.jpeg")
  cv2.imwrite(imgFile, img_tile)

def concat_vh(list_2d):
  # return final image
  return cv2.vconcat([cv2.hconcat(list_h) 
  for list_h in list_2d])

def chunks(lst, n):
  for i in range(0, len(lst), n):
    yield lst[i:i + n]

The code works fine, however in the reconstruction the images are altered, presumably stretched but then resized down. See this close up of a yellow image with single pixel diagonal lines. Not the introduction of pink anti-aliasing on the right image. #notusinglenna

image comparison

The source images must not be altered. I'm not sure how to stop this since there are no options for hconcat. I realise there may be a better way to reconstruct an image like this.

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • Can you please post a reproducible and executable code sample (include the input image/images)? Your question is not clear... In case you don't want the image to be altered, start by using lossless image file format like **PNG** instead of **JPEG**. (Replace `imgFile = os.path.join(folder, "image.jpeg")` with `imgFile = os.path.join(folder, "image.png")`) – Rotem Jun 23 '21 at 20:43
  • @Rotem That was it! I was assuming that saving as JPEG would be lossless. – Ghoul Fool Jun 24 '21 at 08:24

0 Answers0