Posts

Showing posts with the label Png

Creating A PNG File In Python

Answer : Simple PNG files can be generated quite easily from pure Python code - all you need is the standard zlib module and some bytes-encoding to write the chunks. Here is a complete example that the casual reader may use as a starter for their own png generator: #! /usr/bin/python """ Converts a list of list into gray-scale PNG image. """ __copyright__ = "Copyright (C) 2014 Guido Draheim" __licence__ = "Public Domain" import zlib import struct def makeGrayPNG(data, height = None, width = None): def I1(value): return struct.pack("!B", value & (2**8-1)) def I4(value): return struct.pack("!I", value & (2**32-1)) # compute width&height from data if not explicit if height is None: height = len(data) # rows if width is None: width = 0 for row in data: if width < len(row): width = len(row) # generate these chunks...

Can PNG Image Transparency Be Preserved When Using PHP's GDlib Imagecopyresampled?

Answer : imagealphablending( $targetImage, false ); imagesavealpha( $targetImage, true ); did it for me. Thanks ceejayoz. note, the target image needs the alpha settings, not the source image. Edit: full replacement code. See also answers below and their comments. This is not guaranteed to be be perfect in any way, but did achieve my needs at the time. $uploadTempFile = $myField[ 'tmp_name' ] list( $uploadWidth, $uploadHeight, $uploadType ) = getimagesize( $uploadTempFile ); $srcImage = imagecreatefrompng( $uploadTempFile ); $targetImage = imagecreatetruecolor( 128, 128 ); imagealphablending( $targetImage, false ); imagesavealpha( $targetImage, true ); imagecopyresampled( $targetImage, $srcImage, 0, 0, 0, 0, 128, 128, $uploadWidth, $uploadHeight ); imagepng( $targetImage, 'out.png', 9 ); Why do you make things so complicated? the following is what I use and so far...

Convert SVG To Transparent PNG With Antialiasing, Using ImageMagick

Answer : As a side note, I found that getting transparency was a bit tricky. Instead of using transparent , I had to use none . convert -background none in.svg out.png Inkscape will do this: inkscape \ --export-png=out.png --export-dpi=200 \ --export-background-opacity=0 --without-gui in.svg Update The terminology has changed: all the export params suppress gui, and the output parameter is now simply based on the file type. For example, a type of png will cause a file in /path/to/picture.svg to be exported as /path/to/picture.png (caution: this overwrites output). inkscape \ --export-type=png --export-dpi=200 \ --export-background-opacity=0 picture.svg Note cited wiki has quotes on --export-type=png , which is incorrect. Also if don't have Inkscape command line, MacOS can access via bash directly: /Applications/Inkscape.app/Contents/MacOS/inkscape Actually, reading imagemagick documentation: -antialias Enable/Disable of the rendering of anti-aliasing pix...

Convert RGBA PNG To RGB With PIL

Image
Answer : 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 is the alpha channel background.save('foo.jpg', 'JPEG', quality=80) Result @80% Result @ 50% By using Image.alpha_composite , the solution by Yuji 'Tomita' Tomita become simpler. This code can avoid a tuple index out of range error if png has no alpha channel. from PIL import Image png = Image.open(img_path).convert('RGBA') background = Image.new('RGBA', png.size, (255,255,255)) alpha_composite = Image.alpha_composite(background, png) alpha_composite.save('foo.jpg', 'JPEG', quality=80) The transparent parts mostly have RGBA valu...

Convert Video To Apng/png?

Answer : This sort of thing has been done before (with gif) using imagemagick. APNG is not an official file format - the PNG group doesn't support this extension. Even if you get this to work, you are likely going to have lingering issues. You should probably consider an animated gif, unless you have some reason you are forced to use png. Checkout the page on Video Handling. Also take a look at Animation Basics and Optimization. I appear to be able to convert avi to apng directly on linux, but it fails on windows. A work around on windows would be to convert the movie to a sequence of stills: convert test.avi frame%04d.png if you want to use ffmpeg, this will extract frames every 5 seconds: ffmpeg -i test.avi -y -ss 5 -an -r 1/5 frame%03d.png then making the animation by using the apng edit firefox plugin. mng never gained much support, but APNG apparently has some support these days (FF/Chrome/Safari); more than WebP (Chrome/Opera). Be sure to check for browser support fo...

Converting A PDF To PNG

Answer : You can use one commandline with two commands ( gs , convert ) connected through a pipe, if the first command can write its output to stdout, and if the second one can read its input from stdin. Luckily, gs can write to stdout ( ... -o %stdout ... ). Luckily, convert can read from stdin ( convert -background transparent - output.png ). Problem solved: GS used for alpha channel handling a special image, convert used for creating transparent background, pipe used to avoid writing out a temp file on disk. Complete solution: gs -sDEVICE=pngalpha \ -o %stdout \ -r144 cover.pdf \ | \ convert \ -background transparent \ - \ cover.png Update If you want to have a separate PNG per PDF page, you can use the %d syntax: gs -sDEVICE=pngalpha -o file-%03d.png -r144 cover.pdf This will create PNG files named page-000.png , page-001.png , ... (Note that the %d -counting is zero...

Converting Dot To Png In Python

Answer : Load the file with pydot.graph_from_dot_file to get a pydot.Dot class instance. Then write it to a PNG file with the write_png method. import pydot (graph,) = pydot.graph_from_dot_file('somefile.dot') graph.write_png('somefile.png') pydot needs the GraphViz binaries to be installed anyway, so if you've already generated your dot file you might as well just invoke dot directly yourself. For example: from subprocess import check_call check_call(['dot','-Tpng','InputFile.dot','-o','OutputFile.png']) You can use pygraphviz. Once you have a graph loaded, you can do graph.draw('file.png')