PPTX DOCX XLSX PDF ODP
Aspose.Imaging  สำหรับ Python
PNG

ใช้ Python สำหรับการปรับรูปภาพ PNG

สร้างแอป Python เพื่อปรับ PNG รูปภาพและรูปภาพผ่าน Server API

วิธีปรับ PNG รูปภาพและภาพถ่ายด้วย Python

ขณะถ่ายภาพ ข้อผิดพลาดอาจเกิดขึ้นได้เนื่องจากการตั้งค่ากล้องไม่ถูกต้อง แสงที่ไม่สามารถควบคุมได้ยังส่งผลต่อผลลัพธ์ และแม้แต่ภาพถ่ายระดับมืออาชีพก็อาจมีข้อบกพร่องได้ อย่างไรก็ตาม ในสถานการณ์ดังกล่าว มีวิธีการปรับปรุงรูปภาพโดยใช้เครื่องมือซอฟต์แวร์ที่มาจากไลบรารี Python คุณมีความสามารถในการปรับแต่งความสว่าง คอนทราสต์ และความสมดุลของสีของภาพได้อย่างละเอียด ตัวอย่างเช่น หากภาพถ่ายดูสลัวเกินไป การเพิ่มความสว่างจะทำให้บริเวณที่มืดสว่างขึ้น ซึ่งเผยให้เห็นรายละเอียดที่แต่ก่อนถูกบดบังด้วยเงามืด หากรูปภาพของคุณแสดงสีที่ไม่พึงประสงค์เนื่องจากแสงประดิษฐ์ คุณสามารถแก้ไขปัญหานี้ได้โดยใช้คุณสมบัติการปรับแกมมาสี หากต้องการดำเนินการแก้ไขเหล่านี้กับไฟล์ PNG คุณสามารถใช้ประโยชน์จาก Aspose.Imaging สำหรับ Python ผ่าน .NET API ซึ่งเป็น API การจัดการรูปภาพและการแปลงที่มีคุณลักษณะหลากหลาย ทรงพลัง และใช้งานง่ายสำหรับแพลตฟอร์ม Python คุณสามารถติดตั้งได้โดยใช้คำสั่งต่อไปนี้จากคำสั่งระบบของคุณ

บรรทัดคำสั่งของระบบ

>> pip install aspose-imaging-python-net

ขั้นตอนในการปรับ PNGs ผ่าน Python

คุณต้องใช้ aspose-imaging-python-net เพื่อลองใช้เวิร์กโฟลว์ต่อไปนี้ในสภาพแวดล้อมของคุณเอง

  • โหลดไฟล์ PNG ด้วยวิธี Image.Load
  • ปรับภาพ;
  • บันทึกภาพที่บีบอัดลงในแผ่นดิสก์ในรูปแบบที่รองรับโดย Aspose.Imaging

ความต้องการของระบบ

Aspose.Imaging สำหรับ Python ได้รับการสนับสนุนในระบบปฏิบัติการหลักทั้งหมด เพียงตรวจสอบให้แน่ใจว่าคุณมีข้อกำหนดเบื้องต้นดังต่อไปนี้

  • Microsoft Windows / Linux พร้อม .NET Core Runtime
  • ตัวจัดการแพ็คเกจ Python และ PyPi
 

ปรับภาพ PNG - Python

