1

From my current understanding, png is relatively easier to decode than bitmap-based formats like jpg in python and is already implemented in python elsewhere. For my own purposes though I need the jpg format.

What are good resources for building a jpg library from scratch? At the moment I only wish to support the resizing of images, but this would presumably involve both encoding/decoding ops.

Edit: to make myself more clear: I am hoping that there is a high level design type treat of how to implement a jpg library in code: specifically considerations when encoding/decoding, perhaps even pseudocode. Maybe it doesn't exist, but better to ask and stand on the shoulders of giants rather than reinvent the wheel.

user1561108
  • 2,666
  • 9
  • 44
  • 69
  • 2
    how about [this](http://www.jpeg.org/public/jfif.pdf)? (pdf) – Elazar May 25 '13 at 22:11
  • 2
    and in general: http://www.jpeg.org/jpeg/index.html – Elazar May 25 '13 at 22:12
  • 2
    PNG and JPG are _both_ bitmap-based formats. The main difference is that PNG is a losslessly-compressed bitmap, while JPG is (usually) a lossily-compressed bitmap. (PNG also has a _simpler_ compression algorithm, and it's also got a more regular container structure, but those aren't the reasons people choose between PNG and JPG.) If you don't even know these basics, you might want to start at [Wikipedia](http://en.wikipedia.org/wiki/JPEG), because it will explain all the tricky stuff, as well as giving you links to all of the details, which will be a lot easier than diving right into details. – abarnert May 25 '13 at 22:18
  • In addition to great points above. Another thing you will have to research is how to build a library. And it could grow into pretty hefty task on its own. Check this out: http://stackoverflow.com/questions/1302141/how-do-i-create-and-maintain-a-code-reuse-library – PSS May 25 '13 at 22:21
  • 1
    Meanwhile… why are you doing this? If the reason you're planning to do this from scratch is that you thought there were no JPEG implementations for Python, you're wrong, there are. If you're doing it to learn about image-processing code, you'd be much better off starting with something simpler—maybe even XPM or BMP. – abarnert May 25 '13 at 22:21
  • Someone needs to batch all our comments in one great answer :) – PSS May 25 '13 at 22:22
  • Elazar: Yes, but I was hoping that since image manipulation is such a common task there would be higher-level design discussions somewhere on best-practices for implementing jpg libs. abarnet: examples would make your last comment a lot more valid. PSS: I didn't ask for info on how to create a library. I have done that many times, cheers... – user1561108 May 25 '13 at 23:34
  • Also Elazar - you link to jfif - I'm not sure I understand why? – user1561108 May 25 '13 at 23:43

1 Answers1

2

Use PIL, it already has highlevel APIs for image handling.

If you say "I don't want to use PIL" (and remember, there are private/unofficial ports to 3.x) then I would say read the wikipedia article on JPEG, as it will describe the basics, and also links to in depth articles/descriptions of the JPEG format.

Once you read over that, pull up the source code for PIL JPEGS to see what they are doing there (it is surprisingly simple stuff) The only things they import really, are Image, which is a class they made to hold the raw image data.

Jacob Valenta
  • 6,659
  • 8
  • 31
  • 42
  • As you already might have guessed, I'm looking for a pure python implementation. Edit: ah, I think I see what you're saying. The only native C stuff is the Image class which is just a naive binary data holding class? – user1561108 May 26 '13 at 00:34
  • Yeah, looking through the code, you can see what they do. They read in the data and just stick it in an Image class. You would be creating that same kind of image class (maybe store them as lists of lists?) and is where you would be putting your APIs. `Image.resize()` would have an algorithm that it runs on it's own data to calculate how to resize. – Jacob Valenta May 26 '13 at 00:51