Python problem ,

hello I have an error that appears on python it’s ‘tuple’ has no attribute ‘size’. can you help me, (it’s with the PIL module)

from PIL import Image

image1 = Image.open(".\christmas.png")
L, H = image1.size
image2 = Image.new(“RGB”,(L,H))

for y in range(H):
for x in range(L):
pixel = image1.getpixel((x,y))
r = pixel[0]
v = pixel[1]
b = pixel[2]
g = int( ®)
h = int( ®)
i = int( (v))
image2.putpixel((x,y),(g,h,i))

Partie 1

im_size = image2.size

# Define box inside image

left = 0
top = 0
width = 539
height =179

# Create Box

box = (left, top, left+width, top+height)

# Crop Image

area1= image2.crop(box)
area1.show()

#image 2

im_size = image2.size

left2 = 0
top2 =179
width2 = 539
height2 =179

box2 = (left2, top2, left2+width2, top2+height2)

area2 =image2.crop(box2)

area2 = area2.rotate(180)

area2.show()

#image 3

im_size = image2.size

left3 = 0
top3 = 358
width3 = 539
height3 =179

box3 = (left3, top3, left3 + width3, top3 + height3)

area3 = image2.crop(box3)

area3.show()

size = 530
area1 = area1.size
print (area1.size)

#creates a new empty image, RGB mode, and size 400 by 400.
new_im = Image.new(‘RGB’, (2156,2*im_size[1]))

new_im.paste(area1, (0,0))
new_im.paste(area2, (0,im_size[1]))
new_im.paste(area3, (1000,0))

new_im.save(“Modis_2008015_1435.png”, “PNG”)
new_im.show()

hello I have an error that appears on python it’s ‘tuple’ has no
attribute ‘size’. can you help me, (it’s with the PIL module)

It always helps to have the full traceback so that we can compare it
with your code. In particular it has line numbers in it. It also has the
exact text of the error message, which can be very useful.

But you’ve included all the code, which is even more important.

Anyway…

Indeed, tuples do not have a .size attribute. So if you went:

x = 1, 2

then “x” is a 2-tuple of (1,2). But there’s no .size.

What this means is that you tried to access .size on something you
thought had one, but in fact you had a tuple. So let’s see where you use
.size:

from PIL import Image

image1 = Image.open(".\christmas.png")
L, H = image1.size

Looks ok. Images have a .size with PIL.

image2 = Image.new(“RGB”,(L,H))

image2 is also an Image.

[…]

Partie 1

im_size = image2.size

So this should be ok. […] and all the other things you use .size on
are until this:

[…]

area1= image2.crop(box)

area1 is an Image. Ok. Then:

[…]

size = 530
area1 = area1.size

Now area1 isn’t an Image any more, it is the size from the Image it used
to be.

print (area1.size)

And now this doesn’t work, because area1 is not an Image.

It is generally a bad idea to use the same variable name for different
types of things, it makes for just the confusion you’re seeing. I’d do
this:

area1_size = area1.size

Now area1 is unchanged, and area1_size is the size, and its name reminds
you of this.

Cheers,
Cameron Simpson cs@cskk.id.au