from aspose.imaging import *
from aspose.imaging.fileformats.bmp import *
from aspose.imaging.fileformats.dicom import *
from aspose.imaging.fileformats.emf import *
from aspose.imaging.fileformats.jpeg import *
from aspose.imaging.fileformats.jpeg2000 import *
from aspose.imaging.fileformats.png import *
from aspose.imaging.fileformats.psd import *
from aspose.imaging.fileformats.tiff.enums import *
from aspose.imaging.imagefilters.filteroptions import *
from aspose.imaging.imageoptions import *
from aspose.imaging.masking import *
from aspose.imaging.masking.options import *
from aspose.imaging.masking.result import *
from aspose.imaging.sources 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 adjust_gamma_rgb():
# https://apireference.aspose.com/imaging/python-net/aspose.imaging.rasterimage/adjustgamma
filter_images(lambda image: image.adjust_gamma(5, 0.1, 0.1), "adjustgammargb")
def adjust_gamma():
filter_images(lambda image: image.adjust_gamma(3), "adjustgamma")
def adjust_contrast():
filter_images(lambda image: image.adjust_contrast(50), "adjustcontrast")
def adjust_brightness():
filter_images(lambda image: image.adjust_brightness(100), "adjustbrightness")
def filter_images(do_filter, filter_name):
obj_init = []
obj_init.append("jpg")
obj_init.append("png")
obj_init.append("bmp")
obj_init.append("apng")
obj_init.append("dicom")
obj_init.append("jp2")
obj_init.append("j2k")
obj_init.append("tga")
obj_init.append("webp")
obj_init.append("tif")
obj_init.append("gif")
obj_init.append("ico")
raster_formats = obj_init
obj_init2 = []
obj_init2.append("svg")
obj_init2.append("otg")
obj_init2.append("odg")
obj_init2.append("eps")
obj_init2.append("wmf")
obj_init2.append("emf")
obj_init2.append("wmz")
obj_init2.append("emz")
obj_init2.append("cmx")
obj_init2.append("cdr")
vector_formats = obj_init2
all_formats = []
all_formats.extend(raster_formats)
all_formats.extend(vector_formats)
for format_ext in all_formats:
input_file = os.path.join(templates_folder, f"template.{format_ext}")
if not os.path.exists(input_file):
continue
is_vector_format = format_ext in vector_formats
if is_vector_format:
input_file = rasterize_vector_image(format_ext, input_file)
output_file = os.path.join(templates_folder, f"{filter_name}_{format_ext}.png")
print(format_ext)
# explicit type casting from Image to RasterImage
with as_of(Image.load(input_file), RasterImage) as image:
do_filter(image)
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
page_index = 0
for page in multi_page.pages:
file_name = f"{filter_name}_page{page_index}_{format_ext}.png"
page.save(os.path.join(templates_folder + file_name), PngOptions())
# delete an output file
delete_file(os.path.join(templates_folder + file_name))
page_index += 1
else:
image.save(output_file, PngOptions())
# delete an output file
delete_file(output_file)
# delete a rasterized file
if is_vector_format:
delete_file(input_file)
def delete_file(file):
if delete_output:
os.remove(file)
def rasterize_vector_image(format_ext, input_file):
output_file = os.path.join(templates_folder, f"rasterized.{format_ext}.png")
with Image.load(input_file) as image:
image.save(output_file, PngOptions())
return output_file
# call one of the functions
adjust_brightness()
#adjust_contrast()
#adjust_gamma()
#adjust_gamma_rgb()
 
  • เกี่ยวกับ Aspose.Imaging สำหรับ Python API

    Aspose.Imaging API เป็นโซลูชันการประมวลผลรูปภาพเพื่อสร้าง แก้ไข วาดหรือแปลงรูปภาพ (ภาพถ่าย) ภายในแอปพลิเคชัน นำเสนอ: การประมวลผลภาพข้ามแพลตฟอร์ม รวมถึงแต่ไม่จำกัดเพียงการแปลงระหว่างรูปแบบภาพต่างๆ (รวมถึงการประมวลผลภาพแบบหลายหน้าหรือหลายเฟรมแบบเดียวกัน) การปรับเปลี่ยน เช่น การวาด การทำงานกับภาพกราฟิกดั้งเดิม การแปลงภาพ (ปรับขนาด ครอบตัด พลิกและหมุน , ไบนารี, ระดับสีเทา, ปรับ), คุณสมบัติการจัดการภาพขั้นสูง (การกรอง, การแยกสี, การปิดบัง, การเดสก์) และกลยุทธ์การปรับหน่วยความจำให้เหมาะสม เป็นไลบรารีแบบสแตนด์อโลนและไม่ขึ้นกับซอฟต์แวร์ใด ๆ สำหรับการทำงานของรูปภาพ คุณสามารถเพิ่มคุณสมบัติการแปลงรูปภาพประสิทธิภาพสูงด้วย API ดั้งเดิมภายในโปรเจ็กต์ได้อย่างง่ายดาย สิ่งเหล่านี้เป็น API ภายในองค์กรที่เป็นส่วนตัว 100% และอิมเมจได้รับการประมวลผลที่เซิร์ฟเวอร์ของคุณ

    ปรับ PNG ผ่านแอปออนไลน์

    ปรับเอกสาร PNG โดยไปที่ เว็บไซต์สาธิตสด การสาธิตสดมีประโยชน์ดังต่อไปนี้

      ไม่จำเป็นต้องดาวน์โหลดหรือตั้งค่าอะไรเลย
      ไม่ต้องเขียนโค้ดใดๆ
      เพียงอัปโหลดไฟล์ PNG ของคุณและกดปุ่ม ปรับ
      รับลิงค์ดาวน์โหลดทันทีสำหรับไฟล์ผลลัพธ์

    PNG คืออะไร PNG รูปแบบไฟล์

    PNG หรือ Portable Network Graphics หมายถึงรูปแบบไฟล์ภาพแรสเตอร์ชนิดหนึ่งที่ใช้การบีบอัดแบบไม่สูญเสียข้อมูล รูปแบบไฟล์นี้สร้างขึ้นแทน Graphics Interchange Format (GIF) และไม่มีข้อจำกัดด้านลิขสิทธิ์ อย่างไรก็ตาม รูปแบบไฟล์ PNG ไม่รองรับภาพเคลื่อนไหว รูปแบบไฟล์ PNG รองรับการบีบอัดภาพแบบไม่สูญเสียซึ่งทำให้เป็นที่นิยมในหมู่ผู้ใช้ เมื่อเวลาผ่านไป PNG ได้พัฒนาให้เป็นหนึ่งในรูปแบบไฟล์รูปภาพที่ใช้เป็นส่วนใหญ่ ระบบปฏิบัติการเกือบทั้งหมดรองรับการเปิดไฟล์ PNG ตัวอย่างเช่น โปรแกรมแสดง Microsoft Windows มีความสามารถในการเปิดไฟล์ PNG เนื่องจากระบบปฏิบัติการมีการสนับสนุนโดยค่าเริ่มต้นซึ่งเป็นส่วนหนึ่งของการติดตั้ง

    อ่านเพิ่มเติม

    รูปแบบการปรับอื่น ๆ ที่รองรับ

    ด้วยการใช้ Python เราสามารถปรับรูปแบบต่างๆ ได้อย่างง่ายดายรวมถึง

    APNG (กราฟิกเครือข่ายแบบพกพาแบบเคลื่อนไหว)
    BMP (รูปภาพบิตแมป)
    ICO (ไอคอน Windows)
    JPG (กลุ่มผู้เชี่ยวชาญด้านการถ่ายภาพร่วม)
    JPEG (กลุ่มผู้เชี่ยวชาญด้านการถ่ายภาพร่วม)
    DIB (บิตแมปอิสระของอุปกรณ์)
    DICOM (การถ่ายภาพและการสื่อสารดิจิทัล)
    DJVU (รูปแบบกราฟิก)
    DNG (ภาพกล้องดิจิตอล)
    EMF (รูปแบบไฟล์ Metafile ที่ปรับปรุงแล้ว)
    EMZ (Windows บีบอัด Metafile ที่ปรับปรุงแล้ว)
    GIF (รูปแบบการแลกเปลี่ยนกราฟิก)
    JP2 (JPEG 2000)
    J2K (ภาพบีบอัดเวฟเล็ต)
    TIFF (รูปแบบภาพที่ติดแท็ก)
    TIF (รูปแบบภาพที่ติดแท็ก)
    WEBP (รูปภาพเว็บแรสเตอร์)
    WMF (Microsoft Windows Metafile)
    WMZ (สกิน Windows Media Player ที่บีบอัด)
    TGA (Targa Graphic)
    SVG (กราฟิกแบบเวกเตอร์ที่ปรับขนาดได้)
    EPS (ภาษา PostScript ที่ห่อหุ้ม)
    CDR (วาดภาพเวกเตอร์)
    CMX (รูปภาพ Corel Exchange)
    OTG (มาตรฐาน OpenDocument)
    ODG (รูปแบบการวาด Apache OpenOffice)