Convert RGBA PNG to RGB with PIL

Here’s a version that’s much simpler – not sure how performant it is. Heavily based on some django snippet I found while building RGBA -> JPG + BG support for sorl thumbnails. from PIL import Image png = Image.open(object.logo.path) png.load() # required for png.split() background = Image.new(“RGB”, png.size, (255, 255, 255)) background.paste(png, mask=png.split()[3]) # 3 … Read more

Setting jpg compression level with ImageIO in Java

A more succinct way is to get the ImageWriter directly from ImageIO: ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName(“jpg”).next(); ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); jpgWriteParam.setCompressionQuality(0.7f); ImageOutputStream outputStream = createOutputStream(); // For example implementations see below jpgWriter.setOutput(outputStream); IIOImage outputImage = new IIOImage(image, null, null); jpgWriter.write(null, outputImage, jpgWriteParam); jpgWriter.dispose(); The call to ImageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) is needed in order to explicitly set … Read more

Efficient JPEG Image Resizing in PHP

People say that ImageMagick is much faster. At best just compare both libraries and measure that. Prepare 1000 typical images. Write two scripts — one for GD, one for ImageMagick. Run both of them a few times. Compare results (total execution time, CPU and I/O usage, result image quality). Something which the best everyone else, … Read more

Convert SVG image to PNG with PHP

That’s funny you asked this, I just did this recently for my work’s site and I was thinking I should write a tutorial… Here is how to do it with PHP/Imagick, which uses ImageMagick: $usmap = ‘/path/to/blank/us-map.svg’; $im = new Imagick(); $svg = file_get_contents($usmap); /*loop to color each state as needed, something like*/ $idColorArray = … Read more