1

I have a muti lined .txt file with image data like this:

(198, 252, 247) (255, 255, 250) (254, 253, 248) (251, 252, 246) (247, 248, 240) ... 
(100, 144, 247) (255, 200, 250) (254, 253, 248) (251, 252, 246) (247, 248, 240) ... 

How do I read these data into tuples?

lst = [((198, 252, 247), (255, 255, 250)), (second line), (thrid) ...]

and eventually draw each line back into an image file using the Image module

AK_
  • 1,879
  • 4
  • 21
  • 30
  • 1
    Possible duplicate of [How do I create an image in PIL using a list of RGB tuples?](https://stackoverflow.com/questions/12062920/how-do-i-create-an-image-in-pil-using-a-list-of-rgb-tuples) – sudo Dec 24 '17 at 15:55
  • @SoumeshBanerjee no, I'm trying to read the tuples from file – AK_ Dec 24 '17 at 15:58
  • what is the delimiter for the tuples? – sudo Dec 24 '17 at 16:02
  • @SoumeshBanerjee I can change the space to dashes if that makes life easier. so it will be: `(198, 252, 247)-(255, 255, 250)-(254, 253, 248)-...` – AK_ Dec 24 '17 at 16:04
  • @AK_, you say: *"I'm trying to read the tuples from file"*.Then, just ask how to read tuples from file. Simplify the question. – dani herrera Dec 24 '17 at 16:08
  • @danihp done... – AK_ Dec 24 '17 at 16:11

2 Answers2

2

Simply read each line, extract the value triplets from it, and convert them to integers.

import re

triplet = r'\((\d+), (\d+), (\d+)\)' # regex pattern

image = []
with open('image.txt') as fp:
    for line in fp:
        image.append([(int(x), int(y), int(z)) for x, y, z in re.findall(triplet, line)])

EDIT

To actually draw the image, check out this question. However, this should work:

from PIL import Image

width, height = len(image[0]), len(image)
data = sum(image, []) # ugly hack to flatten the image

im = Image.new('RGB', (width, height))
im.putdata(data)
im.save('image.png')
Dan Gittik
  • 3,460
  • 3
  • 17
  • 24
  • this worked; but could you please add how to draw the image for each tuple? – AK_ Dec 24 '17 at 16:38
  • Sure. There you go. – Dan Gittik Dec 24 '17 at 17:09
  • ok this works; there is a one bug that I will edit and I will mark your reply as answer! – AK_ Dec 24 '17 at 17:17
  • If ``images = [[(1,1,1),(2,2,2)],[(3,3,3),(4,4,4)]]``, ``sum(image, [])`` flattens it into ``[(1,1,1),(2,2,2),(3,3,3),(4,4,4)]``, which is what ``putdata`` expects. Your suggested edit of ``map(sum, image)`` just raises a ``TypeError``. :( – Dan Gittik Dec 24 '17 at 17:24
  • I'm still getting the wrong image size. any idea why? – AK_ Dec 24 '17 at 17:25
  • 1
    Maybe your file is transposed, with rows (i.e. ``len(image)``) being the width and columns (i.e. ``len(image[0])``) being the height? – Dan Gittik Dec 24 '17 at 17:29
  • can you please document your code? What is exactly the first defintion do? – AK_ Dec 25 '17 at 08:26
  • It's a regular expression pattern that captures ``(, , )``. See [the ``re`` module documentation](https://docs.python.org/3.6/library/re.html). – Dan Gittik Dec 25 '17 at 18:06
0

First you need to scan and split the data from the file, then you can just parse the tuples from the data (string tuple), then simply create an image object using PIL

def getTuple(s):
    try:
        s = eval(str(s))
        if type(s) == tuple:
            return s
        return
    except:
        return

with open("filename.txt", "rb") as fp:
    im_list = []
    data_points = fp.read()
    data_point_list = data_points.split("-")
    for data_point in data_point_list:
        im_list.append(getTuple(data_point))
    # the code snippet from https://stackoverflow.com/questions/12062920/how-do-i-create-an-image-in-pil-using-a-list-of-rgb-tuples
    im2 = Image.new(im.mode, im.size)
    im2.putdata(list_of_pixels)
sudo
  • 906
  • 2
  • 14
  • 32