JPEG – photos from Ruby
In lesson 50 you painted PNG pictures with chunky_png. PNG keeps every pixel exactly – ideal for logos, pixel art and screenshots, but huge for photos. That is why almost every photo is a JPEG: it leaves out what the eye barely notices, and gets much smaller for it.
Usually a library written in C does this. pure_jpeg by Peter Cooper writes and reads JPEGs in pure Ruby – which is why it runs here in your browser. Let's paint a sunset, pixel by pixel:
install_gem "pure_jpeg"
require "pure_jpeg"
picture = PureJPEG::Source::RawSource.new(96, 64) do |x, y|
if (x - 48)**2 + (y - 40)**2 < 18**2
[255, 210, 60] # the sun
else
[40 + y * 3, 70 + y, 170 - y] # the sky, redder further down
end
end
jpeg = PureJPEG.encode(picture, quality: 85)
show_image jpeg
jpeg.to_bytes.bytesizeRawSource.new(96, 64) calls the block once for every pixel: with x from 0 to 95 going right and y from 0 to 63 going down – unlike the PDF in the last lesson. The block returns the colour as [red, green, blue], each from 0 to 255. The sun is a circle: every point less than 18 pixels away from (48, 40) – Pythagoras, without the square root.
PureJPEG.encode turns it into a JPEG, show_image shows it, and to_bytes returns the finished file: about 1.4 KB. The raw pixels would be 96 × 64 × 3 = 18,432 bytes, more than thirteen times as much. How much JPEG leaves out is up to quality:, from 1 to 100:
[90, 30, 5].each do |quality|
data = PureJPEG.encode(picture, quality: quality).to_bytes
show_image data
puts "quality #{quality}: #{data.bytesize} bytes"
endThe smaller the number, the smaller the file – and the clearer the little squares. JPEG cuts the picture into blocks of 8 × 8 pixels and describes each block as a mix of wave patterns, from completely smooth to very fine (the discrete cosine transform). The lower the quality, the more of the fine waves it rounds away. Smooth gradients like the sky survive that well, hard edges like the rim of the sun do not. Hence: photos as JPEG, logos and screenshots as PNG.
pure_jpeg reads JPEGs back in, too:
photo = PureJPEG.read(jpeg.to_bytes)
middle = photo[48, 40] # right in the sun
[photo.width, photo.height, middle.r, middle.g, middle.b]PureJPEG.read takes the bytes or a file name and returns a picture; photo[x, y] is a pixel with r, g and b. The sun was [255, 210, 60] – what comes back is almost the same, but not quite: JPEG is lossy, every save loses a little.
PureJPEG.encode takes any object that knows width, height and [x, y] – a photo it has read, too. That is how you transform pictures, into a negative, say, or into greyscale:
negative = PureJPEG::Source::RawSource.new(photo.width, photo.height) do |x, y|
pixel = photo[x, y]
[255 - pixel.r, 255 - pixel.g, 255 - pixel.b]
end
show_image PureJPEG.encode(negative)
PureJPEG.encode(photo, grayscale: true).write("sun-gray.jpg")
show_image "sun-gray.jpg"
PureJPEG.info("sun-gray.jpg")write saves the JPEG as a file – it appears as a download below the cell, and show_image takes its name too. PureJPEG.info only reads the file's header, without unpacking the pixels: the size, and component_count – 1 channel for greyscale, 3 for colour.
gem install pure_jpeg, and write creates a real file. PureJPEG.read("holiday.jpg") reads photos from your camera just the same, progressive JPEGs included. With chunky_png it works both ways: PureJPEG.from_chunky_png(png, quality: 80) turns a PNG into a JPEG, and photo.each_pixel { |x, y, pixel| … } lets you paint a JPEG into a ChunkyPNG::Image pixel by pixel.
Limits: EXIF data (camera, date, GPS) is lost when you save again, and Ruby computes the waves more slowly than C – for big photos, reach for a C library such as libvips (the ruby-vips gem). In return, pure_jpeg runs wherever Ruby runs, even here. Peter Cooper published it under the MIT licence.
postcard.jpg, 80 × 60 pixels: the top half sky blue [100, 160, 230], the bottom half grass green [60, 160, 60].# postcard.jpg: 80 × 60, top half sky blue, bottom half grass green