-6

I'm new to programming and perhaps I need a reference to a basic intro course to computer science so if you can direct me to that I'll be glad. But here is my question:

I work with Python. I would like to code say an 800x800 image file (JPEG or PNG or PDF). Whatever it is, I want to be able to specify which pixles go where OR if not that specific, perhaps be able to draw lines, circles, insert image onto the canvas and then export it to an image file (JPEG etc).

How do I do that?

I'm sort of aware that I can use pygame and/or tkinter to create things on a canvas and then write that to a file. However that is using the graphics card to render the image, display it and then save it. Correct? Is that how the process works? Python (or any other program w/ APIs) draws things on a canvas, an interface like Tkinter draws that out on a canvas sending the info to the graphics card to display and then we 'export' the raw pixles to an image/JPEG file. Right?

A second question is, is there a way to code a canvas to arrange items and just save it to a file? Without seeing it? Without involving the graphics card?

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

4

Install Pillow. Then,

from PIL import Image, ImageDraw

image = Image.new("RGB", (800, 600), (255,255,255,0))
draw = ImageDraw.Draw(image)
draw.line([(0, 0), (799, 599)], (0, 0, 0, 0), 1)
image.save("test.jpg", "JPEG")
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    Why you install `Pillow` but you still have to import `PIL`? This seems like bad design. – nbro Mar 25 '15 at 02:16
  • 1
    @Rinzler: AFAIK, work on PIL was abandoned, so someone forked it and called it Pillow. The module name was kept for backward compatibility. But make no mistake, I am not importing PIL, I'm importing Pillow. – Amadan Mar 25 '15 at 02:19
  • 1
    I know that. But they could have given it a new name. Backward compatibility does not seem an issue, since Python 3 code would not run mostly with Python 2 interpreters, so why should we care about compatibility? Anyway, it should also follow Python's modules names conventions, using lower cases: `from pil import Image` or `from pillow import Whatever`... – nbro Mar 25 '15 at 02:28
  • 1
    @Rinzler: Oh, that's what you meant. I thought you were asking me why I suggested installation of one package but then never used it and imported another, seemingly unrelated package. I'm not one of Pillow maintainers, you're complaining at a wrong guy :) – Amadan Mar 25 '15 at 02:30
  • @Amadan Thank you. This helps greatly. But a follow up question here. I guess I just would like to know how all of this is done 'under the hood'. With your example with PIL, is the interpreter creating an 800x600 pixles windows and populating a line at pixles 799,5999 thickness 1? And is this pile data then sent to the graphics card? Is that how it works? I should I look into a JPEG algorithm to understand this. Please guide me in the right direction so I can do more research. I just don't know what key term to google to find this out. Thank you. – John Marris Mar 25 '15 at 04:07
  • No. It has nothing to do with a graphics card. It simply allocates some memory that corresponds to a 2D array containing colours, changes that memory so that some elements become `(0, 0, 0, 0)` ("black") rather than the default `(255, 255, 255, 0)` ("white"), then compresses that array in a way prescribed by the JPEG specification. You don't need to understand the JPEG algorithm, just read the Guides and the Reference for `Image` and `ImageDraw` at the link in my answer. – Amadan Mar 25 '15 at 04:11
0

You can use a Canvas widget to create a canvas and place various shapes and elements on it. Having a graphics card and a monitor simply allows for much greater interactivity and feedback. To save the results to an image, see this question. I found the canvas.postscript() method to be effective.

Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97