mirror of https://github.com/xtekky/gpt4free.git
Upload transparency image
This commit is contained in:
parent
e4f743881c
commit
981d83da62
18
g4f/image.py
18
g4f/image.py
|
@ -20,23 +20,23 @@ def to_image(image: ImageType, is_svg: bool = False) -> Image.Image:
|
||||||
try:
|
try:
|
||||||
import cairosvg
|
import cairosvg
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise RuntimeError('Install "cairosvg" package for open svg images')
|
raise RuntimeError('Install "cairosvg" package for svg images')
|
||||||
if not isinstance(image, bytes):
|
if not isinstance(image, bytes):
|
||||||
image = image.read()
|
image = image.read()
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
cairosvg.svg2png(image, write_to=buffer)
|
cairosvg.svg2png(image, write_to=buffer)
|
||||||
image = Image.open(buffer)
|
return Image.open(buffer)
|
||||||
if isinstance(image, str):
|
if isinstance(image, str):
|
||||||
is_data_uri_an_image(image)
|
is_data_uri_an_image(image)
|
||||||
image = extract_data_uri(image)
|
image = extract_data_uri(image)
|
||||||
if isinstance(image, bytes):
|
if isinstance(image, bytes):
|
||||||
is_accepted_format(image)
|
is_accepted_format(image)
|
||||||
image = Image.open(BytesIO(image))
|
return Image.open(BytesIO(image))
|
||||||
elif not isinstance(image, Image.Image):
|
elif not isinstance(image, Image.Image):
|
||||||
image = Image.open(image)
|
image = Image.open(image)
|
||||||
copy = image.copy()
|
copy = image.copy()
|
||||||
copy.format = image.format
|
copy.format = image.format
|
||||||
image = copy
|
return copy
|
||||||
return image
|
return image
|
||||||
|
|
||||||
def is_allowed_extension(filename: str) -> bool:
|
def is_allowed_extension(filename: str) -> bool:
|
||||||
|
@ -138,6 +138,7 @@ def process_image(img: Image.Image, new_width: int, new_height: int) -> Image.Im
|
||||||
Returns:
|
Returns:
|
||||||
Image.Image: The processed image.
|
Image.Image: The processed image.
|
||||||
"""
|
"""
|
||||||
|
# Fix orientation
|
||||||
orientation = get_orientation(img)
|
orientation = get_orientation(img)
|
||||||
if orientation:
|
if orientation:
|
||||||
if orientation > 4:
|
if orientation > 4:
|
||||||
|
@ -148,7 +149,14 @@ def process_image(img: Image.Image, new_width: int, new_height: int) -> Image.Im
|
||||||
img = img.transpose(Image.ROTATE_270)
|
img = img.transpose(Image.ROTATE_270)
|
||||||
if orientation in [7, 8]:
|
if orientation in [7, 8]:
|
||||||
img = img.transpose(Image.ROTATE_90)
|
img = img.transpose(Image.ROTATE_90)
|
||||||
|
# Resize image
|
||||||
img.thumbnail((new_width, new_height))
|
img.thumbnail((new_width, new_height))
|
||||||
|
# Remove transparency
|
||||||
|
if img.mode != "RGB":
|
||||||
|
img.load()
|
||||||
|
white = Image.new('RGB', img.size, (255, 255, 255))
|
||||||
|
white.paste(img, mask=img.split()[3])
|
||||||
|
return white
|
||||||
return img
|
return img
|
||||||
|
|
||||||
def to_base64(image: Image.Image, compression_rate: float) -> str:
|
def to_base64(image: Image.Image, compression_rate: float) -> str:
|
||||||
|
@ -163,8 +171,6 @@ def to_base64(image: Image.Image, compression_rate: float) -> str:
|
||||||
str: The base64-encoded image.
|
str: The base64-encoded image.
|
||||||
"""
|
"""
|
||||||
output_buffer = BytesIO()
|
output_buffer = BytesIO()
|
||||||
if image.mode != "RGB":
|
|
||||||
image = image.convert('RGB')
|
|
||||||
image.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
|
image.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
|
||||||
return base64.b64encode(output_buffer.getvalue()).decode()
|
return base64.b64encode(output_buffer.getvalue()).decode()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue