Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You seem to be confusing two very different concepts. Following from the sample code you posted, let's assume that: </p> <p><code>srcImage</code> = A Python Image Library image, generated from <code>lolmini.jpg</code>.<br> <code>phrase</code> = A string, <code>'Hi</code>'.</p> <p>You're trying to get <code>phrase</code> to appear as text written on top of <code>srcImage</code>. Your current code shows that you plan on doing this by accessing the individual pixels of the image, and assigning a letter to them.</p> <p>This doesn't work for a few reasons. The primary two are that:</p> <ul> <li><p>You're working with single pixels. A pixel is a <strong>picture element</strong>. 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.</p></li> <li><p>What does your text of <code>Hi</code> 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 <strong>font</strong> 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 <code>phrase</code> onto the image.</p></li> </ul> <p>The Python Imaging Library comes with a module specifically for helping you draw fonts. The documentation for it is here:<br> <a href="http://www.pythonware.com/library/pil/handbook/imagefont.htm" rel="nofollow noreferrer">The ImageFont Module</a></p> <p>Your code shows that you have the general idea correct &mdash; loop through each letter, place it in the image, and increment the <em>x</em> 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.</p> <p>If you take a look at the <code>draw.text()</code> 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.</p> <p>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.</p> <p><strong>Edit</strong>:</p> <p>Just read your comment to another answer, indicating that you are only allowed to use <code>getpixel()</code> and <code>putpixel()</code> for drawing onto the source image. If this is indeed the case, your workload has just increased exponentially. </p> <p>My comments above stand &mdash; 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.</p> <p>You will then need to do this for every letter you want to support. </p> <p>If you could include the text of the assignment verbatim, I think it would help those here to better understand all of your constraints.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload