nman84
nman84

Reputation: 435

image processing

This is an assignment, i have put good effort since i am new to python programming:

I am running the following function which takes in image and phrase (spaces will be removed so just text) as arguments, i have already been given all the import and preprocessing code, i just need to implement this function. I can only use getpixel, putpixel, load, and save. That is why coding this has been a hard task for me.

def InsertoImage(srcImage, phrase):  
    pix = srcImage.load()  
    for index,value in enumerate(phrase):  
        pix[10+index,15] = phrase[index]  
    srcImage.save()  
pass  

This code is giving "system error" which says that "new style getargs format but argument is not tuple"

Edit:

C:\Users\Nave\Desktop\a1>a1_template.py lolmini.jpg Hi  
Traceback (most recent call last):  
  File "C:\Users\Nave\Desktop\a1\a1_template.py", line 31, in <module>  
    doLOLImage(srcImage, phrase)  
  File "C:\Users\Nave\Desktop\a1\a1_template.py", line 23, in doLOLImage  
    pix[10+index,15] = phrase[index]  
SystemError: new style getargs format but argument is not a tuple  

Edit:

Ok Thanks, i understood and now posting code but i am getting error for the if statement not sure why the if statement is not working, here is full code sorry for not adding it entirely before:

from future import division

letters, numbers, and punctation are dictionaries mapping (uppercase)

characters to Images representing that character

NOTE: There is no space character stored!

from imageproc import letters, numbers, punctuation, preProcess

This is the function to implement

def InserttoImage(srcImage, phrase):
pix = srcImage.load()
for index,value in enumerate(phrase):
if value in letters:
pix[10+index, 15] = letters[value]
elif value in numbers:
pix[10+index, 15] = numbers[value]
elif value in punctuation: pix[10+index, 15] = punctuation[value]
srcImage.save()
pass

This code is performed when this script is called from the command line via:

'python .py'

if name == 'main':
srcImage, phrase = preProcess()
InserttoImage(srcImage, phrase)

Thanks, letter, numbers, and punctuation are dictionaries which see the key element and open the image (font). But still there is an issue with pix[10+index, 15] as it is giving error:

pix[10+index, 15]  = letters[value]  

SystemError: new style getargs format but argument is not a tuple

Upvotes: 1

Views: 1780

Answers (4)

alexeyprog
alexeyprog

Reputation: 816

Just try this: pix[10+index:15] = letters[value] Use ":" instead of ","

Upvotes: 0

rwong
rwong

Reputation: 6162

My suggestion to the general problem is to create an image that contains all of the characters, at known coordinates (top, bottom, left, right) and then transfer the appropriate parts of the character image into the new output image.

Upvotes: 0

Andrew
Andrew

Reputation: 13191

You seem to be confusing two very different concepts. Following from the sample code you posted, let's assume that:

srcImage = A Python Image Library image, generated from lolmini.jpg.
phrase = A string, 'Hi'.

You're trying to get phrase to appear as text written on top of srcImage. Your current code shows that you plan on doing this by accessing the individual pixels of the image, and assigning a letter to them.

This doesn't work for a few reasons. The primary two are that:

  • You're working with single pixels. A pixel is a picture element. It only ever displays one colour at a time. You cannot represent a letter with a single pixel. The pixel is just a dot. You need multiple pixels together, to form a coherent shape that we recognize as a letter.

  • What does your text of Hi actually look like? When you envision it being written on top of the image, are the letters thin? Do they vary in their size? Are they thick and chunky? Italic? Do they look handwritten? These are all attributes of a font face. Currently, your program has no idea what those letters should look like. You need to give your program the name of a font, so that it knows how to draw the letters from phrase onto the image.

The Python Imaging Library comes with a module specifically for helping you draw fonts. The documentation for it is here:
The ImageFont Module

Your code shows that you have the general idea correct — loop through each letter, place it in the image, and increment the x value so that the next letter doesn't overlap it. Instead of working with the image's pixels, though, you need to load in a font and use the methods shown in the above-linked library to draw them onto the image.

If you take a look at the draw.text() function in the linked documentation, you'll see that you can in fact skip the need to loop through each letter, instead passing the entire string to be used on the image.

I could've added sample code, but as this is a homework assignment I've intentionally left any out. With the linked documentation and your existing code, you hopefully shouldn't have any troubles seeing this through to completion.

Edit:

Just read your comment to another answer, indicating that you are only allowed to use getpixel() and putpixel() for drawing onto the source image. If this is indeed the case, your workload has just increased exponentially.

My comments above stand — a single pixel will not be able to represent a letter. Assuming you're not allowed any outside source code, you will need to create data structures that contain the locations of multiple pixels, which are then all drawn in a specific location in order to represent a particular letter.

You will then need to do this for every letter you want to support.

If you could include the text of the assignment verbatim, I think it would help those here to better understand all of your constraints.

Upvotes: 2

GWW
GWW

Reputation: 44093

Actually, upon further reading, I think the problem is that you are trying to assign a character value to a pixel. You have to figure out some kind of way to actually draw the characters on the image (and within the images boundaries).

Also as a side note since you are using for index,value in enumerate(phrase):

You could use value instead of phrase[index]

Upvotes: 0

Related Questions