ใช้ Python สำหรับการแปลงรูปภาพจาก DJVU เป็น BMP
สร้างแอป Python เพื่อแปลง DJVU เป็น BMP รูปภาพและรูปภาพผ่าน API ของเซิร์ฟเวอร์
วิธีแปลง DJVU เป็น BMP รูปภาพและภาพถ่ายด้วย Python
การแปลงไฟล์รูปภาพจากรูปแบบหนึ่งไปเป็นอีกรูปแบบหนึ่งเป็นงานทั่วไปที่นักออกแบบกราฟิกทุกคนต้องเผชิญ ประสิทธิภาพและความเป็นเลิศในการแปลงไฟล์ไม่เพียงแต่ส่งผลต่อความเร็วในการเสร็จสิ้นเท่านั้น แต่ยังมีบทบาทสำคัญในการประเมินคุณภาพงานโดยรวมอีกด้วย ในส่วนของแหล่งที่มาของภาพ มักจำเป็นต้องแปลงเป็นรูปแบบอื่นที่เหมาะกับการพิมพ์หรือการเผยแพร่ทางออนไลน์มากกว่า รูปภาพที่สร้างขึ้นในโปรแกรมแก้ไขกราฟิกมีแนวโน้มที่จะอยู่ในรูปแบบเวกเตอร์ ในกรณีเช่นนี้ สำหรับการเผยแพร่บนเว็บไซต์ จะต้องผ่านการแรสเตอร์และบันทึกในรูปแบบแรสเตอร์ คุณมีตัวเลือกในการแปลงรูปภาพในรูปแบบที่ไม่มีการบีบอัดเพื่อคุณภาพที่เหนือกว่า หรือบันทึกเป็นรูปแบบการบีบอัดแบบไม่สูญเสียข้อมูลเพื่อลดขนาดไฟล์ สำหรับสถานการณ์ที่จำเป็นต้องลดขนาดไฟล์ เช่น ในแอปพลิเคชันเว็บไซต์ มีความเป็นไปได้ที่จะแปลงเป็นรูปแบบการบีบอัดที่สูญเสียข้อมูล อัลกอริธึมการบีบอัดข้อมูลเฉพาะสำหรับรูปภาพสามารถลดขนาดไฟล์ได้อย่างมาก ขณะเดียวกันก็รักษาคุณภาพของภาพที่ยอมรับได้ ทำให้มั่นใจได้ว่าการโหลดรูปภาพจะรวดเร็ว ในการแปลงรูปภาพและภาพถ่ายจาก DJVU เป็น BMP เราจะจ้าง Aspose.Imaging สำหรับ Python ผ่าน .NET API ซึ่งเป็น API การจัดการรูปภาพและการแปลงที่มีคุณลักษณะหลากหลาย ทรงพลัง และใช้งานง่ายสำหรับแพลตฟอร์ม Python คุณสามารถติดตั้งได้โดยใช้คำสั่งต่อไปนี้จากคำสั่งระบบของคุณ
บรรทัดคำสั่งของระบบ
>> pip install aspose-imaging-python-net
ขั้นตอนในการแปลง DJVU เป็น BMP ผ่าน Python
นักพัฒนาสามารถโหลดและแปลงไฟล์ DJVU เป็น BMP ได้อย่างง่ายดายด้วยโค้ดเพียงไม่กี่บรรทัด
- โหลดไฟล์ DJVU ด้วย Image.Load method
- สร้างและตั้งค่าอินสแตนซ์ของคลาสย่อยที่จำเป็นของ ImageOptionsBase (เช่น BmpOptions, PngOptions เป็นต้น)
- เรียกวิธี Image.Save
- ส่งเส้นทางไฟล์ด้วยนามสกุล BMP และวัตถุของคลาส ImageOptionsBase
ความต้องการของระบบ
ก่อนรันโค้ดตัวอย่างการแปลง ตรวจสอบให้แน่ใจว่าคุณมีข้อกำหนดเบื้องต้นดังต่อไปนี้
- ระบบปฏิบัติการ: Windows หรือ Linux
- สภาพแวดล้อมการพัฒนา: รองรับ .NET Core 7 และสูงกว่า เช่น Microsoft Visual Studio
แอปฟรีเพื่อแปลง DJVU เป็น BMP
- เลือกหรือลากและวาง DJVU image
- เลือกรูปแบบแล้วคลิกปุ่มแปลง
- คลิกปุ่มดาวน์โหลดเพื่อดาวน์โหลด BMP ภาพ
แปลง DJVU เป็น BMP - Python
from aspose.imaging import * | |
from aspose.imaging.fileformats.tiff.enums import * | |
from aspose.imaging.fileformats.jpeg2000 import * | |
from aspose.imaging.fileformats.png import * | |
from aspose.imaging.imageoptions import * | |
from aspose.pycore import is_assignable | |
import os | |
if 'TEMPLATE_DIR' in os.environ: | |
templates_folder = os.environ['TEMPLATE_DIR'] | |
else: | |
templates_folder = r"C:\Users\USER\Downloads\templates" | |
delete_output = 'SAVE_OUTPUT' not in os.environ | |
data_dir = templates_folder | |
def process_convertion(): | |
import_formats, export_formats = get_available_image_formats() | |
for import_key, import_value in import_formats.items(): | |
format_ext = import_key | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
if not os.path.exists(input_file): | |
continue | |
for export_key, export_value in export_formats.items(): | |
output_file = os.path.join(templates_folder, f"convert-{format_ext}-to-{export_key}.{export_key}") | |
print("Processing conversion:" + output_file) | |
with Image.load(input_file) as image: | |
export_options = export_value.clone() | |
if is_assignable(image, VectorImage): | |
rasterization_options = import_value | |
rasterization_options.page_width = float(image.width) | |
rasterization_options.page_height = float(image.height) | |
export_options.vector_rasterization_options = rasterization_options | |
image.save(output_file, export_options) | |
if delete_output: | |
os.remove(output_file) | |
def get_available_image_formats(): | |
obj_init = Jpeg2000Options() | |
obj_init.codec = Jpeg2000Codec.J2K | |
obj_init2 = Jpeg2000Options() | |
obj_init2.codec = Jpeg2000Codec.JP2 | |
obj_init3 = PngOptions() | |
obj_init3.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
obj_init4 = {} | |
obj_init4["bmp"] = BmpOptions() | |
obj_init4["gif"] = GifOptions() | |
obj_init4["dicom"] = DicomOptions() | |
obj_init4["jpg"] = JpegOptions() | |
obj_init4["jpeg"] = JpegOptions() | |
obj_init4["jpeg2000"] = Jpeg2000Options() | |
obj_init4["j2k"] = obj_init | |
obj_init4["jp2"] = obj_init2 | |
obj_init4["png"] = obj_init3 | |
obj_init4["apng"] = ApngOptions() | |
obj_init4["tiff"] = TiffOptions(TiffExpectedFormat.DEFAULT) | |
obj_init4["tif"] = TiffOptions(TiffExpectedFormat.DEFAULT) | |
obj_init4["tga"] = TgaOptions() | |
obj_init4["webp"] = WebPOptions() | |
obj_init4["ico"] = IcoOptions(FileFormat.PNG, 24) | |
raster_formats_that_support_export_and_import = obj_init4 | |
obj_init5 = EmfOptions() | |
obj_init5.compress = True | |
obj_init6 = WmfOptions() | |
obj_init6.compress = True | |
obj_init7 = SvgOptions() | |
obj_init7.compress = True | |
obj_init8 = {} | |
obj_init8["emf"] = (EmfOptions(), EmfRasterizationOptions()) | |
obj_init8["svg"] = (SvgOptions(), SvgRasterizationOptions()) | |
obj_init8["wmf"] = (WmfOptions(), WmfRasterizationOptions()) | |
obj_init8["emz"] = (obj_init5, EmfRasterizationOptions()) | |
obj_init8["wmz"] = (obj_init6, WmfRasterizationOptions()) | |
obj_init8["svgz"] = (obj_init7, SvgRasterizationOptions()) | |
vector_formats_that_support_export_and_import = obj_init8 | |
obj_init9 = DxfOptions() | |
obj_init9.text_as_lines = True | |
obj_init9.convert_text_beziers = True | |
obj_init10 = {} | |
obj_init10["psd"] = PsdOptions() | |
obj_init10["dxf"] = obj_init9 | |
obj_init10["pdf"] = PdfOptions() | |
obj_init10["html"] = Html5CanvasOptions() | |
formats_only_for_export = obj_init10 | |
obj_init11 = {} | |
obj_init11["djvu"] = None | |
obj_init11["dng"] = None | |
obj_init11["dib"] = None | |
formats_only_for_import = obj_init11 | |
obj_init12 = {} | |
obj_init12["eps"] = EpsRasterizationOptions() | |
obj_init12["cdr"] = CdrRasterizationOptions() | |
obj_init12["cmx"] = CmxRasterizationOptions() | |
obj_init12["otg"] = OtgRasterizationOptions() | |
obj_init12["odg"] = OdgRasterizationOptions() | |
vector_formats_only_for_import = obj_init12 | |
# Get total set of formats to what we can export images | |
export_formats = {k: v[0] for k, v in vector_formats_that_support_export_and_import.items()} | |
export_formats.update(formats_only_for_export) | |
export_formats.update(raster_formats_that_support_export_and_import) | |
# Get total set of formats that can be loaded | |
import_formats = {k : VectorRasterizationOptions() for k in formats_only_for_import} | |
import_formats.update(vector_formats_only_for_import) | |
import_formats.update({k : v[1] for k, v in vector_formats_that_support_export_and_import.items()}) | |
return import_formats, export_formats | |
# run | |
process_convertion() |
DJVU คืออะไร DJVU รูปแบบไฟล์
DjVu ออกเสียงว่า DJVU รูปแบบไฟล์กราฟิกที่ใช้สำหรับสแกนเอกสารและหนังสือ โดยเฉพาะไฟล์ที่มีข้อความ ภาพวาด รูปภาพ และรูปถ่ายผสมกัน ได้รับการพัฒนาโดย AT&T Labs มันใช้เทคนิคหลายอย่าง เช่น การแยกเลเยอร์รูปภาพของข้อความและรูปภาพพื้นหลัง การโหลดแบบโปรเกรสซีฟ การเข้ารหัสเลขคณิต และการบีบอัดแบบไม่สูญเสียสำหรับรูปภาพ bitonal เนื่องจากไฟล์ DJVU สามารถมีรูปภาพสี ข้อความ และภาพวาดที่บีบอัดแต่คุณภาพสูง และสามารถบันทึกได้ในพื้นที่น้อยลง ดังนั้นจึงใช้บนเว็บเป็น eBook คู่มือ หนังสือพิมพ์ เอกสารโบราณ ฯลฯ
อ่านเพิ่มเติม | DJVUBMP คืออะไร BMP รูปแบบไฟล์
ไฟล์ที่มีนามสกุล .BMP แสดงถึงไฟล์ภาพบิตแมปที่ใช้เก็บภาพดิจิทัลบิตแมป รูปภาพเหล่านี้ไม่ขึ้นกับการ์ดจอและเรียกอีกอย่างว่ารูปแบบไฟล์บิตแมปที่ไม่ขึ้นกับอุปกรณ์ (DIB) ความเป็นอิสระนี้มีจุดประสงค์ในการเปิดไฟล์บนหลายแพลตฟอร์ม เช่น Microsoft Windows และ Mac รูปแบบไฟล์ BMP สามารถจัดเก็บข้อมูลเป็นภาพดิจิทัลสองมิติทั้งในรูปแบบขาวดำและรูปแบบสีที่มีความลึกของสีต่างๆ
อ่านเพิ่มเติม | BMPการแปลงอื่น ๆ ที่รองรับ
การใช้ Python จะทำให้สามารถแปลงรูปแบบต่างๆ ได้อย่างง่ายดายรวมถึง