ใช้ Python สำหรับการบีบอัดรูปภาพ OTG
สร้างแอป Python เพื่อบีบอัดรูปภาพและรูปภาพ OTG ผ่าน API ของเซิร์ฟเวอร์
วิธีบีบอัดรูปภาพและภาพถ่าย OTG ด้วย Python
การปรับแต่งภาพเพื่อการเผยแพร่ไม่เพียงแต่รวมถึงกระบวนการสร้างสรรค์เท่านั้น แต่ยังรวมถึงการปรับเปลี่ยนทางเทคนิคด้วย เช่น การบีบอัดไฟล์ โดยทั่วไป รูปภาพที่มีความละเอียดสูงเป็นสิ่งจำเป็นสำหรับสื่อสิ่งพิมพ์หรือการโฆษณากลางแจ้ง ในขณะที่เว็บไซต์อาจเผชิญกับปัญหาเกี่ยวกับขนาดไฟล์ขนาดใหญ่ การตั้งค่าการบีบอัดภาพอาจแตกต่างกันไปขึ้นอยู่กับวัตถุประสงค์การใช้งานและสถานที่ที่จะเผยแพร่ ไฟล์ขนาดใหญ่อาจใช้เวลานานในการดาวน์โหลดไฟล์ โดยเฉพาะการเชื่อมต่อมือถือ ซึ่งส่งผลกระทบต่อประสบการณ์ผู้ใช้โดยรวม อย่างไรก็ตาม รูปภาพที่ถูกบีบอัดมากเกินไปอาจประสบปัญหาจากความพร่ามัวและพิกเซลที่มองเห็นได้ ซึ่งบั่นทอนคุณภาพของภาพ การสร้างความสมดุลระหว่างขนาดไฟล์และคุณภาพของภาพจำเป็นต้องเลือกอัลกอริธึมและระดับการบีบอัดอย่างระมัดระวัง ในการบีบอัดรูปภาพในรูปแบบ OTG เราจะจ้าง Aspose.Imaging สำหรับ Python ผ่าน .NET API ซึ่งเป็น API การจัดการรูปภาพและการแปลงที่มีคุณลักษณะหลากหลาย ทรงพลัง และใช้งานง่ายสำหรับแพลตฟอร์ม Python คุณสามารถติดตั้งได้โดยใช้คำสั่งต่อไปนี้จากคำสั่งระบบของคุณ
บรรทัดคำสั่งของระบบ
>> pip install aspose-imaging-python-net
ขั้นตอนในการบีบอัด OTGs ผ่าน Python
คุณต้องใช้ aspose-imaging-python-net เพื่อลองใช้เวิร์กโฟลว์ต่อไปนี้ในสภาพแวดล้อมของคุณเอง
- โหลดไฟล์ OTG ด้วยวิธี Image.Load
- บีบอัดภาพ;
- บันทึกภาพที่บีบอัดลงในแผ่นดิสก์ในรูปแบบที่รองรับโดย Aspose.Imaging
ความต้องการของระบบ
Aspose.Imaging สำหรับ Python ได้รับการสนับสนุนในระบบปฏิบัติการหลักทั้งหมด เพียงตรวจสอบให้แน่ใจว่าคุณมีข้อกำหนดเบื้องต้นดังต่อไปนี้
- Microsoft Windows / Linux พร้อม .NET Core Runtime
- ตัวจัดการแพ็คเกจ Python และ PyPi
บีบอัดภาพ OTG - Python
from aspose.imaging.imageoptions import * | |
from aspose.imaging import * | |
from aspose.pycore import as_of, 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 | |
def compress_vector_format_to_svg(): | |
obj_init = [] | |
obj_init.append("cdr") | |
obj_init.append("cmx") | |
obj_init.append("odg") | |
obj_init.append("otg") | |
obj_init.append("eps") | |
format_exts = obj_init | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init2 = CdrRasterizationOptions() | |
obj_init2.page_width = float(image.width) | |
obj_init2.page_height = float(image.height) | |
return obj_init2 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init3 = CmxRasterizationOptions() | |
obj_init3.page_width = float(image.width) | |
obj_init3.page_height = float(image.height) | |
return obj_init3 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init4 = OdgRasterizationOptions() | |
obj_init4.page_width = float(image.width) | |
obj_init4.page_height = float(image.height) | |
return obj_init4 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init5 = OtgRasterizationOptions() | |
obj_init5.page_width = float(image.width) | |
obj_init5.page_height = float(image.height) | |
return obj_init5 | |
else: | |
return None | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init6 = SvgOptions() | |
obj_init6.compress = True | |
obj_init6.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.svgz" | |
page.save(out_file, obj_init6) | |
if delete_output: | |
os.remove(out_file) | |
page_index += 1 | |
else: | |
obj_init7 = SvgOptions() | |
obj_init7.compress = True | |
obj_init7.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".svgz", obj_init7) | |
if delete_output: | |
os.remove(output_file + ".svgz") | |
def compress_vector_format_to_wmf(): | |
obj_init8 = [] | |
obj_init8.append("cdr") | |
obj_init8.append("cmx") | |
obj_init8.append("odg") | |
obj_init8.append("otg") | |
obj_init8.append("eps") | |
format_exts = obj_init8 | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init9 = CdrRasterizationOptions() | |
obj_init9.page_width = float(image.width) | |
obj_init9.page_height = float(image.height) | |
return obj_init9 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init10 = CmxRasterizationOptions() | |
obj_init10.page_width = float(image.width) | |
obj_init10.page_height = float(image.height) | |
return obj_init10 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init11 = OdgRasterizationOptions() | |
obj_init11.page_width = float(image.width) | |
obj_init11.page_height = float(image.height) | |
return obj_init11 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init12 = OtgRasterizationOptions() | |
obj_init12.page_width = float(image.width) | |
obj_init12.page_height = float(image.height) | |
return obj_init12 | |
else: | |
return None | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init13 = WmfOptions() | |
obj_init13.compress = True | |
obj_init13.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.wmz" | |
page.save(out_file, obj_init13) | |
if delete_output: | |
os.remove(out_file) | |
else: | |
obj_init14 = WmfOptions() | |
obj_init14.compress = True | |
obj_init14.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".wmz", obj_init14) | |
if delete_output: | |
os.remove(output_file + ".wmz") | |
def compress_vector_formats_to_emf(): | |
obj_init15 = [] | |
obj_init15.append("cdr") | |
obj_init15.append("cmx") | |
obj_init15.append("odg") | |
obj_init15.append("otg") | |
obj_init15.append("eps") | |
format_exts = obj_init15 | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init16 = CdrRasterizationOptions() | |
obj_init16.page_width = float(image.width) | |
obj_init16.page_height = float(image.height) | |
return obj_init16 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init17 = CmxRasterizationOptions() | |
obj_init17.page_width = float(image.width) | |
obj_init17.page_height = float(image.height) | |
return obj_init17 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init18 = OdgRasterizationOptions() | |
obj_init18.page_width = float(image.width) | |
obj_init18.page_height = float(image.height) | |
return obj_init18 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init19 = OtgRasterizationOptions() | |
obj_init19.page_width = float(image.width) | |
obj_init19.page_height = float(image.height) | |
return obj_init19 | |
else: | |
return None | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init20 = EmfOptions() | |
obj_init20.compress = True | |
obj_init20.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.emz" | |
page.save(out_file, obj_init20) | |
if delete_output: | |
os.remove(out_file) | |
else: | |
obj_init21 = EmfOptions() | |
obj_init21.compress = True | |
obj_init21.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".emz", obj_init21) | |
if delete_output: | |
os.remove(output_file + ".emz") | |
# run | |
compress_vector_formats_to_emf() | |
compress_vector_format_to_svg() | |
compress_vector_format_to_wmf() |
เกี่ยวกับ Aspose.Imaging สำหรับ Python API
Aspose.Imaging API เป็นโซลูชันการประมวลผลรูปภาพเพื่อสร้าง แก้ไข วาดหรือแปลงรูปภาพ (ภาพถ่าย) ภายในแอปพลิเคชัน นำเสนอ: การประมวลผลภาพข้ามแพลตฟอร์ม รวมถึงแต่ไม่จำกัดเพียงการแปลงระหว่างรูปแบบภาพต่างๆ (รวมถึงการประมวลผลภาพแบบหลายหน้าหรือหลายเฟรมแบบเดียวกัน) การปรับเปลี่ยน เช่น การวาด การทำงานกับภาพกราฟิกดั้งเดิม การแปลงภาพ (ปรับขนาด ครอบตัด พลิกและหมุน , ไบนารี, ระดับสีเทา, ปรับ), คุณสมบัติการจัดการภาพขั้นสูง (การกรอง, การแยกสี, การปิดบัง, การเดสก์) และกลยุทธ์การปรับหน่วยความจำให้เหมาะสม เป็นไลบรารีแบบสแตนด์อโลนและไม่ขึ้นกับซอฟต์แวร์ใด ๆ สำหรับการทำงานของรูปภาพ คุณสามารถเพิ่มคุณสมบัติการแปลงรูปภาพประสิทธิภาพสูงด้วย API ดั้งเดิมภายในโปรเจ็กต์ได้อย่างง่ายดาย สิ่งเหล่านี้เป็น API ภายในองค์กรที่เป็นส่วนตัว 100% และอิมเมจได้รับการประมวลผลที่เซิร์ฟเวอร์ของคุณบีบอัด OTGs ผ่าน Online App
บีบอัดเอกสาร OTG โดยไปที่ เว็บไซต์ Live Demos การสาธิตสดมีประโยชน์ดังต่อไปนี้
OTG คืออะไร OTG รูปแบบไฟล์
ไฟล์ OTG เป็นเทมเพลตการวาดที่สร้างขึ้นโดยใช้มาตรฐาน OpenDocument ที่เป็นไปตามข้อกำหนด OASIS Office Applications 1.0 มันแสดงถึงองค์กรเริ่มต้นขององค์ประกอบการวาดภาพสำหรับภาพเวกเตอร์ที่สามารถใช้เพื่อปรับปรุงเนื้อหาของไฟล์เพิ่มเติม
อ่านเพิ่มเติมรูปแบบการบีบอัดอื่น ๆ ที่รองรับ
การใช้ Python สามารถบีบอัดรูปแบบต่างๆ ได้อย่างง่ายดายรวมถึง