Skip to content

Commit

Permalink
Small update
Browse files Browse the repository at this point in the history
  • Loading branch information
KazukiPrzyborowski committed Nov 15, 2024
1 parent cbf9743 commit 2e4ad73
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
24 changes: 23 additions & 1 deletion upcean/predraw/premagick.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_save_filename(outfile):
if isinstance(outfile, str):
outfile = outfile.strip()
if outfile in ["-", ""]:
return (outfile, None)
return (outfile, "PNG")
base, ext = os.path.splitext(outfile)
ext = ext[1:].upper() if ext else None
if ext and ext in PYTHONMAGICK_SUPPORTED_EXTENSIONS:
Expand Down Expand Up @@ -155,11 +155,33 @@ def save_to_file(inimage, outfile, outfileext, imgcomment="barcode"):
upc_img.magick(outfileext.upper())

# Handle output destinations
uploadfile = None
outfiletovar = False
if re.match("^(ftp|ftps|sftp):\\/\\/", str(outfile)):
uploadfile = outfile
outfile = BytesIO()
elif outfile == "-":
outfiletovar = True
outfile = BytesIO()

# Handle output destinations
print(dir(upc_img))
if isinstance(outfile, file):
upc_img.write(outfile) # Save to a file-like object
else:
upc_img.write(outfile) # Save to a file path

# Handle FTP uploads or variable output
if uploadfile:
outfile.seek(0)
upload_file_to_internet_file(outfile, uploadfile)
outfile.close()
elif outfiletovar:
outfile.seek(0)
outbyte = outfile.read()
outfile.close()
return outbyte

return True

def save_to_filename(imgout, outfile, imgcomment="barcode"):
Expand Down
29 changes: 14 additions & 15 deletions upcean/predraw/prewand.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes
from upcean.xml.downloader import upload_file_to_internet_file
from wand.image import Image
from wand.image import Image, INTERLACE_LINE, INTERLACE_PANE
from wand.drawing import Drawing
from wand.color import Color
from wand.version import formats
Expand Down Expand Up @@ -217,11 +217,10 @@ def save_to_file(inimage, outfile, outfileext, imgcomment="barcode"):
upc_img = inimage[0]
upc_preimg = inimage[1]

# Set the format for the image
upc_img.magick(outfileext.upper()) # Ensure format is uppercase for compatibility
upc_img.format = outfileext.lower()

# Set image comment
upc_img.comment(imgcomment)
upc_img.metadata["comment"] = imgcomment

# Handle output destinations
uploadfile = None
Expand All @@ -235,22 +234,22 @@ def save_to_file(inimage, outfile, outfileext, imgcomment="barcode"):

# Set specific options for formats
if outfileext.lower() == "png":
upc_img.depth(8) # Set bit depth
upc_img.interlaceType(PythonMagick.InterlaceType.LineInterlace)
upc_img.compressionQuality(100) # Set to high quality for PNG
upc_img.depth = 24 # Set bit depth
upc_img.interlace_scheme = INTERLACE_LINE # Use LINE interlacing for PNG
upc_img.compression_quality = 100 # Set to high quality for PNG
elif outfileext.lower() in ["jpg", "jpeg"]:
upc_img.interlaceType(PythonMagick.InterlaceType.PlaneInterlace)
upc_img.quality(100) # Set high quality for JPEG
upc_img.interlace_scheme = INTERLACE_PLANE # Use PLANE interlacing for JPEG
upc_img.compression_quality = 100 # Set high quality for JPEG
elif outfileext.lower() == "webp":
upc_img.quality(100) # Set high quality for WEBP
# Note: `lossless` is not directly supported in PythonMagick
upc_img.compression_quality = 100 # Set high quality for WEBP
# Note: Lossless WEBP support depends on Wand/ImageMagick version.
elif outfileext.lower() == "tiff":
upc_img.compression(PythonMagick.CompressionType.LZWCompression) # Use LZW compression for TIFF
upc_img.interlaceType(PythonMagick.InterlaceType.LineInterlace)
upc_img.compression = "lzw" # Use LZW compression for TIFF
upc_img.interlace_scheme = INTERLACE_LINE # Use LINE interlacing for TIFF
elif outfileext.lower() == "bmp":
upc_img.depth(24) # Set bit depth to 24 for BMP
upc_img.depth = 24 # Set bit depth to 24 for BMP
elif outfileext.lower() == "gif":
upc_img.type(PythonMagick.ImageType.PaletteType) # Set to palette type for GIF
upc_img.type = "palette" # Set to palette type for GIF

# Save the image
try:
Expand Down

0 comments on commit 2e4ad73

Please sign in to comment.