Convert the Jpg+mask file in the turret legend to a PNG file with transparent channels

Source: Internet
Author: User

The actual operation is very technical, but would like to show the Almighty Python. Although I am disgusted with the text encoding and whitespace alignment of Python, I have to say that its interface design is excellent, making complex work very simple, and this idea is reflected in the third-party extension.

Turret legend uses the form of jpg+mask files to achieve the purpose of compressing resources. The compression ratio of JPG is excellent, and mask contains transparent channel information, so that the image size can be greatly compressed.

However, I personally prefer PNG, using png8+ compression texture is not worse than this scheme, in exchange for faster loading speed and memory savings. Especially in unity, I prefer unity to manage textures, rather than worrying about myself.

#coding: Utf-8import os;from PIL import image;# convert mask+jpg files in turret legend to PNG files def doconvertjpg (Jpgpath, Maskpath):    Pngpath = Jpgpath.replace ('. jpg ', '. png ');    Maskdata = open (Maskpath, ' RB '). read ();    Jpgdata = open (Jpgpath, ' RB '). read ();    JPG = Image.open (jpgpath);    width = jpg.size[0];    Height = jpg.size[1];    PNG = Jpg.convert (' RGBA ')    mask = Image.open (Maskpath);    Png.putalpha (mask);    Png.save (Pngpath);    Os.remove (Jpgpath);    Os.remove (maskpath);d EF doconvertpath (path):    for Root, dirs, files on Os.walk (path): For        file in files:            If File.find ('. jpg ')! =-1:                FullPath = os.path.join (root, file);                Maskpath = Fullpath.replace ('. jpg ', ' _alpha_mask ');                If Os.path.exists (maskpath):                    doconvertjpg (FullPath, Maskpath);d oconvertpath (' UI '); Os.system (' PAUSE ')

The most magical is the Image.putalpha function, which has done all the work that needs to be done.

Convert the Jpg+mask file in the turret legend to a PNG file with transparent channels

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.